<?php

namespace Tests\Unit\Services\Incidents;

use App\Services\Incidents\IncidentAnalysisService;
use App\Services\Incidents\IncidentLookupService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class IncidentAnalysisServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_analyze_groups_incidents_by_student(): void
    {
        DB::table('classSection')->insert([
            'id' => 1,
            'class_section_id' => 1,
            'class_section_name' => '1A',
            'class_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('incident')->insert([
            [
                'id' => 1,
                'student_id' => 10,
                'student_name' => 'Kid Tester',
                'grade' => '1',
                'incident' => 'behavior',
                'incident_datetime' => '2025-01-02 10:00:00',
                'incident_state' => 'Closed',
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'id' => 2,
                'student_id' => 10,
                'student_name' => 'Kid Tester',
                'grade' => '1',
                'incident' => 'behavior',
                'incident_datetime' => '2025-01-01 10:00:00',
                'incident_state' => 'Canceled',
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
        ]);

        $service = new IncidentAnalysisService(new IncidentLookupService());
        $result = $service->analyze('2025-2026', 'Fall');

        $this->assertCount(1, $result);
        $this->assertSame('Kid Tester', $result[0]['student_name']);
        $this->assertSame('1A', $result[0]['grade']);
        $this->assertSame(2, $result[0]['total']);
        $this->assertSame(1, $result[0]['closed']);
        $this->assertSame(1, $result[0]['canceled']);
        $this->assertSame('2025-01-02 10:00:00', $result[0]['last_incident']);
    }
}
