<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Core\Service\PropertyAdditionalInfo\PropertyAdditionalInfoService;
use Illuminate\Http\Request;

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

class PropertyAdditionalInfoController extends Controller
{


    private $request;
    private $propertyAdditionalInfoService;


    public function __construct
    (
        Request $request,
        PropertyAdditionalInfoService $propertyAdditionalInfoService
    )
    {
        $this->request = $request;
        $this->propertyAdditionalInfoService = $propertyAdditionalInfoService;
    }

    public function getPropertyAdditionalInfo()
    {
        $response = ['status' => false, 'statusCode' => 500, 'message' => '', 'data' => null];

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

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

            //TODO : Validataion eklenecek. Eklendikten sonra if  kaldırılması gerekiyor
            if (!isset($params['locale']) || empty($params['locale'])) {
                throw new ApiErrorException(lang('Location  is a required field'));
            }

            $getPropertyAdditionalInfo = $this->propertyAdditionalInfoService->getPropertyAdditionalInfo($params);
            $getPropertyAdditionalInfo = $getPropertyAdditionalInfo ? $getPropertyAdditionalInfo : null;

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

        } 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']);
    }

    public function insertPropertyAdditionalInfo()
    {
        $response = ['status' => false, 'statusCode' => 500, 'message' => '', 'data' => null];

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

            $params = $this->request->params;
            $params['user_id'] = $this->request->auth->id;


            //TODO : Validataion eklenecek ( benzersiz alanları unutma!!!). Eklendikten sonra if'in  kaldırılması gerekiyor.
            if (!isset($params['data'][0]['additional_info_key_id']) || empty($params['data'][0]['additional_info_key_id'])) {
                throw new ApiErrorException(lang('Location  is a required field'));
            }




        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . ' ' . $e->getLine() . ' ' . $e->getMessage();
            Log::error($message);
        }

        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }

    public function updatePropertyAdditionalInfo()
    {
        $response = ['status' => false, 'statusCode' => 500, 'message' => '', 'data' => null];

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

            $params = $this->request->params;
            $params['user_id'] = $this->request->auth->id;


            //TODO : Validataion eklenecek ( benzersiz alanları unutma!!!). Eklendikten sonra if'in  kaldırılması gerekiyor.
            if (!isset(current($params['data'])['additional_info_key_id']) || empty(current($params['data'])['additional_info_key_id'])) {
                throw new ApiErrorException(lang('Location  is a required field'));
            }

            $response = $this->propertyAdditionalInfoService->updatePropertyAdditionalInfo($params);


        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . ' ' . $e->getLine() . ' ' . $e->getMessage();
            Log::error($message);
        }

        return apiResponse($response['status'], $response['message'], $response['data'], $response['statusCode']);
    }


}
