<?php

namespace Database\Seeders;

use App\Models\Company;
use Illuminate\Database\Seeder;

class QuotaSeeder extends Seeder
{
    /**
     * Run the database seeds.
     * 
     * Mengisi kuota untuk koreksi absen, izin pulang cepat, dan izin terlambat
     * untuk semua perusahaan yang ada di database.
     */
    public function run(): void
    {
        $companies = Company::all();

        if ($companies->isEmpty()) {
            $this->command->warn('Tidak ada perusahaan di database. Jalankan DatabaseSeeder terlebih dahulu.');
            return;
        }

        $this->command->info("Mengisi kuota untuk {$companies->count()} perusahaan...");

        $updated = 0;
        $skipped = 0;

        foreach ($companies as $company) {
            // Skip jika sudah ada kuota yang diisi (tidak null)
            $hasQuota = !is_null($company->correction_quota_max) 
                     || !is_null($company->early_leave_quota_max) 
                     || !is_null($company->late_arrival_quota_max);

            if ($hasQuota) {
                $this->command->info("  ⏭️  Skip: {$company->name} (sudah memiliki kuota)");
                $skipped++;
                continue;
            }

            // Default quota values
            // NULL = unlimited, atau set angka untuk membatasi
            $quotaSettings = [
                // Koreksi Absen: 3 kali per bulan (default)
                'correction_quota_max' => 3,
                'correction_quota_period' => 'monthly',

                // Izin Pulang Cepat: 5 kali per bulan (default)
                'early_leave_quota_max' => 5,
                'early_leave_quota_period' => 'monthly',

                // Izin Terlambat: 5 kali per bulan (default)
                'late_arrival_quota_max' => 5,
                'late_arrival_quota_period' => 'monthly',
            ];

            // Custom quota untuk perusahaan tertentu (optional)
            // Contoh: untuk perusahaan dengan nama tertentu, set kuota berbeda
            if (str_contains(strtolower($company->name), 'bpkp')) {
                // BPKP mungkin lebih ketat
                $quotaSettings = [
                    'correction_quota_max' => 2,
                    'correction_quota_period' => 'monthly',
                    'early_leave_quota_max' => 3,
                    'early_leave_quota_period' => 'monthly',
                    'late_arrival_quota_max' => 3,
                    'late_arrival_quota_period' => 'monthly',
                ];
            } elseif (str_contains(strtolower($company->name), 'contoh')) {
                // Untuk perusahaan contoh, set unlimited (NULL)
                $quotaSettings = [
                    'correction_quota_max' => null,
                    'correction_quota_period' => 'monthly',
                    'early_leave_quota_max' => null,
                    'early_leave_quota_period' => 'monthly',
                    'late_arrival_quota_max' => null,
                    'late_arrival_quota_period' => 'monthly',
                ];
            }

            $company->update($quotaSettings);

            $correctionMax = $quotaSettings['correction_quota_max'] ?? 'Unlimited';
            $earlyLeaveMax = $quotaSettings['early_leave_quota_max'] ?? 'Unlimited';
            $lateArrivalMax = $quotaSettings['late_arrival_quota_max'] ?? 'Unlimited';

            $this->command->info("  ✅ Updated: {$company->name}");
            $this->command->line("     - Koreksi: {$correctionMax} / {$quotaSettings['correction_quota_period']}");
            $this->command->line("     - Pulang Cepat: {$earlyLeaveMax} / {$quotaSettings['early_leave_quota_period']}");
            $this->command->line("     - Terlambat: {$lateArrivalMax} / {$quotaSettings['late_arrival_quota_period']}");

            $updated++;
        }

        $this->command->newLine();
        $this->command->info("✅ Selesai!");
        $this->command->info("   Updated: {$updated} perusahaan");
        $this->command->info("   Skipped: {$skipped} perusahaan");
    }
}
