<?php

namespace Database\Seeders;

use App\Models\DocumentType;
use App\Models\User;
use Illuminate\Database\Seeder;

class DocumentTypeSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $companies = User::where('type', 'company')->get();

        if ($companies->isEmpty()) {
            $this->command?->warn('No company users found. Document types are created per company.');

            return;
        }

        $documentTypes = [
            ['name' => 'Identity Proof', 'is_required' => true, 'description' => 'Government-issued ID: national ID, passport, or driver licence.'],
            ['name' => 'Address Proof', 'is_required' => true, 'description' => 'Proof of residence (utility bill, tenancy agreement, or bank statement).'],
            ['name' => 'Educational Certificates', 'is_required' => true, 'description' => 'Degrees, diplomas, transcripts, and professional qualifications.'],
            ['name' => 'Experience Letters', 'is_required' => false, 'description' => 'Reference or service letters from previous employers.'],
            ['name' => 'Medical Certificate', 'is_required' => false, 'description' => 'Fitness-to-work or occupational health clearance where applicable.'],
            ['name' => 'Bank Account Details', 'is_required' => true, 'description' => 'Bank confirmation letter or cancelled cheque for payroll.'],
            ['name' => 'Tax / TPIN Documentation', 'is_required' => false, 'description' => 'Taxpayer identification and withholding-related documents (e.g. Malawi TPIN).'],
            ['name' => 'Emergency Contact Form', 'is_required' => true, 'description' => 'Signed emergency contact names, relationships, and phone numbers.'],
            ['name' => 'Passport Size Photo', 'is_required' => true, 'description' => 'Recent photograph for ID badge and personnel records.'],
            ['name' => 'Offer Letter', 'is_required' => false, 'description' => 'Signed job offer and agreed terms of employment.'],
            ['name' => 'Employment Contract', 'is_required' => true, 'description' => 'Signed contract of employment or engagement letter.'],
            ['name' => 'Non-Disclosure Agreement', 'is_required' => false, 'description' => 'Confidentiality or data-protection acknowledgment where required.'],
            ['name' => 'Police Clearance / Background Check', 'is_required' => false, 'description' => 'Criminal record or safeguarding clearance if policy requires it.'],
            ['name' => 'Professional Licence / Registration', 'is_required' => false, 'description' => 'Membership or licence for regulated professions (accounting, nursing, etc.).'],
            ['name' => 'Pension / Retirement Scheme Enrollment', 'is_required' => false, 'description' => 'Pension or statutory contribution enrollment forms (e.g. scheme registration).'],
            ['name' => 'Medical Aid / Insurance Enrollment', 'is_required' => false, 'description' => 'Health insurance or medical scheme membership documents.'],
            ['name' => 'Compliance & Policy Acknowledgment', 'is_required' => true, 'description' => 'Signed acknowledgment of code of conduct, anti-harassment, and safety policies.'],
            ['name' => 'Induction / Training Completion', 'is_required' => false, 'description' => 'Certificates or logs for mandatory onboarding and compliance training.'],
            ['name' => 'Leave & Absence Forms', 'is_required' => false, 'description' => 'Stored leave applications or medical certificates for extended absence.'],
            ['name' => 'Performance Review Documents', 'is_required' => false, 'description' => 'Appraisal forms, goal agreements, and improvement plans.'],
            ['name' => 'Disciplinary / Grievance Records', 'is_required' => false, 'description' => 'Formal warnings, hearings, or grievance outcomes (restricted access).'],
            ['name' => 'Termination / Clearance', 'is_required' => false, 'description' => 'Exit interview, clearance checklist, and final settlement documents.'],
        ];

        foreach ($companies as $company) {
            foreach ($documentTypes as $documentType) {
                if (DocumentType::where('name', $documentType['name'])->where('created_by', $company->id)->exists()) {
                    continue;
                }

                try {
                    DocumentType::create([
                        'name' => $documentType['name'],
                        'is_required' => $documentType['is_required'],
                        'description' => $documentType['description'],
                        'created_by' => $company->id,
                    ]);
                } catch (\Exception $e) {
                    $this->command?->error('Failed to create document type: '.$documentType['name'].' for company: '.$company->name);

                    continue;
                }
            }
        }

        $this->command?->info('DocumentType seeder completed successfully!');
    }
}
