<?php

namespace Tests\Unit\Services\Settings\SchoolCalendar;

use App\Services\Settings\SchoolCalendar\SchoolCalendarQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class SchoolCalendarQueryServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_events_filters_by_school_year(): void
    {
        DB::table('calendar_events')->insert([
            [
                'title' => 'Event A',
                'date' => '2026-01-01',
                'description' => '',
                'notify_parent' => 1,
                'notify_admin' => 0,
                'notify_teacher' => 0,
                'no_school' => 0,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
                'notification_sent' => 0,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'title' => 'Event B',
                'date' => '2026-01-02',
                'description' => '',
                'notify_parent' => 1,
                'notify_admin' => 0,
                'notify_teacher' => 0,
                'no_school' => 0,
                'semester' => 'Fall',
                'school_year' => '2026-2027',
                'notification_sent' => 0,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);

        $service = new SchoolCalendarQueryService();
        $events = $service->listEvents(['school_year' => '2025-2026']);

        $this->assertCount(1, $events);
        $this->assertSame('Event A', $events->first()->title);
    }

    public function test_filter_events_for_audience_limits_visibility(): void
    {
        DB::table('calendar_events')->insert([
            [
                'title' => 'Parent Event',
                'date' => '2026-01-01',
                'description' => '',
                'notify_parent' => 1,
                'notify_admin' => 0,
                'notify_teacher' => 0,
                'no_school' => 0,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
                'notification_sent' => 0,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'title' => 'Teacher Event',
                'date' => '2026-01-02',
                'description' => '',
                'notify_parent' => 0,
                'notify_admin' => 0,
                'notify_teacher' => 1,
                'no_school' => 0,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
                'notification_sent' => 0,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);

        $service = new SchoolCalendarQueryService();
        $events = $service->listEvents(['school_year' => '2025-2026']);
        $filtered = $service->filterEventsForAudience($events, 'parent');

        $this->assertCount(1, $filtered);
        $this->assertSame('Parent Event', $filtered->first()->title);
    }
}
