<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Core\Service\PropertyFactService;
use Illuminate\Http\Request;

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

class PropertyFactController
{


    private $request;
    private $propertyFactService;


    public function __construct(
        Request $request,
        PropertyFactService $propertyFactService
    )
    {
        $this->request = $request;
        $this->propertyFactService = $propertyFactService;

    }

    public function getPropertyFact(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;

            $requestFact = [
              'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),
            ];

            $getPropertyFact = $this->propertyFactService->getPropertyFact($requestFact);



            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => ['get_facts' => $getPropertyFact['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']);


    }

    public function searchPropertyFact(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;

            $requestFact = [
                'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'search_term' => fillOnUndefined($params, 'search_term'),
            ];

            $getPropertyFact = $this->propertyFactService->searchPropertyFact($requestFact);

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

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

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' => ['get_facts' => $getPropertyFact['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']);


    }

    public function getSubCategoryFacts(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;

            $requestFact = [
                'locale' => fillOnUndefined($params, 'locale'),
                'property_id' => fillOnUndefined($params, 'property_id'),
                'sub_category_id' => fillOnUndefined($params, 'sub_category_id'),
            ];

            $factRequestData = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['sub_category_id']],
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                    ['field' => 'type', 'condition' => '=', 'value' => 0],
                ],
                'with' => ['propertyFactParent'],
                'firstRow' => 1
            ];
            $thisFact = $this->propertyFactService->select($factRequestData, ['id', 'parent_id', 'name', 'language_key', 'title_language_key']);
            if($thisFact['status'] != 'success' || !$thisFact['data']){
                throw new ApiErrorException('Fact data not found') ;
            }


            $subCategoryFacts = $this->propertyFactService->getSubCategoryFacts($requestFact);
            if($subCategoryFacts['status'] != 'success'){
                throw new ApiErrorException($subCategoryFacts['message']) ;
            }

            $requestFact['parent_id'] = $thisFact['data']['parent_id'] ;

            $thisMenus = $this->propertyFactService->getSubCategoryMenus($requestFact);
            if($thisMenus['status'] != 'success'){
                throw new ApiErrorException($thisMenus['message']) ;
            }
            $thisSubCategory = $thisFact['data'] ;
            $thisMainCategory = $thisSubCategory['property_fact_parent'] ;
            unset($thisSubCategory['property_fact_parent']) ;

            $response = ['status' => 1, 'statusCode' => 200, 'message' => null, 'data' =>
                [
                    'get_facts' => $subCategoryFacts['data'],
                    'get_menus' => $thisMenus['data'],
                    'this_sub_category' => $thisSubCategory,
                    'this_main_category' => $thisMainCategory,
                ]

            ];

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


    }

}
