<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
use App\Models\AlertMailRule;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;

class Scheduler extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    // protected $signature = 'app:scheduler';

    protected $signature = 'custom:scheduler';

    /**
     * The console command description.
     *
     * @var string
     */
    // protected $description = 'Command description';

    protected $description = 'Manually trigger scheduler logic';

    /**
     * Execute the console command.
     */
    // public function handle()
    // {
    //     //
    // }

    public function handle()
    {

        $this->call('approval:trigger-mails');
        // $this->call('sftp:process-files');

        // --- Production Rules ---
        $productionRules = AlertMailRule::where('module', 'ProductionQuantities')
            ->where('rule_name', 'ProductionMail')
            ->select('plant', 'schedule_type')
            ->distinct()
            ->get();

        foreach ($productionRules as $rule) {
            switch ($rule->schedule_type) {
                case 'Live':
                    // Run every minute
                    \Artisan::call('send:production-report', [
                        'schedule_type' => $rule->schedule_type,
                        'plant' => $rule->plant,
                    ]);
                    break;
                case 'Hourly':
                    if (now()->minute == 0) {
                        \Artisan::call('send:production-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
                case 'Daily':
                    if (now()->format('H:i') == '07:59') {
                        \Artisan::call('send:production-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
            }
        }

        // --- Invoice Validation Rules --- (Invoice Mail)
        $invoiceRules = AlertMailRule::where('module', 'InvoiceValidation')
            ->where('rule_name', 'InvoiceMail')
            ->select('plant', 'schedule_type')
            ->distinct()
            ->get();

        foreach ($invoiceRules as $rule) {

            switch ($rule->schedule_type) {
                case 'Live':
                    // Run every minute
                    \Artisan::call('send:invoice-report', [
                        'schedule_type' => $rule->schedule_type,
                        'plant' => $rule->plant,
                    ]);
                    break;

                case 'Hourly':
                    if (now()->minute == 0) {
                        \Artisan::call('send:invoice-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
                case 'Daily':
                    if (now()->format('H:i') == '07:59') {
                        \Artisan::call('send:invoice-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
            }
        }

        //..SerialInvoice

        $invoiceSerialRules = AlertMailRule::where('module', 'InvoiceValidation')
            ->where('rule_name', 'SerialInvoiceMail')
            ->select('plant', 'schedule_type')
            ->distinct()
            ->get();

        foreach ($invoiceSerialRules as $rule) {

            switch ($rule->schedule_type) {
                case 'Live':
                    // Run every minute
                    \Artisan::call('send:invoice-report', [
                        'schedule_type' => $rule->schedule_type,
                        'plant' => $rule->plant,
                    ]);
                    break;

                case 'Hourly':
                    if (now()->minute == 0) {
                        \Artisan::call('send:invoice-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
                case 'Daily':
                    if (now()->format('H:i') == '07:59') {
                        \Artisan::call('send:invoice-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
            }
        }

        //..MaterialInvoice

        $invoiceMaterialRules = AlertMailRule::where('module', 'InvoiceValidation')
            ->where('rule_name', 'MaterialInvoiceMail')
            ->select('plant', 'schedule_type')
            ->distinct()
            ->get();

        foreach ($invoiceMaterialRules as $rule) {

            switch ($rule->schedule_type) {
                case 'Live':
                    // Run every minute
                    \Artisan::call('send:invoice-report', [
                        'schedule_type' => $rule->schedule_type,
                        'plant' => $rule->plant,
                    ]);
                    break;

                case 'Hourly':
                    if (now()->minute == 0) {
                        \Artisan::call('send:invoice-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
                case 'Daily':
                    if (now()->format('H:i') == '07:59') {
                        \Artisan::call('send:invoice-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
            }
        }

        // foreach ($invoiceRules as $rule) {

        //     switch ($rule->schedule_type) {
        //         case 'Live':
        //             try {
        //                 Artisan::call('send:invoice-report', [
        //                     'schedule_type' => $rule->schedule_type,
        //                     'plant' => $rule->plant,
        //                 ]);

        //                 Log::info('Invoice report sent | Plant: '.$rule->plant);
        //             } catch (\Throwable $e) {
        //                 Log::error("Invoice Live Failed ({$rule->plant}): ".$e->getMessage());
        //             }
        //             break;

        //         case 'Hourly':
        //             if (now()->minute == 0) {
        //                 Artisan::call('send:invoice-report', [
        //                     'schedule_type' => $rule->schedule_type,
        //                     'plant' => $rule->plant,
        //                 ]);
        //             }
        //             break;

        //         case 'Daily':
        //             if (now()->format('H:i') == '07:59') {
        //                 Artisan::call('send:invoice-report', [
        //                     'schedule_type' => $rule->schedule_type,
        //                     'plant' => $rule->plant,
        //                 ]);
        //             }
        //             break;
        //     }
        // }


        // --- Invoice Data Report Rules ---
        $invoiceDataRules = AlertMailRule::where('module', 'InvoiceDataReport')
            ->where('rule_name', 'InvoiceDataMail')
            ->select('plant', 'schedule_type')
            ->distinct()
            ->get();

        foreach ($invoiceDataRules as $rule) {

            switch ($rule->schedule_type) {
                case 'Live':
                    // Run every minute
                    \Artisan::call('send:invoice-data-report', [
                        'schedule_type' => $rule->schedule_type,
                        'plant' => $rule->plant,
                    ]);
                    break;
                case 'Hourly':
                    if (now()->minute == 0) {
                        \Artisan::call('send:invoice-data-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
                case 'Daily':
                    if (now()->format('H:i') == '11:30') {
                        \Artisan::call('send:invoice-data-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
            }
        }

        $invoiceTransitRules = AlertMailRule::where('module', 'InvoiceTransit')
            ->where('rule_name', 'InvoiceTransitMail')
            ->select('plant', 'schedule_type')
            ->distinct()
            ->get();

        foreach ($invoiceTransitRules as $rule) {

            switch ($rule->schedule_type) {
                case 'Live':
                    // Run every minute
                    \Artisan::call('send:invoice-transit-report', [
                        'schedule_type' => $rule->schedule_type,
                        'plant' => $rule->plant,
                    ]);
                    break;
                case 'Hourly':
                    if (now()->minute == 0) {
                        \Artisan::call('send:invoice-transit-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
                case 'Daily':
                    if (now()->format('H:i') == '11:00') {
                        try {
                            \Artisan::call('send:invoice-transit-report', [
                                'schedule_type' => $rule->schedule_type,
                                'plant' => $rule->plant,
                            ]);

                            Log::info('Invoice Transit executed', [
                                'plant' => $rule->plant,
                                'type' => $rule->schedule_type,
                            ]);
                        }
                        catch (\Throwable $e) {
                            Log::error('Invoice Transit FAILED', [
                                'plant' => $rule->plant,
                                'error' => $e->getMessage(),
                                'trace' => $e->getTraceAsString(),
                            ]);
                        }
                    }
                    break;
            }
        }

        $ImportTransitRules = AlertMailRule::where('module', 'ImportTransit')
            ->where('rule_name', 'ImportTransitMail')
            ->select('plant', 'schedule_type')
            ->distinct()
            ->get();

        foreach ($ImportTransitRules as $rule) {

            switch ($rule->schedule_type) {
                case 'Live':
                    // Run every minute
                    \Artisan::call('send-import-transit', [
                        'schedule_type' => $rule->schedule_type,
                        'plant' => $rule->plant,
                    ]);
                    break;
                case 'Hourly':
                    if (now()->minute == 0) {
                        \Artisan::call('send-import-transit', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
                case 'Daily':
                    if (now()->format('H:i') == '08:00') {
                        try {
                            \Artisan::call('send-import-transit', [
                                'schedule_type' => $rule->schedule_type,
                                'plant' => $rule->plant,
                            ]);

                            Log::info('Invoice Transit executed', [
                                'plant' => $rule->plant,
                                'type' => $rule->schedule_type,
                            ]);
                        }
                        catch (\Throwable $e) {
                            Log::error('Invoice Transit FAILED', [
                                'plant' => $rule->plant,
                                'error' => $e->getMessage(),
                                'trace' => $e->getTraceAsString(),
                            ]);
                        }
                    }
                    break;
            }
        }

        $vehicleReports = AlertMailRule::where('module', 'VehicleReport')
            ->where('rule_name', 'VehicleReportMail')
            ->select('plant', 'schedule_type')
            ->distinct()
            ->get();

        foreach ($vehicleReports as $rule) {
            switch ($rule->schedule_type) {
                case 'Live':
                    // Run every minute
                    \Artisan::call('send:vehicle-report', [
                        'schedule_type' => $rule->schedule_type,
                        'plant' => $rule->plant,
                    ]);
                    break;
                case 'Hourly':
                    if (now()->minute == 0) {
                        \Artisan::call('send:vehicle-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
                case 'Daily':
                    if (now()->format('H:i') == '07:59') {
                        \Artisan::call('send:vehicle-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
            }
        }
    }

    /**
     * Helper to call Artisan commands with parameters.
     */
    protected function callArtisanCommand($commandName, $rule)
    {
        \Artisan::call($commandName, [
            'schedule_type' => $rule->schedule_type,
            'plant' => $rule->plant,
        ]);

        $this->info("Executed {$commandName} for plant: {$rule->plant}");
        \Log::info("Executed {$commandName} for plant: {$rule->plant}");
    }
}
