<?php

namespace Tests\Feature\Api\V1\Workflows;

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

/**
 * Scenario J: Cross-module API smoke checks.
 *
 * These are intentionally use-case oriented and broad. They prove the major
 * modules can be reached with realistic authentication and seeded school context.
 * They do not pretend to replace each module's dedicated CRUD and edge tests.
 */
class ScenarioJCrossModuleApiUseCaseSmokeTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;

    public function test_admin_can_reach_core_operational_dashboards_without_server_errors(): void
    {
        $world = $this->seedTeacherClassWithStudent();
        $admin = $this->actingAsApiAdministrator();
        $this->actingAs($admin, 'api');

        $invoiceId = $this->seedInvoiceForParent($world['parent_id']);

        $endpoints = [
            '/api/v1/administrator/dashboard/metrics',
            '/api/v1/administrator/dashboard/user-search?query=Kai',
            '/api/v1/users',
            '/api/v1/students',
            '/api/v1/students/' . $world['student_id'],
            '/api/v1/students/' . $world['student_id'] . '/classes',
            '/api/v1/class-sections',
            '/api/v1/teachers/classes?class_section_id=' . $world['class_section_id'],
            '/api/v1/attendance/admin/daily?date=2025-10-05',
            '/api/v1/attendance-comment-templates',
            '/api/v1/finance/invoices/parent/' . $world['parent_id'],
            '/api/v1/finance/invoices/' . $invoiceId . '/preview',
            '/api/v1/finance/paypal-transactions',
            '/api/v1/inventory/items',
            '/api/v1/inventory/categories',
            '/api/v1/messages',
            '/api/v1/support',
            '/api/v1/settings',
            '/api/v1/system/semester-range/resolve?date=2025-10-05',
        ];

        foreach ($endpoints as $endpoint) {
            $response = $this->getJson($endpoint);

            $this->assertLessThan(
                500,
                $response->getStatusCode(),
                "$endpoint returned a server error for an authenticated admin."
            );
        }
    }

    public function test_major_mutating_use_cases_validate_bad_payloads_instead_of_crashing(): void
    {
        $world = $this->seedTeacherClassWithStudent();
        $admin = $this->actingAsApiAdministrator();
        $this->actingAs($admin, 'api');

        $adminMutations = [
            ['POST', '/api/v1/users', []],
            ['POST', '/api/v1/students', []],
            ['POST', '/api/v1/class-sections', []],
            ['POST', '/api/v1/attendance-comment-templates', []],
            ['POST', '/api/v1/assignments', []],
            ['POST', '/api/v1/messages', []],
            ['POST', '/api/v1/support', []],
            ['POST', '/api/v1/inventory/items', []],
            ['POST', '/api/v1/settings/events', []],
            ['POST', '/api/v1/finance/payments', []],
            ['POST', '/api/v1/finance/refunds', []],
            ['POST', '/api/v1/finance/fees/tuition-breakdown', []],
            ['POST', '/api/v1/extra-charges', []],
            ['POST', '/api/v1/reports/slips/preview', []],
        ];

        foreach ($adminMutations as [$method, $endpoint, $payload]) {
            $response = $this->json($method, $endpoint, $payload);

            $this->assertLessThan(
                500,
                $response->getStatusCode(),
                "$method $endpoint crashed instead of validating/rejecting the request."
            );
        }

        $this->actingAs($world['teacher'], 'api');
        foreach ([
            ['POST', '/api/v1/attendance/teacher/submit', []],
            ['POST', '/api/v1/teacher/absence-vacation', []],
            ['POST', '/api/v1/teacher/class-progress-submit', []],
            ['POST', '/api/v1/scores/homework', []],
        ] as [$method, $endpoint, $payload]) {
            $response = $this->json($method, $endpoint, $payload);

            $this->assertLessThan(
                500,
                $response->getStatusCode(),
                "$method $endpoint crashed for an authenticated teacher."
            );
        }

        $this->actingAs($world['parent'], 'api');
        foreach ([
            ['PATCH', '/api/v1/parents/profile', []],
            ['POST', '/api/v1/parents/emergency-contacts', []],
            ['POST', '/api/v1/parents/attendance-reports', []],
            ['POST', '/api/v1/parents/events/participation', []],
        ] as [$method, $endpoint, $payload]) {
            $response = $this->json($method, $endpoint, $payload);

            $this->assertLessThan(
                500,
                $response->getStatusCode(),
                "$method $endpoint crashed for an authenticated parent."
            );
        }
    }
}
