<?php

use App\Filament\Resources\Plans\Pages\EditPlan;
use App\Filament\Resources\Plans\Pages\ListPlans;
use App\Models\Plan;
use App\Models\User;
use Filament\Actions\DeleteAction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    $this->artisan('db:seed', ['--class' => 'PlanSeeder']);
    $this->actingAs(User::factory()->create(['is_admin' => true]));
});

// ─── ListPlans ────────────────────────────────────────────────────────────────

it('lists all four plans', function (): void {
    Livewire::test(ListPlans::class)
        ->assertCanSeeTableRecords(Plan::all());
});

it('has no create button on the list page', function (): void {
    Livewire::test(ListPlans::class)
        ->assertActionDoesNotExist('create');
});

// ─── EditPlan — no delete ─────────────────────────────────────────────────────

it('has no delete action on the edit page', function (): void {
    $plan = Plan::where('name', 'basic')->first();

    Livewire::test(EditPlan::class, ['record' => $plan->id])
        ->assertActionDoesNotExist(DeleteAction::class);
});

// ─── EditPlan — saves features correctly ─────────────────────────────────────

it('saves email frequency on edit', function (): void {
    $plan = Plan::where('name', 'free')->first();

    Livewire::test(EditPlan::class, ['record' => $plan->id])
        ->fillForm([
            'email_frequency' => 'daily',
        ])
        ->call('save')
        ->assertHasNoFormErrors();

    expect($plan->fresh()->email_frequency)->toBe('daily');
});

it('saves sms daily limit on edit', function (): void {
    $plan = Plan::where('name', 'plus')->first();

    Livewire::test(EditPlan::class, ['record' => $plan->id])
        ->fillForm([
            'sms_daily_limit' => 3,
        ])
        ->call('save')
        ->assertHasNoFormErrors();

    expect($plan->fresh()->sms_daily_limit)->toBe(3);
});

it('saves null fuel type max for pro (unlimited)', function (): void {
    $plan = Plan::where('name', 'pro')->first();

    Livewire::test(EditPlan::class, ['record' => $plan->id])
        ->fillForm([
            'max_fuel_types' => null,
        ])
        ->call('save')
        ->assertHasNoFormErrors();

    expect($plan->fresh()->max_fuel_types)->toBeNull();
});
