<?php

namespace Tests\Unit;

use App\Models\AttendanceDay;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AttendanceDayTest extends TestCase
{
    use RefreshDatabase;

    public function test_get_or_create_draft_creates_once_and_reuses(): void
    {
        $a = AttendanceDay::getOrCreateDraft(10, '2026-02-23', 'Fall', '2025-2026', 5);
        $b = AttendanceDay::getOrCreateDraft(10, '2026-02-23', 'Fall', '2025-2026', 7);

        $this->assertSame($a->id, $b->id);
        $this->assertSame('draft', $a->status);

        $this->assertDatabaseCount('attendance_day', 1);
        $this->assertDatabaseHas('attendance_day', [
            'id' => $a->id,
            'class_section_id' => 10,
            'date' => '2026-02-23 00:00:00',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'draft',
        ]);
    }

    public function test_find_by_section_date_returns_model_or_null(): void
    {
        $found = AttendanceDay::findBySectionDate(1, '2026-02-23', 'Fall', '2025-2026');
        $this->assertNull($found);

        $row = AttendanceDay::getOrCreateDraft(1, '2026-02-23', 'Fall', '2025-2026');
        $found2 = AttendanceDay::findBySectionDate(1, '2026-02-23', 'Fall', '2025-2026');

        $this->assertNotNull($found2);
        $this->assertSame($row->id, $found2->id);
    }

    public function test_submit_sets_submitted_fields(): void
    {
        $row = AttendanceDay::getOrCreateDraft(2, '2026-02-23', 'Fall', '2025-2026');

        $ok = $row->submit(99, '2026-03-01 10:00:00');
        $this->assertTrue($ok);

        $row->refresh();
        $this->assertSame('submitted', $row->status);
        $this->assertSame(99, $row->submitted_by);
        $this->assertNotNull($row->submitted_at);
        $this->assertNotNull($row->auto_publish_at);
    }

    public function test_finalize_is_alias_to_submit(): void
    {
        $row = AttendanceDay::getOrCreateDraft(3, '2026-02-23', 'Fall', '2025-2026');

        $row->finalize(101);
        $row->refresh();

        $this->assertSame('submitted', $row->status);
        $this->assertSame(101, $row->submitted_by);
        $this->assertNotNull($row->submitted_at);
    }

    public function test_publish_sets_published_fields(): void
    {
        $row = AttendanceDay::getOrCreateDraft(4, '2026-02-23', 'Fall', '2025-2026');

        $row->publish(55);
        $row->refresh();

        $this->assertSame('published', $row->status);
        $this->assertSame(55, $row->published_by);
        $this->assertNotNull($row->published_at);
    }

    public function test_reopen_sets_reopen_fields_and_status(): void
    {
        $row = AttendanceDay::getOrCreateDraft(5, '2026-02-23', 'Fall', '2025-2026');
        $row->publish(1);

        $row->reopen(2, 'draft', 'Fix absences');
        $row->refresh();

        $this->assertSame('draft', $row->status);
        $this->assertSame(2, $row->reopened_by);
        $this->assertNotNull($row->reopened_at);
        $this->assertSame('Fix absences', $row->reopen_reason);
    }

    public function test_reopen_rejects_invalid_status(): void
    {
        $this->expectException(\InvalidArgumentException::class);

        $row = AttendanceDay::getOrCreateDraft(6, '2026-02-23', 'Fall', '2025-2026');
        $row->reopen(2, 'published'); // invalid
    }

    public function test_is_finalized_true_for_published_or_legacy_finalized(): void
    {
        $row = AttendanceDay::getOrCreateDraft(7, '2026-02-23', 'Fall', '2025-2026');
        $row->publish(1);

        $this->assertTrue(AttendanceDay::isFinalized(7, '2026-02-23', 'Fall', '2025-2026'));
        $this->assertFalse(AttendanceDay::isFinalized(7, '2026-02-24', 'Fall', '2025-2026'));

        // legacy "finalized"
        AttendanceDay::query()->create([
            'class_section_id' => 8,
            'date' => '2026-02-23',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'finalized',
            'created_at' => now('UTC'),
            'updated_at' => now('UTC'),
        ]);

        $this->assertTrue(AttendanceDay::isFinalized(8, '2026-02-23', 'Fall', '2025-2026'));
    }
}
