<?php

namespace Tests\Unit\Services\Grading;

use App\Services\Grading\GradingLockService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class GradingLockServiceTest extends TestCase
{
    use RefreshDatabase;

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

        $service = new GradingLockService();
        $locked = $service->toggle(1, 'Fall', '2025-2026', 99);

        $this->assertTrue($locked);
        $this->assertDatabaseHas('grading_locks', [
            'class_section_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'is_locked' => 1,
        ]);
    }

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

        $service = new GradingLockService();
        $count = $service->lockAll('Fall', '2025-2026', 99);

        $this->assertSame(2, $count);
        $this->assertDatabaseHas('grading_locks', [
            'class_section_id' => 2,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'is_locked' => 1,
        ]);
    }
}
