<?php

namespace App\Console\Commands\PropertyTrialCheck;

use App\Core\Service\PropertyProductMappingService;
use App\Core\Service\PropertyService;
use App\Exceptions\ApiErrorException;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class PropertyTrialCheck extends Command
{
    protected $signature = 'cron:property-trial-check';

    protected $description = '';

    private $propertyService;

    public function __construct(
        PropertyService $propertyService,
        PropertyProductMappingService $productMappingService
    )
    {
        parent::__construct();

        $this->propertyService = $propertyService;
        $this->productMappingService = $productMappingService;
    }

    public function handle()
    {

        $response = ['status' => false, 'message' => ''];

        $propertySelectCriteria = [
            'criteria' => [
                //['field' => 'id', 'condition' => '=', 'value' => 1],
                ['field' => 'status', 'condition' => '=', 'value' => 1],
                ['field' => 'commission', 'condition' => '=', 'value' => null],
                ['field' => 'created_at', 'condition' => '>', 'value' => Carbon::parse('2021-10-01')->timestamp],

            ],
            'with' => ['propertyProductMapping'],
            'orderBy' => [
                ["field" => "id", "value" => "ASC"]
            ],
            'take' => 100
        ];

        $propertyData = $this->propertyService->select($propertySelectCriteria);

        if ($propertyData['status'] == 'success') {

            foreach ($propertyData['data'] as $property) {


                if (empty($property['property_product_mapping'])) {
                    continue;
                }

                $expireDay = Carbon::now()->diff(Carbon::createFromTimestamp($property['created_at']))->days;

                $this->info(date('Y-m-d H:i:s') . ' Property: ' . $property['id'] . ' - ' . $property['name'] . ' - ' . $expireDay. ' Day');

                if (in_array($expireDay, [30])) {

                    DB::beginTransaction();

                    try {

                        $this->info(date('Y-m-d H:i:s') . ' : Property: ' . $property['id'] . ' - ' . $property['name']);

                        //Product remove from property
                        foreach ($property['property_product_mapping'] as $productMapping) {
                            $this->productMappingService->update($productMapping['id'], ['status' => 0]);
                        }

                        //Trial period status
                        $propertyData = $this->propertyService->update($property['id'], ['status' => 2]);

                        //Burada otel user larına mail atılabilir, trial süresi biti diye

                        $response['status'] = true;


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

                    if ($response['status']) {
                        DB::commit();
                    } else {
                        DB::rollBack();
                    }

                }


                //Expiration Check
                //Product remove from property via expiration date
                foreach ($property['property_product_mapping'] as $productMapping) {

                    if($productMapping['status'] == 1 && !is_null($productMapping['expiration_date'])) {
                        if(Carbon::now()->greaterThanOrEqualTo(Carbon::parse($productMapping['expiration_date']))) {
                            $this->productMappingService->update($productMapping['id'], ['status' => 3]);}

                    }

                }



            }


        }

    }
}
