<?php

use App\Livewire\Studio\Dashboard\Overview;
use App\Models\Expense;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Perfume;
use App\Models\Purchase;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

beforeEach(function () {
    $this->setupUser();

    // Ensure the user has Developer role and required permissions
    $role = Role::firstOrCreate(['name' => 'Developer']);
    Permission::firstOrCreate(['name' => 'view studio overview']);
    $role->givePermissionTo('view studio overview');
    $this->user->assignRole($role);
});

it('renders the overview page correctly', function () {
    $this->actingAs($this->user)
        ->get(route('studio.dashboard.overview'))
        ->assertOk()
        ->assertSeeLivewire(Overview::class);
});

it('calculates stats correctly for today and yesterday', function () {
    // Today's data
    Order::factory()->count(2)->today()->paid()->withOutlet($this->outlet)->create([
        'total' => 100000,
        'cogs' => 40000,
        'discount' => 10000,
    ]);
    Expense::factory()->today()->create(['amount' => 5000, 'outlet_id' => $this->outlet->id]);
    Purchase::factory()->today()->create(['total' => 50000]);

    // Yesterday's data
    Order::factory()->count(1)->yesterday()->paid()->withOutlet($this->outlet)->create([
        'total' => 80000,
        'cogs' => 30000,
        'discount' => 5000,
    ]);
    Expense::factory()->yesterday()->create(['amount' => 2000, 'outlet_id' => $this->outlet->id]);
    Purchase::factory()->yesterday()->create(['total' => 30000]);

    Livewire::actingAs($this->user)
        ->test(Overview::class)
        ->assertSet('stats.2.value', 2) // Total Order Today
        ->assertSet('stats.2.previous', 1) // Total Order Yesterday
        ->assertSet('stats.3.value', 'Rp200.000') // Pendapatan Today (2 * 100k)
        ->assertSet('stats.3.previous', 'Rp80.000'); // Pendapatan Yesterday (1 * 80k)
});

it('filters stats by selected outlets', function () {
    $anotherOutlet = \App\Models\Outlet::factory()->create();
    $this->user->outlets()->attach($anotherOutlet);

    // Order in main outlet
    Order::factory()->today()->paid()->withOutlet($this->outlet)->create(['total' => 100000]);
    // Order in another outlet
    Order::factory()->today()->paid()->withOutlet($anotherOutlet)->create(['total' => 50000]);

    Livewire::actingAs($this->user)
        ->test(Overview::class)
        // No filter (shows both)
        ->assertSet('stats.3.value', 'Rp150.000')
        // Filter by main outlet only
        ->set('selectedOutletIds', [$this->outlet->id])
        ->assertSet('stats.3.value', 'Rp100.000')
        // Filter by another outlet only
        ->set('selectedOutletIds', [$anotherOutlet->id])
        ->assertSet('stats.3.value', 'Rp50.000');
});

it('shows top perfume correctly', function () {
    $perfume = Perfume::factory()->create(['name' => 'Royal Oud']);

    $order = Order::factory()->today()->paid()->withOutlet($this->outlet)->create();
    OrderItem::factory()->create([
        'order_id' => $order->id,
        'orderable_type' => Perfume::class,
        'orderable_id' => $perfume->id,
        'quantity' => 100,
    ]);

    Livewire::actingAs($this->user)
        ->test(Overview::class)
        ->assertSee('Royal Oud (100ml)');
});
