<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

/**
 * Administrator full-surface operational contract checks.
 */
class ApiAdminOperationsFullSurfaceContractTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;
    use AssertsE2EApiResponses;

    public function test_administrator_operational_surface_responds_with_controlled_contracts(): void
    {
        $this->seedE2EConfiguration();
        $world = $this->seedTeacherClassWithStudent(9201);
        $admin = $this->actingAsApiAdministrator();

        foreach ($this->adminGetEndpoints($world) as $uri) {
            $response = $this->getJson($uri);
            $this->assertNoServerError($response, 'Admin GET ' . $uri);
            $this->assertStatusIn($response, [200, 204, 302, 400, 401, 403, 404, 405, 409, 419, 422], 'Admin GET ' . $uri);
        }

        foreach ($this->adminMutationEndpoints($world, $admin->id) as [$method, $uri, $payload]) {
            $response = $this->json($method, $uri, $payload);
            $this->assertNoServerError($response, 'Admin ' . $method . ' ' . $uri);
            $this->assertStatusIn($response, [200, 201, 202, 204, 302, 400, 401, 403, 404, 405, 409, 419, 422], 'Admin ' . $method . ' ' . $uri);
        }
    }

    /**
     * @param array<string, mixed> $world
     * @return list<string>
     */
    private function adminGetEndpoints(array $world): array
    {
        $studentId = (int) $world['student_id'];
        $classSectionId = (int) $world['class_section_id'];

        return [
            '/api/v1/administrator/attendance-templates',
            '/api/v1/administrator/absence',
            '/api/v1/administrator/dashboard/metrics',
            '/api/v1/administrator/dashboard/user-search?query=Fixture',
            '/api/v1/administrator/teacher-submissions',
            '/api/v1/administrator/trophy',
            '/api/v1/administrator/trophy/winners',
            '/api/v1/administrator/trophy/final',
            '/api/v1/administrator/teacher-class/assignments',
            '/api/v1/administrator/notifications/alerts',
            '/api/v1/administrator/notifications/print-recipients',
            '/api/v1/administrator/enrollment-withdrawal',
            '/api/v1/administrator/enrollment-withdrawal/new-students',
            '/api/v1/administrator/enroll-withdrawal',
            '/api/v1/administrator/enroll-withdrawal/new-students',
            '/api/v1/administrator/promotions',
            '/api/v1/administrator/promotions/summary',
            '/api/v1/administrator/promotions/levels',
            '/api/v1/administrator/promotions/1',
            '/api/v1/administrator/promotions/1/reminders',
            '/api/v1/administrator/promotions/1/audit',
            '/api/v1/administrator/emergency-contacts',
            '/api/v1/administrator/emergency-contacts/data',
            '/api/v1/administrator/emergency-contacts/1',
            '/api/v1/administrator/staff',
            '/api/v1/administrator/staff/1',
            '/api/v1/administrator/flags/pending',
            '/api/v1/administrator/flags/form-meta',
            '/api/v1/administrator/flags/students/1',
            '/api/v1/administrator/flags/processed',
            '/api/v1/administrator/flags/analysis',
            '/api/v1/administrator/grading/homework-tracking?class_section_id=' . $classSectionId,
            '/api/v1/administrator/expenses',
            '/api/v1/administrator/expenses/options',
            '/api/v1/administrator/expenses/1',
            '/api/v1/administrator/reports/financial-detailed',
            '/api/v1/administrator/reports/stakeholder-analysis',
            '/api/v1/administrator/reports/parent-payment-followups',
            '/api/v1/administrator/invoices/management',
            '/api/v1/administrator/paypal-transactions',
            '/api/v1/administrator/reimbursements/under-processing',
            '/api/v1/administrator/reimbursements/export',
            '/api/v1/administrator/reimbursements/reimbursed-expenses',
            '/api/v1/administrator/reimbursements',
            '/api/v1/administrator/printables/badges/form-options',
            '/api/v1/administrator/printables/report-card/meta',
            '/api/v1/administrator/certificates/dashboard',
            '/api/v1/administrator/certificates/form-options',
            '/api/v1/administrator/certificates/audit-log',
            '/api/v1/administrator/certificates/reprint/CERT-E2E-001',
            '/api/v1/administrator/role-permission/roles',
            '/api/v1/administrator/role-permission/roles/1',
            '/api/v1/administrator/role-permission/users',
            '/api/v1/administrator/role-permission/permissions',
            '/api/v1/administrator/role-permission/permissions/1',
            '/api/v1/administrator/role-permission/roles/1/permissions',
            '/api/v1/school-years',
            '/api/v1/school-years/current',
            '/api/v1/school-years/options',
            '/api/v1/school-years/1/summary',
            '/api/v1/school-years/1/parent-balances',
            '/api/v1/school-years/1/promotion-preview',
            '/api/v1/class-sections',
            '/api/v1/classes',
            '/api/v1/users',
            '/api/v1/users/' . $world['parent_id'],
            '/api/v1/students/' . $studentId,
        ];
    }

    /**
     * @param array<string, mixed> $world
     * @return list<array{0: string, 1: string, 2: array<string, mixed>}>
     */
    private function adminMutationEndpoints(array $world, int $adminId): array
    {
        $studentId = (int) $world['student_id'];
        $teacherId = (int) $world['teacher']->id;
        $classSectionId = (int) $world['class_section_id'];

        return [
            ['POST', '/api/v1/administrator/absence', ['user_id' => $adminId, 'date' => '2025-10-05', 'reason' => 'Contract probe']],
            ['POST', '/api/v1/administrator/teacher-submissions/notify', ['teacher_id' => $teacherId, 'message' => 'Contract probe']],
            ['POST', '/api/v1/administrator/teacher-class/assign', ['teacher_id' => $teacherId, 'class_section_id' => $classSectionId, 'position' => 'main']],
            ['POST', '/api/v1/administrator/teacher-class/delete', ['teacher_id' => $teacherId, 'class_section_id' => $classSectionId]],
            ['POST', '/api/v1/administrator/notifications/alerts', ['alerts' => ['contract_probe' => true]]],
            ['POST', '/api/v1/administrator/notifications/print-recipients', ['recipients' => [$adminId]]],
            ['POST', '/api/v1/administrator/enrollment-withdrawal/update-statuses', ['enrollment_status' => [$studentId => 'enrolled']]],
            ['POST', '/api/v1/administrator/promotions/evaluate', ['student_ids' => [$studentId]]],
            ['POST', '/api/v1/administrator/promotions/reminders/dispatch', ['dry_run' => true]],
            ['POST', '/api/v1/administrator/promotions/deadlines/expire', ['dry_run' => true]],
            ['POST', '/api/v1/administrator/promotions/deadlines', ['deadline' => '2025-12-01']],
            ['POST', '/api/v1/administrator/promotions/placement/preview', ['student_ids' => [$studentId]]],
            ['POST', '/api/v1/administrator/promotions/placement/batches/1/finalize', ['confirm' => true]],
            ['POST', '/api/v1/administrator/promotions/levels', ['from_level' => '1', 'to_level' => '2']],
            ['POST', '/api/v1/administrator/promotions/levels/seed', ['confirm' => true]],
            ['PATCH', '/api/v1/administrator/promotions/1/status', ['status' => 'approved']],
            ['PATCH', '/api/v1/administrator/promotions/1/enrollment-steps', ['steps' => ['confirmed' => true]]],
            ['PATCH', '/api/v1/administrator/emergency-contacts/1', ['name' => 'Contract Contact', 'phone' => '5551234567']],
            ['DELETE', '/api/v1/administrator/emergency-contacts/1', []],
            ['POST', '/api/v1/administrator/staff', ['firstname' => 'Staff', 'lastname' => 'Probe', 'email' => 'staff.probe@example.test']],
            ['PATCH', '/api/v1/administrator/staff/1', ['firstname' => 'Staff Updated']],
            ['DELETE', '/api/v1/administrator/staff/1', []],
            ['POST', '/api/v1/administrator/flags', ['student_id' => $studentId, 'reason' => 'Contract probe']],
            ['POST', '/api/v1/administrator/flags/1/state', ['state' => 'reviewing']],
            ['POST', '/api/v1/administrator/flags/1/close', ['resolution' => 'closed']],
            ['POST', '/api/v1/administrator/flags/1/cancel', ['reason' => 'cancelled']],
            ['POST', '/api/v1/school-years', ['name' => '2026-2027', 'start_date' => '2026-09-01', 'end_date' => '2027-06-30']],
            ['PATCH', '/api/v1/school-years/1', ['status' => 'active']],
            ['POST', '/api/v1/school-years/1/validate-close', ['confirm' => true]],
            ['POST', '/api/v1/school-years/1/preview-close', ['confirm' => true]],
            ['POST', '/api/v1/school-years/1/close', ['confirm' => true]],
            ['POST', '/api/v1/school-years/1/reopen', ['confirm' => true]],
        ];
    }
}
