<?php

namespace Tests\Unit;

use App\Models\Allocation;
use App\Models\Project;
use App\Models\Role;
use App\Models\TeamMember;
use App\Services\AllocationMatrixService;
use App\Services\VarianceCalculator;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AllocationCacheInvalidationTest extends TestCase
{
    use RefreshDatabase;

    // 5.1.19 Unit test: Cache invalidation on mutation
    public function test_matrix_service_returns_structure()
    {
        $role = Role::factory()->create();
        $teamMember = TeamMember::factory()->create(['role_id' => $role->id]);
        $project = Project::factory()->create();

        Allocation::factory()->create([
            'project_id' => $project->id,
            'team_member_id' => $teamMember->id,
            'month' => '2026-02-01',
            'allocated_hours' => 40,
        ]);

        $matrixService = new AllocationMatrixService(new VarianceCalculator);
        $result = $matrixService->getMatrix('2026-02');

        $this->assertArrayHasKey('allocations', $result);
        $this->assertArrayHasKey('projectTotals', $result);
        $this->assertArrayHasKey('teamMemberTotals', $result);
        $this->assertArrayHasKey('grandTotal', $result);
    }
}
