<?php

declare(strict_types=1);

use App\Models\Plan;
use App\Models\User;
use Database\Seeders\RoleAndPermissionSeeder;

beforeEach(function (): void {
    $this->seed(RoleAndPermissionSeeder::class);
    $this->accountUrl = 'http://'.config('app.domains.account');
});

it('displays the plans index page', function (): void {
    $user = User::factory()->create();
    Plan::factory()->count(3)->create(['status' => 'active']);

    $this->actingAs($user)
        ->get($this->accountUrl.'/plans')
        ->assertOk()
        ->assertInertia(fn ($page) => $page
            ->component('Plans/Index')
            ->has('plansByType')
        );
});

it('does not show inactive plans', function (): void {
    $user = User::factory()->create();
    Plan::factory()->create(['status' => 'active', 'service_type' => 'vps']);
    Plan::factory()->create(['status' => 'inactive', 'service_type' => 'vps']);

    $this->actingAs($user)
        ->get($this->accountUrl.'/plans')
        ->assertOk()
        ->assertInertia(fn ($page) => $page
            ->component('Plans/Index')
            ->where('plansByType.vps', fn ($plans) => count($plans) === 1)
        );
});

it('displays a single plan page', function (): void {
    $user = User::factory()->create();
    $plan = Plan::factory()->create(['status' => 'active']);

    $this->actingAs($user)
        ->get($this->accountUrl.'/plans/'.$plan->id)
        ->assertOk()
        ->assertInertia(fn ($page) => $page
            ->component('Plans/Show')
            ->has('plan')
            ->where('plan.id', $plan->id)
        );
});

it('returns 404 for inactive plan', function (): void {
    $user = User::factory()->create();
    $plan = Plan::factory()->create(['status' => 'inactive']);

    $this->actingAs($user)
        ->get($this->accountUrl.'/plans/'.$plan->id)
        ->assertNotFound();
});
