<?php

namespace App\Core\Service;


use App\Core\Repository\Product\PropertyProductMappingRepository;


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

class PropertyProductMappingService
{

    private $propertyProductMappingRepository;

    public function __construct(
        PropertyProductMappingRepository $propertyProductMappingRepository
    )
    {
        $this->propertyProductMappingRepository = $propertyProductMappingRepository;
    }

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

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

        try {

            $data = $this->propertyProductMappingRepository->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 create($params = [])
    {
        $response = ['status' => -1, 'message' => '', 'data' => null];
        try {


            $insertData = [];
            foreach ($params['products'] as $product){
                $insertData[] = [
                    'property_id' => fillOnUndefined($params, 'property_id'),
                    'product_id' => fillOnUndefined($product, 'product_id'),
                    'expiration_date' => fillOnUndefined($product, 'expiration_date'),
                    'amount' => fillOnUndefined($product, 'amount'),
                    'currency' => fillOnUndefined($product, 'currency'),
                    'status' => fillOnUndefined($product, 'status', 1),
                    'created_by' => fillOnUndefined($product, 'user_id', 1),
                    'updated_by' => fillOnUndefined($product, 'user_id', 1),
                    'created_at' => Carbon::now()->timestamp,
                    'updated_at' => Carbon::now()->timestamp,
                ];
            }

            $insertResult = $this->propertyProductMappingRepository->insert($insertData);
            if ($insertResult['status'] != 'success') {
                throw new Exception('api-unknown_error');
            }

            $userData = $insertResult["data"];
            $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 update($id, $param = [])
    {

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

            $updateResult = $this->propertyProductMappingRepository->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);
    }

}
