<?php

namespace Tests\Feature\Api\V1\Workflows;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\AssertsE2EApiResponses;
use Tests\Concerns\SeedsE2ETestFixtures;
use Tests\TestCase;

/**
 * Scenario P: Settings, configuration, calendar, and notification governance.
 *
 * This verifies that operational settings can be reviewed, changed, and audited
 * through the API instead of being tribal knowledge in someone's browser tab.
 */
class ScenarioPSettingsConfigurationAndCalendarGovernanceTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;
    use AssertsE2EApiResponses;

    public function test_admin_manages_calendar_event_configuration_and_notification_lifecycle(): void
    {
        $this->seedE2EConfiguration();
        $admin = $this->actingAsApiAdministrator();
        $this->actingAs($admin, 'api');

        $this->getJson('/api/v1/settings')
            ->assertOk();

        $settingsUpdate = $this->patchJson('/api/v1/settings', [
            'timezone' => 'America/New_York',
            'school_name' => 'Al-Rahma Test School',
            'default_semester' => self::E2E_SEMESTER,
            'default_school_year' => self::E2E_SCHOOL_YEAR,
        ]);
        $this->assertNoServerError($settingsUpdate, 'Settings update');

        $this->getJson('/api/v1/settings/school-calendar/options')
            ->assertOk()
            ->assertJsonPath('status', true);

        $event = $this->postJson('/api/v1/settings/school-calendar/events', [
            'title' => 'E2E Parent Orientation',
            'date' => '2025-10-12',
            'description' => 'Orientation for new families.',
            'audience' => 'parents',
            'semester' => self::E2E_SEMESTER,
            'school_year' => self::E2E_SCHOOL_YEAR,
        ]);
        $event->assertCreated()->assertJsonPath('status', true);

        $eventId = (int) DB::table('calendar_events')
            ->where('title', 'E2E Parent Orientation')
            ->value('id');
        $this->assertGreaterThan(0, $eventId);

        $this->getJson('/api/v1/settings/school-calendar/events?school_year=' . self::E2E_SCHOOL_YEAR . '&include_meetings=1')
            ->assertOk()
            ->assertJsonPath('status', true);

        $this->patchJson('/api/v1/settings/school-calendar/events/' . $eventId, [
            'title' => 'E2E Parent Orientation Updated',
            'date' => '2025-10-19',
            'description' => 'Updated orientation for new families.',
            'audience' => 'parents',
            'semester' => self::E2E_SEMESTER,
            'school_year' => self::E2E_SCHOOL_YEAR,
        ])->assertOk()->assertJsonPath('status', true);

        $notification = $this->postJson('/api/v1/notifications', [
            'title' => 'Orientation Reminder',
            'message' => 'Parent orientation moved to October 19.',
            'audience' => 'parents',
            'type' => 'announcement',
            'starts_at' => '2025-10-10 09:00:00',
            'ends_at' => '2025-10-20 09:00:00',
            'is_active' => true,
        ]);
        $this->assertSuccessfulOrValidation($notification, 'Notification creation');

        $notificationId = (int) DB::table('notifications')
            ->where('title', 'Orientation Reminder')
            ->value('id');

        if ($notificationId > 0) {
            $this->getJson('/api/v1/notifications/active')
                ->assertOk();

            $this->postJson('/api/v1/notifications/' . $notificationId . '/read')
                ->assertSuccessful();

            $this->deleteJson('/api/v1/notifications/' . $notificationId)
                ->assertSuccessful();

            $this->postJson('/api/v1/notifications/' . $notificationId . '/restore')
                ->assertSuccessful();
        }

        $this->deleteJson('/api/v1/settings/school-calendar/events/' . $eventId)
            ->assertOk()
            ->assertJsonPath('status', true);
    }

    public function test_configuration_admin_crud_keeps_school_year_context_explicit(): void
    {
        $this->seedE2EConfiguration();
        $admin = $this->actingAsApiAdministrator();
        $this->actingAs($admin, 'api');

        $this->getJson('/api/v1/configuration-admin')
            ->assertOk();

        $create = $this->postJson('/api/v1/configuration-admin', [
            'config_key' => 'e2e_guardrail_message',
            'config_value' => 'E2E guardrail enabled',
            'description' => 'Created by explicit workflow coverage.',
        ]);
        $this->assertSuccessfulOrValidation($create, 'Configuration creation');

        $configId = (int) DB::table('configuration')
            ->where('config_key', 'e2e_guardrail_message')
            ->value('id');

        $this->getJson('/api/v1/configuration-admin/key/e2e_guardrail_message')
            ->assertSuccessful();

        if ($configId > 0) {
            $this->patchJson('/api/v1/configuration-admin/' . $configId, [
                'config_value' => 'E2E guardrail updated',
                'description' => 'Updated by explicit workflow coverage.',
            ])->assertSuccessful();

            $this->deleteJson('/api/v1/configuration-admin/' . $configId)
                ->assertSuccessful();
        }
    }
}
