<?php

namespace Tests\Feature\Api\V1\Finance;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class FeeCalculationControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_refund_endpoint_returns_amount(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        $this->seedConfig();
        $this->seedClassSection();

        DB::table('invoices')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_number' => 'INV-1',
            'total_amount' => 200.00,
            'balance' => 0.00,
            'paid_amount' => 200.00,
            'has_discount' => 0,
            'issue_date' => '2025-01-05',
            'status' => 'paid',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);

        DB::table('payments')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_id' => 1,
            'paid_amount' => 200.00,
            'total_amount' => 200.00,
            'balance' => 0.00,
            'number_of_installments' => 1,
            'payment_date' => '2025-01-10',
            'payment_method' => 'manual',
            'school_year' => '2025-2026',
            'status' => 'Paid',
        ]);

        $response = $this->postJson('/api/v1/finance/fees/refund', [
            'parent_id' => 10,
            'students' => [
                [
                    'student_id' => 1,
                    'class_section_id' => 101,
                    'enrollment_status' => 'withdrawn',
                    'admission_status' => 'accepted',
                    'withdrawal_date' => '2025-01-15',
                ],
            ],
        ]);

        $response->assertOk();
        $response->assertJsonPath('ok', true);
        $this->assertGreaterThan(0, (float) $response->json('refund.refund_amount'));
    }

    public function test_tuition_total_endpoint_returns_total(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        $this->seedConfig();
        $this->seedClassSection();

        $response = $this->postJson('/api/v1/finance/fees/tuition-total', [
            'students' => [
                ['class_section_id' => 101],
                ['class_section_id' => 101],
            ],
        ]);

        $response->assertOk();
        $response->assertJsonPath('ok', true);
        $this->assertGreaterThan(0, (float) $response->json('tuition.total_tuition'));
    }

    private function seedConfig(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'refund_deadline', 'config_value' => '2025-02-01'],
            ['config_key' => 'weeks_study', 'config_value' => '8'],
            ['config_key' => 'last_school_day', 'config_value' => '2025-06-01'],
            ['config_key' => 'first_student_fee', 'config_value' => '350'],
            ['config_key' => 'second_student_fee', 'config_value' => '200'],
            ['config_key' => 'youth_fee', 'config_value' => '180'],
        ]);
    }

    private function seedClassSection(): void
    {
        DB::table('classSection')->insert([
            'id' => 1,
            'class_id' => 1,
            'class_section_id' => 101,
            'class_section_name' => '3',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);
    }

    private function createUser(): User
    {
        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Admin',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'admin@example.com',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'is_verified' => 1,
            'status' => 'Active',
            'is_suspended' => 0,
            'failed_attempts' => 0,
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        return User::query()->findOrFail(1);
    }
}
