<?php

namespace Tests\Unit;

use App\Models\Company;
use App\Models\Document;
use App\Models\User;
use App\Services\Documents\EmployeeDocumentComplianceService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class EmployeeDocumentComplianceServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_uploaded_by_only_document_counts_toward_compliance(): void
    {
        $company = Company::factory()->create();
        $employee = User::factory()->create([
            'company_id' => $company->id,
            'status' => 'active',
        ]);

        Document::query()->create([
            'company_id' => $company->id,
            'name' => 'My Aadhaar',
            'type' => 'pdf',
            'category' => 'aadhaar_card',
            'path' => 'documents/aadhaar.pdf',
            'uploaded_by' => $employee->id,
            'visibility' => 'private',
            'status' => 'active',
        ]);

        $service = app(EmployeeDocumentComplianceService::class);
        $compliance = $service->buildCompliance(
            $service->documentsForUser((int) $company->id, (int) $employee->id)
        );

        $this->assertContains('aadhaar_card', $compliance['completed_keys']);
        $this->assertLessThan(6, count($compliance['missing_keys']));
    }

    public function test_gaps_for_users_excludes_fully_compliant_employee(): void
    {
        $company = Company::factory()->create();
        $employee = User::factory()->create([
            'company_id' => $company->id,
            'status' => 'active',
            'name' => 'Fully Compliant',
        ]);

        $service = app(EmployeeDocumentComplianceService::class);
        foreach (array_keys($service->mandatoryCategories()) as $categoryKey) {
            Document::query()->create([
                'company_id' => $company->id,
                'name' => ucfirst(str_replace('_', ' ', $categoryKey)),
                'type' => $categoryKey,
                'category' => $categoryKey,
                'path' => 'documents/'.$categoryKey.'.pdf',
                'documentable_type' => User::class,
                'documentable_id' => $employee->id,
                'uploaded_by' => $employee->id,
                'visibility' => 'private',
                'status' => 'active',
            ]);
        }

        $gaps = $service->gapsForUsers((int) $company->id, [(int) $employee->id]);

        $this->assertTrue($gaps->isEmpty());
    }

    public function test_legacy_name_based_document_assigns_to_employee(): void
    {
        $company = Company::factory()->create();
        $employee = User::factory()->create([
            'company_id' => $company->id,
            'status' => 'active',
            'name' => 'Priya Verma',
        ]);

        Document::query()->create([
            'company_id' => $company->id,
            'name' => 'PAN Card - Priya Verma',
            'type' => 'pan_card',
            'category' => 'pan_card',
            'path' => 'documents/pan.pdf',
            'uploaded_by' => 999,
            'visibility' => 'company',
            'status' => 'active',
        ]);

        $service = app(EmployeeDocumentComplianceService::class);
        $compliance = $service->buildCompliance(
            $service->documentsForUser((int) $company->id, (int) $employee->id)
        );

        $this->assertContains('pan_card', $compliance['completed_keys']);
    }
}
