<?php

namespace App\Services;

use App\Models\DocumentType;
use App\Models\ServiceAuthority;
use App\Models\ServiceCategory;
use App\Models\Tenant;

class UaeReferenceDataService
{
    public function ensureForTenant(Tenant $tenant): void
    {
        foreach ([
            ['IMMIGRATION', 'Immigration & Residency', 'الهجرة والإقامة'],
            ['IDENTITY', 'Emirates ID & Identity', 'الهوية الإماراتية'],
            ['LABOUR', 'Labour & Employment', 'العمل والتوظيف'],
            ['VISAS', 'Entry Permits & Visit Visas', 'أذونات الدخول وتأشيرات الزيارة'],
            ['BUSINESS', 'Business Licensing', 'ترخيص الأعمال'],
            ['MUNICIPAL', 'Municipality & Local Services', 'البلدية والخدمات المحلية'],
            ['PRO', 'PRO & Document Clearing', 'العلاقات الحكومية وتخليص المعاملات'],
        ] as [$code, $nameEn, $nameAr]) {
            ServiceCategory::updateOrCreate(
                ['tenant_id' => $tenant->id, 'code' => $code],
                ['name_en' => $nameEn, 'name_ar' => $nameAr, 'is_active' => true]
            );
        }

        foreach ([
            ['ICP', 'Federal Authority for Identity, Citizenship, Customs & Port Security'],
            ['GDRFA', 'General Directorate of Residency and Foreigners Affairs'],
            ['MOHRE', 'Ministry of Human Resources and Emiratisation'],
            ['DET', 'Department of Economy and Tourism'],
            ['DHA', 'Dubai Health Authority'],
            ['MUNICIPALITY', 'Municipality'],
            ['COURTS', 'Courts'],
            ['POLICE', 'Police'],
        ] as [$code, $name]) {
            ServiceAuthority::updateOrCreate(
                ['tenant_id' => $tenant->id, 'code' => $code],
                ['name' => $name]
            );
        }

        foreach ([
            ['PASSPORT', 'Passport', 'customer', true],
            ['EMIRATES_ID', 'Emirates ID', 'customer', true],
            ['RESIDENCE_VISA', 'Residence Visa', 'customer', true],
            ['PERSONAL_PHOTO', 'Personal Photograph', 'customer', false],
            ['ENTRY_PERMIT', 'Entry Permit', 'customer', true],
            ['EMPLOYMENT_CONTRACT', 'Employment Contract', 'employee', true],
            ['WORK_PERMIT', 'Work Permit / Labour Card', 'employee', true],
            ['TRADE_LICENSE', 'Trade License', 'company', true],
            ['ESTABLISHMENT_CARD', 'Establishment Card', 'company', true],
            ['TENANCY_CONTRACT', 'Tenancy Contract / Ejari', 'universal', true],
            ['INSURANCE', 'Health Insurance Certificate', 'universal', true],
            ['MEDICAL_FITNESS', 'Medical Fitness Certificate', 'customer', true],
        ] as [$code, $name, $entity, $expires]) {
            DocumentType::updateOrCreate(
                ['tenant_id' => $tenant->id, 'code' => $code],
                [
                    'name_en' => $name, 'entity_type' => $entity, 'has_document_number' => $code !== 'PERSONAL_PHOTO',
                    'has_issue_date' => $code !== 'PERSONAL_PHOTO', 'has_expiry_date' => $expires,
                    'expiry_reminder_supported' => $expires, 'is_active' => true,
                ]
            );
        }
    }
}
