<?php

namespace Tests\Unit;

use App\Models\Project;
use App\Services\AllocationValidationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AllocationValidationServiceTest extends TestCase
{
    use RefreshDatabase;

    protected AllocationValidationService $service;

    protected function setUp(): void
    {
        parent::setUp();
        $this->service = new AllocationValidationService;
    }

    // 5.1.18 Unit test: Allocation validation service
    public function test_validate_capacity_returns_zero_utilization_for_missing_team_member()
    {
        $result = $this->service->validateCapacity(
            'non-existent-id',
            '2026-02',
            40
        );

        $this->assertTrue($result['valid']);
        $this->assertNull($result['warning']);
        $this->assertEquals(0, $result['utilization']);
    }

    public function test_validate_approved_estimate_returns_green_when_at_100_percent()
    {
        $project = Project::factory()->create([
            'approved_estimate' => 100,
        ]);

        $result = $this->service->validateApprovedEstimate(
            $project->id,
            '2026-02',
            100
        );

        $this->assertTrue($result['valid']);
        $this->assertEquals('green', $result['indicator']);
    }

    public function test_validate_approved_estimate_returns_yellow_when_under()
    {
        $project = Project::factory()->create([
            'approved_estimate' => 100,
        ]);

        $result = $this->service->validateApprovedEstimate(
            $project->id,
            '2026-02',
            60
        );

        $this->assertTrue($result['valid']);
        $this->assertEquals('yellow', $result['indicator']);
        $this->assertStringContainsString('under by', $result['message']);
    }

    public function test_validate_approved_estimate_returns_red_when_over()
    {
        $project = Project::factory()->create([
            'approved_estimate' => 100,
        ]);

        $result = $this->service->validateApprovedEstimate(
            $project->id,
            '2026-02',
            120
        );

        $this->assertTrue($result['valid']);
        $this->assertEquals('red', $result['indicator']);
        $this->assertStringContainsString('over', $result['message']);
    }

    public function test_validate_approved_estimate_returns_gray_when_no_estimate()
    {
        $project = Project::factory()->create([
            'approved_estimate' => null,
        ]);

        $result = $this->service->validateApprovedEstimate(
            $project->id,
            '2026-02',
            40
        );

        $this->assertTrue($result['valid']);
        $this->assertEquals('gray', $result['indicator']);
    }

    public function test_validate_approved_estimate_returns_gray_when_estimate_is_zero()
    {
        $project = Project::factory()->create([
            'approved_estimate' => 0,
        ]);

        $result = $this->service->validateApprovedEstimate(
            $project->id,
            '2026-02',
            40
        );

        $this->assertTrue($result['valid']);
        $this->assertEquals('gray', $result['indicator']);
    }
}
