<?php

namespace Tests\Feature;

use App\Models\Branch;
use App\Models\Employee;
use App\Models\Service;
use App\Models\Tenant;
use App\Services\BranchManagementService;
use App\Services\SequenceAndBillingService;
use App\Services\TenantOnboardingService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class TenantOnboardingAndSettingsTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed();
    }

    public function test_onboarding_progress_calculation_and_go_live_completion(): void
    {
        $tenant = Tenant::first();
        $onboardingService = app(TenantOnboardingService::class);

        $progress = $onboardingService->getProgress($tenant);
        $this->assertGreaterThan(0, $progress['percentage']);

        $completed = $onboardingService->completeOnboarding($tenant);
        $this->assertEquals('completed', $completed->status);
        $this->assertEquals(100, $completed->progress_percentage);
    }

    public function test_branch_creation_obeys_subscription_plan_limits(): void
    {
        $tenant = Tenant::first();
        $branchService = app(BranchManagementService::class);

        // Current count is 3 (Dubai Main, Al Qusais, Deira). Starter plan max is 1, Professional max is 5.
        $tenant->subscription->plan->update(['max_branches' => 3]);

        $this->expectException(\Exception::class);
        $this->expectExceptionMessageMatches('/maximum of 3 branches/');

        $branchService->createBranch($tenant, [
            'name' => 'Excess Branch 4',
            'emirate' => 'Dubai',
        ]);
    }

    public function test_main_branch_cannot_be_deleted(): void
    {
        $tenant = Tenant::first();
        $mainBranch = Branch::where('tenant_id', $tenant->id)->where('is_main', true)->first();
        $branchService = app(BranchManagementService::class);

        $this->expectException(\Exception::class);
        $this->expectExceptionMessageMatches('/Main Branch cannot be deleted/');

        $branchService->deleteBranch($mainBranch);
    }

    public function test_employee_creation_and_sequence_generator(): void
    {
        $tenant = Tenant::first();
        $branch = Branch::where('tenant_id', $tenant->id)->first();
        $sequenceService = app(SequenceAndBillingService::class);

        $code = $sequenceService->generateReference($tenant, 'employee');
        $this->assertStringStartsWith('EMP-', $code);

        $emp = Employee::create([
            'tenant_id' => $tenant->id,
            'branch_id' => $branch->id,
            'employee_code' => $code,
            'first_name' => 'Saeed',
            'full_name' => 'Saeed Al Nuaimi',
            'email' => 'saeed@almadina.ae',
            'designation' => 'PRO Representative',
            'status' => 'active',
        ]);

        $this->assertNotNull($emp);
        $this->assertEquals('Saeed Al Nuaimi', $emp->full_name);
    }

    public function test_service_master_creation_with_vat_computation(): void
    {
        $tenant = Tenant::first();

        $govFee = 250.00;
        $typingFee = 80.00;
        $serviceCharge = 20.00;
        $vat = round(($typingFee + $serviceCharge) * 0.05, 2); // 5.00
        $total = $govFee + $typingFee + $serviceCharge + $vat; // 355.00

        $service = Service::create([
            'tenant_id' => $tenant->id,
            'service_code' => 'EID-NEW-01',
            'name_en' => 'Emirates ID New Application',
            'gov_fee' => $govFee,
            'typing_fee' => $typingFee,
            'service_charge' => $serviceCharge,
            'vat_applicable' => true,
            'vat_amount' => $vat,
            'total_price' => $total,
            'status' => 'active',
        ]);

        $this->assertEquals(355.00, $service->total_price);
        $this->assertEquals(5.00, $service->vat_amount);
    }
}
