<?php

namespace App\Core\Service;

use App\Core\Repository\GeneralTimezone\GeneralTimezoneRepository;

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

use Carbon\Carbon;
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Str;



class GeneralTimezoneService
{

    private $generalTimezoneRepository;

    public function __construct(

        GeneralTimezoneRepository $generalTimezoneRepository
    )
    {

        $this->generalTimezoneRepository = $generalTimezoneRepository;

    }



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

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

        try {

            $data = $this->generalTimezoneRepository->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 getAllaGeneralTimezone($params){

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

            $generalTimezoneRequest = [
                'criteria' => [
                    ['field' => 'status', 'condition' => '=', 'value' => 1],
                ]

            ];

            $generalTimezoneData = $this->select($generalTimezoneRequest);
            if($generalTimezoneData['status'] != "success"){
                throw new Exception('api-unknown_error');
            }

            $generalTimezones = collect($generalTimezoneData['data'])->map(function ($timezone){
                return [
                    "id" => $timezone["id"],
                    "location" => $timezone["location"],
                    "description" => $timezone["description"],
                    "action_type" => $timezone["action_type"],
                    "hour" => $timezone["hour"],
                    "minute" => $timezone["minute"],

                ];

            });

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

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




}
