<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

/**
 * Finance, inventory, procurement, messaging, and support full-surface probes.
 */
class ApiFinanceInventoryCommunicationsFullSurfaceContractTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;
    use AssertsE2EApiResponses;

    public function test_back_office_and_communications_surfaces_have_controlled_contracts(): void
    {
        $this->seedE2EConfiguration();
        $world = $this->seedTeacherClassWithStudent(9401);
        $invoiceId = $this->seedInvoiceForParent((int) $world['parent_id']);
        $this->actingAsApiAdministrator();

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

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

        return [
            ['GET', '/api/v1/finance/financial-report', []],
            ['GET', '/api/v1/finance/financial-summary', []],
            ['GET', '/api/v1/finance/stakeholder-analysis', []],
            ['GET', '/api/v1/finance/parent-payment-followups', []],
            ['POST', '/api/v1/finance/parent-payment-followups/' . $parentId . '/note', ['note' => 'Contract probe']],
            ['POST', '/api/v1/finance/parent-payment-followups/' . $parentId . '/mark-contacted', ['contacted_at' => '2025-10-05']],
            ['POST', '/api/v1/finance/parent-payment-followups/' . $parentId . '/promise-to-pay', ['amount' => 25, 'promise_date' => '2025-10-15']],
            ['POST', '/api/v1/finance/parent-payment-followups/' . $parentId . '/resolve', ['resolution' => 'resolved']],
            ['GET', '/api/v1/finance/carryforwards/preview', []],
            ['POST', '/api/v1/finance/carryforwards/draft', ['parent_id' => $parentId, 'amount' => 25]],
            ['POST', '/api/v1/finance/carryforwards/1/approve', ['approved' => true]],
            ['POST', '/api/v1/finance/carryforwards/1/post', ['school_year' => '2026-2027']],
            ['POST', '/api/v1/finance/carryforwards/1/waive', ['reason' => 'Contract probe']],
            ['POST', '/api/v1/finance/carryforwards/1/adjust', ['amount' => 10, 'reason' => 'Contract probe']],
            ['GET', '/api/v1/finance/carryforwards/report', []],
            ['GET', '/api/v1/finance/event-charges', []],
            ['POST', '/api/v1/finance/event-charges', ['student_id' => $studentId, 'amount' => 25, 'description' => 'Contract event']],
            ['GET', '/api/v1/finance/event-charges/1', []],
            ['PUT', '/api/v1/finance/event-charges/1', ['amount' => 30, 'description' => 'Updated contract event']],
            ['DELETE', '/api/v1/finance/event-charges/1', []],
            ['POST', '/api/v1/finance/event-charges/1/approve', ['approved' => true]],
            ['POST', '/api/v1/finance/event-charges/1/void', ['reason' => 'Contract probe']],
            ['POST', '/api/v1/finance/event-charges/1/attach-to-invoice', ['invoice_id' => $invoiceId]],
            ['GET', '/api/v1/finance/installment-plans', []],
            ['GET', '/api/v1/finance/installment-plans/1', []],
            ['POST', '/api/v1/finance/invoices/' . $invoiceId . '/installment-plans', ['amount' => 100, 'installments' => 2]],
            ['POST', '/api/v1/finance/installment-plans/1/activate', ['activate' => true]],
            ['POST', '/api/v1/finance/installment-plans/1/cancel', ['reason' => 'Contract probe']],
            ['POST', '/api/v1/finance/installment-plans/1/restructure', ['installments' => 3]],
            ['POST', '/api/v1/finance/payments/1/allocate-installments', ['installment_ids' => [1]]],
            ['GET', '/api/v1/finance/installments/due', []],
            ['GET', '/api/v1/finance/installments/overdue', []],
            ['POST', '/api/v1/finance/payments/1/send-receipt', ['email' => 'receipt@example.test']],
            ['POST', '/api/v1/finance/refunds/1/send-receipt', ['email' => 'refund@example.test']],
            ['POST', '/api/v1/finance/invoices/' . $invoiceId . '/send-statement', ['email' => 'statement@example.test']],
            ['POST', '/api/v1/finance/parents/' . $parentId . '/send-overdue-reminder', ['email' => 'overdue@example.test']],
            ['POST', '/api/v1/finance/installments/1/send-reminder', ['email' => 'installment@example.test']],
            ['GET', '/api/v1/finance/notification-logs', []],
            ['GET', '/api/v1/finance/unpaid-parents', []],
            ['POST', '/api/v1/finance/fees/refund', ['parent_id' => $parentId, 'amount' => 25]],
            ['POST', '/api/v1/finance/fees/tuition-total', ['parent_id' => $parentId]],
            ['POST', '/api/v1/finance/fees/tuition-breakdown', ['parent_id' => $parentId]],
            ['POST', '/api/v1/finance/fees/family-balance', ['parent_id' => $parentId]],
            ['GET', '/api/v1/finance/fees/charges', []],
            ['POST', '/api/v1/finance/fees/charges/extra', ['parent_id' => $parentId, 'amount' => 25, 'description' => 'Contract probe']],
            ['POST', '/api/v1/finance/fees/charges/event', ['student_id' => $studentId, 'amount' => 25, 'description' => 'Contract probe']],
            ['POST', '/api/v1/finance/fees/charges/extra/1/cancel', ['reason' => 'Contract probe']],
            ['POST', '/api/v1/finance/fees/charges/extra/1/apply', ['invoice_id' => $invoiceId]],
            ['POST', '/api/v1/finance/fees/charges/event/1/cancel', ['reason' => 'Contract probe']],
            ['POST', '/api/v1/finance/fees/charges/event/1/mark-paid', ['amount' => 25]],
            ['GET', '/api/v1/finance/refunds', []],
            ['POST', '/api/v1/finance/refunds/recalculate-overpayments', ['parent_id' => $parentId]],
            ['GET', '/api/v1/finance/refunds/parent-balances/' . $parentId, []],
            ['POST', '/api/v1/finance/refunds', ['parent_id' => $parentId, 'amount' => 25, 'reason' => 'Contract probe']],
            ['GET', '/api/v1/finance/refunds/1', []],
            ['PATCH', '/api/v1/finance/refunds/1', ['amount' => 20]],
            ['DELETE', '/api/v1/finance/refunds/1', []],
            ['POST', '/api/v1/finance/refunds/1/approve', ['approved' => true]],
            ['POST', '/api/v1/finance/refunds/1/reject', ['reason' => 'Contract probe']],
            ['POST', '/api/v1/finance/refunds/1/payments', ['amount' => 20, 'payment_method' => 'cash']],
            ['GET', '/api/v1/inventory/categories', []],
            ['POST', '/api/v1/inventory/categories', ['name' => 'Contract Category']],
            ['GET', '/api/v1/inventory/items', []],
            ['POST', '/api/v1/inventory/items', ['name' => 'Contract Item', 'sku' => 'E2E-' . uniqid(), 'quantity' => 2]],
            ['GET', '/api/v1/inventory/items/1', []],
            ['PATCH', '/api/v1/inventory/items/1', ['name' => 'Contract Item Updated']],
            ['DELETE', '/api/v1/inventory/items/1', []],
            ['GET', '/api/v1/inventory/movements', []],
            ['POST', '/api/v1/inventory/movements', ['item_id' => 1, 'quantity' => 1, 'type' => 'adjustment']],
            ['GET', '/api/v1/suppliers', []],
            ['POST', '/api/v1/suppliers', ['name' => 'Contract Supplier', 'email' => 'supplier@example.test']],
            ['GET', '/api/v1/supply-categories', []],
            ['POST', '/api/v1/supply-categories', ['name' => 'Contract Supply']],
            ['GET', '/api/v1/messages', []],
            ['POST', '/api/v1/messages', ['recipient_id' => $teacherId, 'subject' => 'Contract probe', 'body' => 'Message body']],
            ['GET', '/api/v1/messages/1', []],
            ['PATCH', '/api/v1/messages/1/read', ['read' => true]],
            ['PATCH', '/api/v1/messages/1/archive', ['archived' => true]],
            ['GET', '/api/v1/support', []],
            ['POST', '/api/v1/support', ['subject' => 'Contract support', 'message' => 'Support body']],
            ['GET', '/api/v1/support/teacher', []],
            ['POST', '/api/v1/broadcast-email', ['subject' => 'Contract email', 'body' => 'Email body', 'recipients' => [$parentId]]],
            ['GET', '/api/v1/email-extractor/emails', []],
            ['POST', '/api/v1/email-extractor/compare', ['emails' => ['one@example.test'], 'against' => ['two@example.test']]],
            ['GET', '/api/v1/communications', []],
            ['POST', '/api/v1/communications', ['subject' => 'Contract comm', 'message' => 'Body']],
            ['GET', '/api/v1/whatsapp/contacts', []],
            ['POST', '/api/v1/whatsapp/links', ['phone' => '5551234567', 'message' => 'Contract probe']],
        ];
    }
}
