<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyWebPhotoMapping\PropertyWebPhotoMappingRepository;

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

class PropertyWebPhotoMappingService
{
    private $propertyWebPhotoMappingRepository;

    public function __construct(
        PropertyWebPhotoMappingRepository $propertyWebPhotoMappingRepository
    )
    {
        $this->propertyWebPhotoMappingRepository = $propertyWebPhotoMappingRepository;
    }

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

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

        try {

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

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

        try {
            $deleteCriteria = [
                'criteria' => [
                    ['field' => 'property_id', 'condition' => '=', 'value' => $params['property_id']],
                    ['field' => 'property_photo_id', 'condition' => '=', 'value' => $params['photo_id']],
                ]
            ];

            $deleteData = $this->propertyWebPhotoMappingRepository->findByCriteria($deleteCriteria, ['id']);
            $deleteData = $deleteData ? $deleteData : [] ;
            if($deleteData){
                $deleteIds = array_column($deleteData, 'id') ;
                $destroyResult = $this->propertyWebPhotoMappingRepository->destroy($deleteIds);
                if ($destroyResult['status'] != 'success') {
                    throw new Exception('api-unknown_error');
                }
            }

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

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


    }

}
