<?php

namespace App\Console\Commands\Jobs;

use App\Exceptions\ApiErrorException;
use App\Models\ChannelManagerPropertyMapping;
use App\Models\CurrencyRates;
use App\Models\Property;
use App\Models\PropertyPriceComparison;
use App\Models\PropertyRoomRatePrice;
use App\Models\vwActiveProperty;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Exception;

class PropertySummaryService extends Command
{
    protected $signature = 'cron:property-summary-service';

    protected $description = '';

    private $propertySummaryService;

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {

        $propertySummaryService = App::make('App\Core\Service\PropertySummaryService');

        //BULK
        /*$firstAllOfYear = '2022-01-01';
        $lastAllOfYear = '2027-01-01';

        $startDayOfYear = Carbon::parse($firstAllOfYear)->firstOfMonth()->toDateString();
        $lastDayOfYear = Carbon::parse($lastAllOfYear)->firstOfMonth()->toDateString();

        $diffInPeriod = Carbon::parse($startDayOfYear)->diffInMonths(Carbon::parse($lastDayOfYear));

        $periodList = [];
        for ($i = 0; $i < $diffInPeriod; $i++) {
            $periodList[] = Carbon::parse($firstAllOfYear)->addMonths($i)->format('Y-m');
        }*/
        //$periodList[] = '2025-09';
        //BULK

        //$invoiceWithPeriod = $propertyInvoiceService->invoiceWithPeriod(623, '2025-10');
        //dd($invoiceWithPeriod);
        //$periodList = ['2025-09'];

        //$lastOfMonth = '2025-10-30';
        //$dayOfMonth = '2025-10-30';

        //$lastOfMonth = Carbon::now()->lastOfMonth()->format('Y-m-d');
        //$dayOfMonth = Carbon::now()->format('Y-m-d');

        //if ($lastOfMonth != $dayOfMonth) {
        //$this->alert(date('Y-m-d H:i:s') . ' : ' . $lastOfMonth . ' - ' . $dayOfMonth);
        //return false;
        //}


        //$period = Carbon::parse($dayOfMonth)->lastOfMonth()->format('Y-m');
        //$periodList = [$period];

        $year = Carbon::now()->year;
        $currentMonth = Carbon::now()->month;
        $periodList = [];
        for ($month = $currentMonth; $month <= 12; $month++) {
            $periodList[] = Carbon::create($year, $month, 1)->format('Y-m');
        }

        //$periodList = ['2025-03'];

        $this->info(date('Y-m-d H:i:s') . ' START');

        $propertyList = Property::where('commission', '>', 1)
            ->where('status', 1)
            //->whereIn('id', [71])
            ->get()->toArray();

        $types[1] = 'Checkout';
        $types[2] = 'Transaction';
        foreach ($periodList as $period) {

            $this->alert('PERIOD: ' . $period);

            foreach ($propertyList as $property) {

                try {

                    foreach ($types as $typeCode => $type) {

                        $invoiceWithPeriod = $propertySummaryService->summaryWithPeriod($property['id'], $period, $typeCode);

                        if ($invoiceWithPeriod['status'] == 'success') {
                            $this->info(date('Y-m-d H:i:s') . ': ' . $period . ' ' . $property['id'] . ' - ' . $property['name'] . ' : ' . $type . ' : ' . $period . ' - ' . $invoiceWithPeriod['data']['total'] . ' ' . $invoiceWithPeriod['data']['currency']);
                        } else {
                            $this->error(date('Y-m-d H:i:s') . ': ' . $period . ' ' . $property['id'] . ' - ' . $invoiceWithPeriod['message']);
                        }

                    }


                } catch (ApiErrorException | Exception $e) {
                    $this->error(date('Y-m-d H:i:s') . ' ERROR: L: ' . $e->getLine() . ' - ' . $e->getMessage() . ' - ' . $property['id'] . ' - ' . $property['name']);
                }

            }
        }


        $this->info(date('Y-m-d H:i:s') . ' FINISHED');


    }
}
