<?php

namespace Tests\Feature\Api\V1\Workflows;

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

/**
 * Scenario D: Family Billing Lifecycle.
 *
 * @see docs/alrahma_end_to_end_use_case_test_plan.md Scenario D
 */
class ScenarioDFamilyBillingTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;

    public function test_admin_runs_invoice_discount_payment_and_reporting_workflow(): void
    {
        $world = $this->seedTeacherClassWithStudent();
        $admin = $this->actingAsApiAdministrator();
        $parentId = $world['parent_id'];
        $studentId = $world['student_id'];

        DB::table('enrollments')->insert([
            'parent_id' => $parentId,
            'student_id' => $studentId,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'semester' => self::E2E_SEMESTER,
            'enrollment_status' => 'payment pending',
            'enrollment_date' => '2025-09-01',
        ]);

        $invoiceId = $this->seedInvoiceForParent($parentId);
        $voucherId = $this->seedDiscountVoucher();

        $this->actingAs($admin, 'api');
        $this->postJson('/api/v1/discounts/apply', [
            'voucher_id' => $voucherId,
            'parent_ids' => [$parentId],
            'allow_additional' => true,
        ])->assertOk()->assertJsonPath('ok', true);

        $chargeResponse = $this->postJson('/api/v1/finance/event-charges', [
            'event_name' => 'Science Fair',
            'description' => 'Materials fee',
            'amount' => 15.00,
            'parent_id' => $parentId,
            'student_id' => $studentId,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'semester' => self::E2E_SEMESTER,
        ]);
        $chargeResponse->assertOk()->assertJsonPath('ok', true);
        $chargeId = (int) DB::table('event_charges')
            ->where('parent_id', $parentId)
            ->where('student_id', $studentId)
            ->where('event_name', 'Science Fair')
            ->orderByDesc('id')
            ->value('id');
        $this->assertGreaterThan(0, $chargeId);

        $this->postJson('/api/v1/finance/event-charges/' . $chargeId . '/approve')
            ->assertOk()
            ->assertJsonPath('ok', true);

        $this->postJson('/api/v1/finance/payments', [
            'parent_id' => $parentId,
            'invoice_id' => $invoiceId,
            'total_amount' => 100,
            'number_of_installments' => 1,
            'payment_date' => '2025-10-01',
            'payment_method' => 'cash',
            'semester' => self::E2E_SEMESTER,
            'school_year' => self::E2E_SCHOOL_YEAR,
        ])->assertStatus(201)->assertJsonPath('ok', true);

        $this->assertDatabaseHas('payments', [
            'parent_id' => $parentId,
            'invoice_id' => $invoiceId,
        ]);

        $this->getJson('/api/v1/finance/payments/parent/' . $parentId . '?school_year=' . self::E2E_SCHOOL_YEAR)
            ->assertOk()
            ->assertJsonPath('ok', true);

        $this->getJson('/api/v1/finance/unpaid-parents')
            ->assertOk()
            ->assertJsonPath('ok', true);
    }
}
