<?php

namespace App\Http\Controllers\V1;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Core\Service\PropertyContactService;
use App\Core\Service\PropertyService;

use App;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
use Exception;
use App\Exceptions\ApiErrorException;

class PropertyContactController extends Controller
{

    private $request;
    private $propertyContactService;
    private $propertyService;


    public function __construct(
        Request $request,
        PropertyService $propertyService,
        PropertyContactService $propertyContactService
    )
    {
        $this->request = $request;
        $this->propertyService = $propertyService ;
        $this->propertyContactService = $propertyContactService;

    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function getPropertyContact()
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;

            $requestParams = [
                'property_id' => fillOnUndefined($params, 'property_id'),
            ];
            $propertyContact = $this->propertyContactService->propertyContact($requestParams);

            if ($propertyContact['status'] != 'success'){
                throw new Exception($propertyContact['message']);
            }
            $requestParams = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => fillOnUndefined($params, 'property_id')],
                ],
                'firstRow' => 1
            ];
            $property = $this->propertyService->select($requestParams, ['country']);
            if($property['status'] != 'success'){
                throw new Exception($property['message']);
            }
            $propertyContact['data']['country_code'] =  strtolower($property['data']['country']) ;


            $responseData = $propertyContact['data'];

            $propertyRequest = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']],
                ],
                'firstRow' => true
            ];

            $getPropertyFields = ['id', 'name', 'official_name','tax_office', 'tax_number'];
            $property = $this->propertyService->select($propertyRequest, $getPropertyFields);

            if($property['status'] != 'success'){
                throw new Exception($property['message']);
            }
            $responseData['get_property'] = $property['data'] ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $responseData];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    /**
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateOrCreatePropertyContact(Request $request)
    {
        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];

        try {

            if (is_null($this->request->getContent())) {
                throw new ApiErrorException(lang('Parameter Error.'));
            }

            $params = $this->request->params;

            $requestParams = [
                'contact' => fillOnUndefined($params, 'contact'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'user_id' => $request->credentials->user_id,
            ];
            $propertyContact = $this->propertyContactService->propertyContactUpdateOrCreate($requestParams);


            if($propertyContact['status'] != 'success'){

                throw new ApiErrorException($propertyContact['message']);
            }


            $propertyData = fillOnUndefined($params, 'property_info');

            if($propertyData){
                $updateData = [];
                $validateKeys = ['official_name', 'tax_office', 'tax_number'];
                foreach ($propertyData as $key => $value) {

                    if (!in_array($key, $validateKeys)) {
                        throw new ApiErrorException(lang('Error Columns'));
                    }
                    $updateData[$key] = $value;
                }

                if ($updateData) {
                    $updateData['updated_by'] = $requestParams['user_id'];
                    $updateData['updated_at'] = time();
                }
                $propertyUpdateResult = $this->propertyService->update($params['property_id'], $updateData);

                if ($propertyUpdateResult['status'] != 'success') {
                    throw  new ApiErrorException($propertyUpdateResult['message']);
                }

            }



            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => $propertyContact['data']];

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }
        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

}
