<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
use App\Models\AlertMailRule;


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()
    // {
    //     // --- Production Rules ---
    //     $productionRules = AlertMailRule::where('module', 'ProductionQuantities')
    //         ->where('rule_name', 'ProductionMail')
    //         ->select('plant', 'schedule_type')
    //         ->distinct()
    //         ->get();

    //     // foreach ($productionRules as $rule) {
    //     //     if ($rule->schedule_type == 'Live') {
    //     //         $this->callArtisanCommand('send:production-report', $rule);
    //     //     }
    //     // }

    //     foreach ($productionRules as $rule) {
    //         $command = $schedule->command('send:production-report', [
    //             $rule->schedule_type,
    //             $rule->plant,
    //         ]);

    //         switch ($rule->schedule_type) {
    //             case 'Live':
    //                 $command->everyMinute();
    //                 break;
    //             case 'Hourly':
    //                 $command->hourly();
    //                 break;
    //             case 'Daily':
    //                 $command->dailyAt('07:59');
    //                 break;
    //         }
    //     }

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

    //     foreach ($invoiceRules as $rule) {
    //         if ($rule->schedule_type == 'Live') {
    //             $this->callArtisanCommand('send:invoice-report', $rule);
    //         }
    //     }

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

    //     foreach ($invoiceDataRules as $rule) {
    //         if ($rule->schedule_type == 'Live') {
    //             $this->callArtisanCommand('send:invoice-data-report', $rule);
    //         }
    //     }

    //     \Log::info('Custom scheduler executed at ' . now());
    //     $this->info('Custom scheduler executed successfully.');
    // }

    public function handle()
    {
        //$schedule = app(\Illuminate\Console\Scheduling\Schedule::class);

        // --- 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') == '15:59') {
                        \Artisan::call('send:production-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
            }
        }


        // --- Invoice Validation Rules ---
        $invoiceRules = AlertMailRule::where('module', 'InvoiceValidation')
            ->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') == '15:59') {
                        \Artisan::call('send:invoice-report', [
                            'schedule_type' => $rule->schedule_type,
                            'plant' => $rule->plant,
                        ]);
                    }
                    break;
            }
        }

        // --- Invoice Data Report Rules ---
        $invoiceDataRules = AlertMailRule::where('module', 'InvoiceDataReport')
            ->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') == '15:59') {
                        \Artisan::call('send:invoice-data-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}");
    }
}
