<?php

namespace App\Core\Service;

use App\Core\Repository\VWDestination\VWDestinationRepository;
use App\Core\Validator\Destination\DestinationSearchValidator;

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

class DestinationService
{

   private $VWDestinationRepository;
   private $destinationSearchValidator;


    public function __construct(
        DestinationSearchValidator $destinationSearchValidator,
        VWDestinationRepository $VWDestinationRepository

    )
    {

        $this->VWDestinationRepository = $VWDestinationRepository;
        $this->destinationSearchValidator = $destinationSearchValidator ;

    }

    public function select($param = [], $column = ['*'])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];

        try {

            $data = $this->VWDestinationRepository->findByCriteria($param, $column);

            $response['status'] = 1;
            $response['data'] = $data;


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

        return output($response);
    }

    public function searchDestination($params){

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            $validationResult = $this->destinationSearchValidator->validate($params);

            if ($validationResult->errors()->first()) {
                $errors = $validationResult->errors()->all();
                throw new ApiErrorException($errors);
            }


            $searchCriteria = [
              'criteria' => [
                  ['field' => 'long_name', 'condition' => 'like', 'value' => '%'.$params['search_term'].'%'],
                  ['field' => 'status', 'condition' => '=', 'value' => 1],
              ],
                'orderBy' => [
                    ["field" => "priority", "value" => "DESC"],
                    ["field" => "level", "value" => "ASC"],
                    ["field" => "name", "value" => "ASC"]
                ],
                'take' => 5
            ];

            $searchResult = $this->select($searchCriteria, ['id', 'long_name']);

            if(!$searchResult['data']){
                throw new ApiErrorException(lang('Search data not found'));
            }

            $response = ['statusCode' => 200, 'status' => true, 'message' => '', 'data' => ['destination' =>$searchResult['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 output($response);
    }




}