<?php

namespace Database\Seeders;

use App\Domain\Documents\Enums\DocumentCategory;
use App\Domain\Documents\Models\DocumentType;
use App\Domain\Organization\Models\Company;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

/**
 * The §17A document catalogue as seed data. Agencies add their own types and
 * deactivate what they do not use.
 */
class DocumentTypeSeeder extends Seeder
{
    /**
     * [code, name, category, sensitive, needs number, needs issue, needs expiry]
     *
     * @var array<int, array{0: string, 1: string, 2: DocumentCategory, 3?: bool, 4?: bool, 5?: bool, 6?: bool}>
     */
    private function types(): array
    {
        $c = DocumentCategory::class;

        return [
            // Identity
            ['national_id', 'National ID (NID)', DocumentCategory::Identity, true, true],
            ['birth_certificate', 'Birth Registration Certificate', DocumentCategory::Identity, true, true],
            ['passport', 'Passport', DocumentCategory::Identity, true, true, true, true],
            ['old_passport', 'Old Passport', DocumentCategory::Identity, true, true],
            ['driving_license', 'Driving Licence', DocumentCategory::Identity, false, true, true, true],
            ['marriage_certificate', 'Marriage Certificate', DocumentCategory::Identity, true],

            // Photographs
            ['photo_white_bg', 'Passport Photo — White Background', DocumentCategory::Photo],
            ['photo_blue_bg', 'Passport Photo — Blue Background', DocumentCategory::Photo],
            ['photo_full_body', 'Full-Body Photograph', DocumentCategory::Photo],

            // Education
            ['ssc_certificate', 'SSC / Secondary Certificate', DocumentCategory::Education],
            ['hsc_certificate', 'HSC / Higher Secondary Certificate', DocumentCategory::Education],
            ['diploma', 'Diploma', DocumentCategory::Education],
            ['degree', 'Bachelor Degree', DocumentCategory::Education],
            ['transcript', 'Marksheet / Transcript', DocumentCategory::Education],

            // Experience
            ['experience_certificate', 'Experience Certificate', DocumentCategory::Experience],
            ['release_letter', 'Release Letter', DocumentCategory::Experience],
            ['previous_work_permit', 'Previous Work Permit', DocumentCategory::Experience],

            // Skills and trade
            ['trade_certificate', 'Trade / Skill Certificate', DocumentCategory::Skill],
            ['welding_certificate', 'Welding Certificate', DocumentCategory::Skill],
            ['electrical_certificate', 'Electrician Certificate', DocumentCategory::Skill],
            ['heavy_vehicle_license', 'Heavy Vehicle Licence', DocumentCategory::Skill, false, true, true, true],
            ['language_certificate', 'Language Certificate', DocumentCategory::Skill],

            // Medical
            ['medical_slip', 'Medical Appointment Slip', DocumentCategory::Medical, true],
            ['medical_report', 'Medical Report', DocumentCategory::Medical, true, false, true, true],
            ['fitness_certificate', 'Medical Fitness Certificate', DocumentCategory::Medical, true, false, true, true],
            ['vaccination_certificate', 'Vaccination Certificate', DocumentCategory::Medical, true],

            // Police and security
            ['police_clearance', 'Police Clearance Certificate', DocumentCategory::Police, true, true, true, true],
            ['character_certificate', 'Character Certificate', DocumentCategory::Police],

            // Visa and immigration
            ['visa_application', 'Visa Application Form', DocumentCategory::Visa],
            ['visa_copy', 'Visa Copy', DocumentCategory::Visa, true, true, true, true],
            ['work_permit', 'Work Permit', DocumentCategory::Visa, true, true, true, true],
            ['biometric_receipt', 'Biometric Receipt', DocumentCategory::Visa],
            ['embassy_receipt', 'Embassy Submission Receipt', DocumentCategory::Visa],

            // Travel
            ['air_ticket', 'Air Ticket / E-Ticket', DocumentCategory::Travel],
            ['boarding_pass', 'Boarding Pass', DocumentCategory::Travel],
            ['travel_insurance', 'Travel Insurance', DocumentCategory::Travel, false, true, true, true],

            // Contracts
            ['employment_contract', 'Employment Contract', DocumentCategory::Contract, true],
            ['offer_letter', 'Offer Letter', DocumentCategory::Contract],
            ['service_agreement', 'Agency Service Agreement', DocumentCategory::Contract, true],
            ['consent_form', 'Consent Form', DocumentCategory::Contract],

            // Financial
            ['payment_receipt', 'Payment Receipt', DocumentCategory::Financial, true],
            ['bank_deposit_slip', 'Bank Deposit Slip', DocumentCategory::Financial, true],

            // Employer-side
            ['demand_letter', 'Demand Letter', DocumentCategory::Employer, false, true, true],
            ['power_of_attorney', 'Power of Attorney', DocumentCategory::Employer, true, true, true, true],
            ['employer_trade_license', 'Employer Trade Licence', DocumentCategory::Employer, false, true, true, true],
            ['labor_approval', 'Labour Approval', DocumentCategory::Employer, false, true, true, true],
            ['job_description', 'Job Description', DocumentCategory::Employer],

            ['other', 'Other Document', DocumentCategory::Other],
        ];
    }

    public function run(): void
    {
        $company = Company::query()->orderBy('id')->first();

        if ($company === null) {
            $this->command?->warn('No company found; skipping document type seeding.');

            return;
        }

        DB::transaction(function () use ($company) {
            $order = 10;

            foreach ($this->types() as $definition) {
                [$code, $name, $category] = $definition;

                DocumentType::updateOrCreate(
                    ['company_id' => $company->id, 'code' => $code],
                    [
                        'name' => $name,
                        'category' => $category->value,
                        'is_sensitive' => $definition[3] ?? false,
                        'requires_number' => $definition[4] ?? false,
                        'requires_issue_date' => $definition[5] ?? false,
                        'requires_expiry_date' => $definition[6] ?? false,
                        'reminder_days' => ($definition[6] ?? false) ? [90, 60, 30, 15, 7] : null,
                        'max_file_size_kb' => config('mams.documents.max_file_size_kb'),
                        'is_active' => true,
                        'sort_order' => $order,
                    ],
                );

                $order += 10;
            }
        });
    }
}
