<?php

namespace Tests\Feature\Api\V1\Finance;

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

class PaymentNotificationControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_send_creates_log(): void
    {
        Mail::fake();
        $this->seedUsers();
        $this->seedConfig();
        $this->seedInvoice();

        Sanctum::actingAs(User::query()->findOrFail(1));

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

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

    public function test_index_lists_logs(): void
    {
        $this->seedUsers();

        DB::table('payment_notification_logs')->insert([
            'parent_id' => 10,
            'school_year' => '2025-2026',
            'period_year' => 2025,
            'period_month' => 1,
            'type' => 'installment',
            'to_email' => 'parent@example.com',
            'status' => 'sent',
            'balance_snapshot' => 100,
            'sent_at' => '2025-01-01 10:00:00',
        ]);

        Sanctum::actingAs(User::query()->findOrFail(1));

        $response = $this->getJson('/api/v1/finance/payment-notifications');

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

    private function seedUsers(): void
    {
        DB::table('roles')->insert([
            ['id' => 1, 'name' => 'admin', 'slug' => 'admin', 'is_active' => 1],
            ['id' => 2, 'name' => 'parent', 'slug' => 'parent', 'is_active' => 1],
        ]);

        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',
        ]);

        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' => 1, 'role_id' => 1],
            ['user_id' => 10, 'role_id' => 2],
        ]);
    }

    private function seedConfig(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
            ['config_key' => 'installment_date', 'config_value' => '2025-06-01'],
        ]);
    }

    private function seedInvoice(): void
    {
        DB::table('invoices')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_number' => 'INV-001',
            'total_amount' => 200,
            'balance' => 200,
            'paid_amount' => 0,
            'status' => 'unpaid',
            'school_year' => '2025-2026',
            'issue_date' => '2025-01-01',
        ]);
    }
}
