<?php

namespace Tests\Unit\Services\Settings\SchoolCalendar;

use App\Models\CalendarEvent;
use App\Services\Settings\SchoolCalendar\SchoolCalendarContextService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarMutationService;
use App\Services\Settings\SchoolCalendar\SchoolCalendarNotificationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mockery;
use Tests\TestCase;

class SchoolCalendarMutationServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_create_persists_event(): void
    {
        $context = new SchoolCalendarContextService();
        $notifier = Mockery::mock(SchoolCalendarNotificationService::class);
        $notifier->shouldReceive('notify')->andReturn([]);

        $service = new SchoolCalendarMutationService($context, $notifier);
        $result = $service->create([
            'title' => 'New Event',
            'date' => '2026-01-05',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ], []);

        $this->assertInstanceOf(CalendarEvent::class, $result['event']);
        $this->assertDatabaseHas('calendar_events', ['title' => 'New Event']);
    }

    public function test_update_persists_changes(): void
    {
        $event = CalendarEvent::query()->create([
            'title' => 'Old Event',
            'date' => '2026-01-01',
            'description' => '',
            'notify_parent' => 0,
            'notify_admin' => 0,
            'notify_teacher' => 0,
            'no_school' => 0,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'notification_sent' => 0,
        ]);

        $context = new SchoolCalendarContextService();
        $notifier = Mockery::mock(SchoolCalendarNotificationService::class);
        $notifier->shouldReceive('notify')->andReturn([]);

        $service = new SchoolCalendarMutationService($context, $notifier);
        $service->update($event, ['title' => 'Updated Event'], []);

        $this->assertDatabaseHas('calendar_events', [
            'id' => $event->id,
            'title' => 'Updated Event',
        ]);
    }
}
