<?php

namespace Tests\Feature\Api\V1\Discounts;

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

class DiscountControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_options_returns_vouchers_and_parents(): void
    {
        $this->seedDiscountData();

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

        $response = $this->getJson('/api/v1/discounts/options');

        $response->assertOk();
        $this->assertNotEmpty($response->json('vouchers'));
        $this->assertNotEmpty($response->json('parents'));
    }

    public function test_apply_voucher_applies_discount(): void
    {
        $this->seedDiscountData();

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

        $response = $this->postJson('/api/v1/discounts/apply', [
            'voucher_id' => 1,
            'parent_ids' => [10],
            'allow_additional' => true,
        ]);

        $response->assertOk();
        $response->assertJson(['ok' => true]);

        $this->assertDatabaseHas('discount_usages', [
            'voucher_id' => 1,
            'parent_id' => 10,
            'invoice_id' => 1,
        ]);
    }

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

        $response = $this->postJson('/api/v1/discounts/vouchers', [
            'code' => 'NEW-10',
            'discount_type' => 'percent',
            'discount_value' => 10,
            'is_active' => true,
        ]);

        $response->assertCreated();
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('discount_vouchers', ['code' => 'NEW-10']);
    }

    public function test_show_voucher_returns_resource(): void
    {
        $this->seedDiscountData();

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

        $response = $this->getJson('/api/v1/discounts/vouchers/1');

        $response->assertOk();
        $response->assertJson([
            'ok' => true,
            'voucher' => [
                'id' => 1,
                'code' => 'TEST-100',
            ],
        ]);
    }

    public function test_delete_voucher_removes_row(): void
    {
        $this->seedDiscountData();

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

        $response = $this->deleteJson('/api/v1/discounts/vouchers/1');

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseMissing('discount_vouchers', ['id' => 1]);
    }

    private function seedDiscountData(): void
    {
        DB::table('configuration')->insert([
            ['id' => 1, 'config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['id' => 2, 'config_key' => 'semester', 'config_value' => 'Fall'],
        ]);

        DB::table('users')->insert([
            'id' => 10,
            'school_id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'One',
            '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('roles')->insert([
            'id' => 1,
            'name' => 'parent',
            'slug' => 'parent',
            'is_active' => 1,
        ]);

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

        DB::table('discount_vouchers')->insert([
            'id' => 1,
            'code' => 'TEST-100',
            'discount_type' => 'fixed',
            'discount_value' => 100,
            'max_uses' => 10,
            'times_used' => 0,
            'is_active' => 1,
        ]);

        DB::table('invoices')->insert([
            'id' => 1,
            'parent_id' => 10,
            'invoice_number' => 'INV-1',
            'total_amount' => 100,
            'balance' => 100,
            'paid_amount' => 0,
            'status' => 'Unpaid',
            'issue_date' => '2025-09-01',
            'school_year' => '2025-2026',
        ]);

        DB::table('enrollments')->insert([
            'parent_id' => 10,
            'student_id' => 1,
            'school_year' => '2025-2026',
            'enrollment_status' => 'payment pending',
            'enrollment_date' => '2025-09-01',
        ]);
    }

    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);
    }
}
