<?php

namespace App\Core\Service;

use App\Core\Repository\OfferCancellationPolicy\OfferCancellationPolicyRepository;
use App\Core\Validator\OfferCancellationPolicy\OfferCancellationPolicyCreateValidator;

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

class OfferCancellationPolicyService
{

    private $offerCancellationPolicyRepository;
    private $offerCancellationPolicyCreateValidator;


    public function __construct(

        OfferCancellationPolicyRepository $offerCancellationPolicyRepository,
        OfferCancellationPolicyCreateValidator $offerCancellationPolicyCreateValidator
    )
    {
        $this->offerCancellationPolicyRepository = $offerCancellationPolicyRepository;
        $this->offerCancellationPolicyCreateValidator = $offerCancellationPolicyCreateValidator;
    }

    public function insertCancellationPolicy($param = [], $oldOfferCancellationPolicy)
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {


            $validationResult = $this->offerCancellationPolicyCreateValidator->validate($param);
            if ($validationResult->errors()->first()) {
                $errors = $validationResult->errors()->all();
                throw new ApiErrorException($errors);
            }
            $cancellationPolicies = $param['cancellation_policy'];
            $cancellationPolicyData = [];
            $userData = [] ;

            if($param['has_cancellation_policy'] == true){
                foreach ($cancellationPolicies as $cancellationPolicy){

                    $cancellationPolicyData[] = [

                        "offer_id" => $param['offer_id'],
                        "days_before" => fillOnUndefined($cancellationPolicy, "days_before"),
                        "type" => fillOnUndefined($cancellationPolicy, "type"),
                        "value" => fillOnUndefined($cancellationPolicy, "value"),
                        "status" => fillOnUndefined($cancellationPolicy, "status",1),
                        "created_by" => fillOnUndefined($param, "user_id"),
                        "updated_by" => fillOnUndefined($param, "user_id"),
                        "created_at" => time(),
                        "updated_at" => time()
                    ];
                }

                $userCreateResult = $this->offerCancellationPolicyRepository->createAll($cancellationPolicyData);
                if ($userCreateResult['status'] != 'success') {
                    throw new Exception('api-unknown_error');
                }
                $userData = $userCreateResult["data"];

            }


            if($oldOfferCancellationPolicy){
                $deleteThisArray = $this->offerCancellationPolicyRepository->destroy($oldOfferCancellationPolicy);
                if ($deleteThisArray['status'] != 'success') {
                    throw new Exception('api-unknown_error');
                }
            }

            $response = [
                'status' => true,
                'data' => $userData,
            ];

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

        return output($response);
    }

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

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

        try {

            $data = $this->offerCancellationPolicyRepository->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 update($id, $param = [])
    {

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

            $updateResult = $this->offerCancellationPolicyRepository->update($id, $param);
            if ($updateResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }
            $updateData = $updateResult["data"];
            $response = [
                'status' => true,
                'data' => $updateData,
            ];

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

        return output($response);
    }

    public function updateOrCreate($criteria = [], $saveData = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];

        try {

            $data = $this->offerCancellationPolicyRepository->updateOrCreate($criteria, $saveData);
            if ($data['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }
            $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);


    }


}
