<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyType\PropertyTypeRepository;


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

class PropertyTypeService
{

   private $propertyTypeRepository;



    public function __construct(
        PropertyTypeRepository $propertyTypeRepository

    )
    {

        $this->propertyTypeRepository = $propertyTypeRepository;

    }

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

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

        try {

            $data = $this->propertyTypeRepository->findByCriteria($param, $column);
            $response = [
                'status' => true,
                'data' => $data,
            ];

        } catch (ApiErrorException $e) {
            $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 getPropertyTypes($params){

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

            $return = [];
            $searchCriteria = [
              'criteria' => [
                  ['field' => 'status', 'condition' => '=', 'value' => 1]
              ],
              'orderBy' => [
                    ["field" => "name", "value" => "ASC"]
                ],
            ];

            $searchResult = $this->select($searchCriteria, ['id', 'name', 'language_key']);
            if($searchResult['status'] != 'success'){
                throw new Exception($searchResult['message']);
            }

            $return = $searchResult['data'];

            $response = ['statusCode' => 200, 'status' => true, 'message' => '', 'data' => ['property_type' =>$return ] ];
        } 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);
    }




}
