<?php

namespace Tests\Feature\Livewire;

use App\Livewire\Dashboard\Dashboard;
use App\Models\Plan;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Livewire\Livewire;
use Tests\TestCase;

class DashboardTest extends TestCase
{
    public $user;
    protected function setUp(): void
    {
        parent::setUp();

        // Clear all relevant caches to prevent conflicts
        cache()->forget('app_plans');
        cache()->forget('app_menus');
        cache()->forget('app_blogs');

        // Create plans for the dashboard to use BEFORE loading application data
        Plan::factory()->count(2)->create();

        // Reload application data to pick up the plans we just created
        $this->loadApplicationData();

        $this->user = User::factory()->create();
        Auth::login($this->user);
    }

    /** @test */
    public function it_renders_dashboard_component(): void
    {
        $component = Livewire::test(Dashboard::class);

        $component->assertStatus(200);
        $component->assertViewIs('livewire.dashboard.dashboard');
    }

    /** @test */
    public function it_displays_user_information(): void
    {
        $component = Livewire::test(Dashboard::class);

        // Check that dashboard renders with usage statistics
        $component->assertSee('Mailbox Created');
        $component->assertSee('Emails Received');
        $component->assertSee('0'); // usage counts
    }

    /** @test */
    public function it_shows_subscription_status(): void
    {
        $component = Livewire::test(Dashboard::class);

        // Test that the component displays subscription pricing section (since user is not subscribed)
        $component->assertSee('Purchase Subscription');
        $component->assertSee('Have an Activation Key?');
    }
}
