<?php

namespace App\Core\Service;

use App;
use Illuminate\Support\Facades\Log;
use Exception;
use App\Exceptions\ApiErrorException;
use App\Core\Repository\PropertyCompetitorMapping\PropertyCompetitorMappingRepository;

class CompetitorPriceAnalysisService
{

    private $propertyCompetitorMappingRepository;

    public function __construct(
        PropertyCompetitorMappingRepository $propertyCompetitorMappingRepository
    )
    {
        $this->propertyCompetitorMappingRepository = $propertyCompetitorMappingRepository;
    }

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


            $insertData = [
                "property_id" => fillOnUndefined($param, "property_id"),
                "competitor_property_name" => fillOnUndefined($param, "competitor_property_name"),
                "competitor_property_source" => fillOnUndefined($param, "competitor_property_source", 'tripadvisor'),
                "competitor_property_key" => fillOnUndefined($param, "competitor_property_key"),
                "status" => fillOnUndefined($param, "status", 1),
                "created_by" => fillOnUndefined($param, "created_by"),
                "updated_by" => fillOnUndefined($param, "updated_by"),
                "created_at" => time(),
                "updated_at" => time(),
            ];

            $insertDataResult = $this->propertyCompetitorMappingRepository->create($insertData);
            if ($insertDataResult['status'] != 'success') {
                throw new Exception($insertDataResult['message']);
            }

            $insertDataResult = $insertDataResult["data"];

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

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

        return output($response);
    }

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

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

        try {

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

            $propertyList = [];
            foreach ($data as $property) {
                $propertyList[] = [
                    'id' => $property['id'],
                    'property_id' => $property['property_id'],
                    'name' => $property['competitor_property_name'],
                    'key' => $property['competitor_property_key'],
                    'competitor_property_source' => $property['competitor_property_source'],

                ];
            }

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

        } 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 deletePropertyCompetitorMapping($deleteThisId)
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {

            $deleteThisId = is_array($deleteThisId) ? $deleteThisId : [$deleteThisId];
            $deleteThisArray = $this->propertyCompetitorMappingRepository->destroy($deleteThisId);

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

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

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


}
