<?php

namespace Tests\Unit\Services\Payments;

use App\Services\Payments\PaymentPlanService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class PaymentPlanServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_create_plan_uses_defaults_when_missing(): void
    {
        DB::table('configuration')->insert([
            ['id' => 2001, 'config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['id' => 2002, 'config_key' => 'semester', 'config_value' => 'Fall'],
        ]);

        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('invoices')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_number' => 'INV-1',
            'total_amount' => 200,
            'balance' => 200,
            'paid_amount' => 0,
            'has_discount' => 0,
            'issue_date' => '2025-01-01',
            'status' => 'unpaid',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);

        $service = new PaymentPlanService();
        $payment = $service->createPlan([
            'parent_id' => 10,
            'invoice_id' => 1,
            'total_amount' => 200,
            'number_of_installments' => 2,
            'payment_date' => '2025-01-01',
            'payment_method' => 'cash',
        ]);

        $this->assertSame('2025-2026', $payment->school_year);
        $this->assertSame('Fall', $payment->semester);
        $this->assertSame(200.0, (float) $payment->total_amount);
    }
}
