<?php

use App\Services\ProductionBatchService;
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schedule;

Artisan::command('inspire', function () {
    $this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote');

// Mark expired production batches as waste - runs every 15 minutes
Artisan::command('batches:expire', function () {
    $service = app(ProductionBatchService::class);
    $count = $service->markExpiredBatches();
    $this->info("Marked {$count} batches as expired.");
})->purpose('Mark expired production batches and record remaining scoops as waste');

// Schedule the expiration check to run every 15 minutes
Schedule::command('batches:expire')
    ->everyFifteenMinutes()
    ->appendOutputTo(storage_path('logs/cron.log'));

// Daily waste summary report - runs at end of day (11:59 PM)
Artisan::command('batches:daily-summary {branch_id?}', function (?int $branchId = null) {
    $service = app(ProductionBatchService::class);
    $date = now()->toDateString();

    if ($branchId) {
        $summary = $service->getDailyScoopSummary($branchId, $date);
        $this->table(['Metric', 'Value'], collect($summary)->map(fn($v, $k) => [$k, $v])->toArray());
    } else {
        $this->info("Daily batch summary for {$date} - specify branch_id for detailed report");
    }
})->purpose('Generate daily scoop sales and waste summary');

Schedule::command('batches:daily-summary')
    ->dailyAt('23:59')
    ->appendOutputTo(storage_path('logs/cron.log'));
