<?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 InvoiceControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_management_returns_parent_invoice_data(): void
    {
        $this->seedBaseData();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->getJson('/api/v1/finance/invoices/management?school_year=2025-2026');

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertNotEmpty($response->json('invoices'));
    }

    public function test_generate_creates_invoice(): void
    {
        $this->seedBaseData(false);

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/finance/invoices/generate', [
            'parent_id' => 10,
            'school_year' => '2025-2026',
        ]);

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('invoices', [
            'parent_id' => 10,
            'school_year' => '2025-2026',
        ]);
    }

    public function test_by_parent_returns_invoices(): void
    {
        $this->seedBaseData();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->getJson('/api/v1/finance/invoices/parent/10?school_year=2025-2026');

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertNotEmpty($response->json('invoices'));
    }

    private function seedBaseData(bool $withInvoice = true): void
    {
        DB::table('configuration')->insert([
            'id' => 1,
            'config_key' => 'school_year',
            'config_value' => '2025-2026',
        ]);

        DB::table('roles')->insert([
            ['id' => 1, 'name' => 'parent', 'slug' => 'parent', 'is_active' => 1],
        ]);

        DB::table('users')->insert([
            'id' => 10,
            'school_id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'parent@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',
        ]);

        DB::table('user_roles')->insert([
            'user_id' => 10,
            'role_id' => 1,
        ]);

        DB::table('students')->insert([
            'id' => 100,
            'parent_id' => 10,
            'firstname' => 'Kid',
            'lastname' => 'User',
            'school_id' => 1,
            'age' => 10,
            'gender' => 'Male',
            'is_active' => 1,
            'photo_consent' => 1,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
        ]);

        DB::table('classSection')->insert([
            'class_section_id' => 1,
            'class_section_name' => '1A',
            'class_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('student_class')->insert([
            'student_id' => 100,
            'class_section_id' => 1,
            'school_year' => '2025-2026',
            'is_event_only' => 0,
            'semester' => 'Fall',
        ]);

        DB::table('enrollments')->insert([
            'student_id' => 100,
            'parent_id' => 10,
            'class_section_id' => 1,
            'enrollment_status' => 'enrolled',
            'enrollment_date' => '2025-01-10',
            'school_year' => '2025-2026',
        ]);

        if ($withInvoice) {
            DB::table('invoices')->insert([
                'id' => 1,
                'parent_id' => 10,
                'invoice_number' => 'INV-001',
                'total_amount' => 100,
                'balance' => 100,
                'paid_amount' => 0,
                'has_discount' => 0,
                'issue_date' => '2025-01-10',
                'status' => 'unpaid',
                '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);
    }
}
