<?php

namespace Tests\Feature;

use App\Models\{Category, CashSession, Product, Setting, Transaction, TransactionItem, User};
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ReportTest extends TestCase
{
    use RefreshDatabase;

    protected User $admin;
    protected User $kasir;
    protected User $manajer;

    protected function setUp(): void
    {
        parent::setUp();

        Setting::insert([
            ['key' => 'store_name', 'value' => 'Toko Test', 'type' => 'string', 'group' => 'store', 'label' => 'Nama'],
        ]);

        $this->admin = User::create([
            'name' => 'Admin', 'username' => 'admin',
            'email' => 'admin@test.com', 'password' => bcrypt('p'),
            'role' => 'admin', 'is_active' => true,
        ]);

        $this->kasir = User::create([
            'name' => 'Kasir Satu', 'username' => 'kasir1',
            'email' => 'kasir@test.com', 'password' => bcrypt('p'),
            'role' => 'kasir', 'is_active' => true,
        ]);

        $this->manajer = User::create([
            'name' => 'Manajer', 'username' => 'manajer1',
            'email' => 'manajer@test.com', 'password' => bcrypt('p'),
            'role' => 'manajer', 'is_active' => true,
        ]);
    }

    /** @test */
    public function admin_bisa_akses_laporan_penjualan()
    {
        $this->actingAs($this->admin)
            ->get(route('reports.sales'))
            ->assertOk()
            ->assertViewIs('reports.sales');
    }

    /** @test */
    public function manajer_bisa_akses_laporan_kasir()
    {
        $this->actingAs($this->manajer)
            ->get(route('reports.clerk'))
            ->assertOk()
            ->assertViewIs('reports.clerk');
    }

    /** @test */
    public function kasir_tidak_bisa_akses_laporan()
    {
        $this->actingAs($this->kasir)
            ->get(route('reports.sales'))
            ->assertStatus(403);

        $this->actingAs($this->kasir)
            ->get(route('reports.clerk'))
            ->assertStatus(403);
    }

    /** @test */
    public function laporan_stok_tampil_benar()
    {
        $cat = Category::create(['name' => 'Cat', 'slug' => 'cat', 'color' => '#000']);
        Product::create([
            'name' => 'Produk X', 'sku' => 'X001',
            'category_id'   => $cat->id,
            'cost_price'    => 5000,
            'selling_price' => 10000,
            'stock'         => 3,
            'min_stock'     => 10,   // lebih dari stok = menipis
            'track_stock'   => true,
            'is_active'     => true,
        ]);

        $this->actingAs($this->admin)
            ->get(route('reports.stock'))
            ->assertOk()
            ->assertViewIs('reports.stock');
    }

    /** @test */
    public function laporan_bisa_difilter_berdasarkan_tanggal()
    {
        $this->actingAs($this->admin)
            ->get(route('reports.sales', [
                'start' => now()->subDays(7)->format('Y-m-d'),
                'end'   => now()->format('Y-m-d'),
            ]))
            ->assertOk();
    }

    /** @test */
    public function dashboard_tampil_ringkasan_hari_ini()
    {
        $this->actingAs($this->admin)
            ->get(route('dashboard'))
            ->assertOk()
            ->assertViewIs('dashboard.index')
            ->assertViewHas(['todaySales', 'weekSales', 'lowStockProducts', 'topTodayProducts']);
    }
}
