<?php

namespace Tests\Feature;

use App\Models\Kategori;
use App\Models\ObatMasuk;
use App\Models\Satuan;
use App\Models\User;
use Tests\DatabaseTestCase;

class DashboardTest extends DatabaseTestCase
{
    private User $user;

    protected function setUp(): void
    {
        parent::setUp();
        $this->user = User::factory()->create(['role' => 'apoteker']);
        $this->actingAs($this->user);
    }

    // ─────────────────────────────────────────
    // index
    // ─────────────────────────────────────────

    public function test_dashboard_returns_200_for_authenticated_user(): void
    {
        $response = $this->get(route('dashboard'));
        $response->assertStatus(200);
    }

    public function test_dashboard_redirects_guest_to_login(): void
    {
        auth()->logout();
        $response = $this->get(route('dashboard'));
        $response->assertRedirect(route('login'));
    }

    public function test_dashboard_passes_required_variables_to_view(): void
    {
        $response = $this->get(route('dashboard'));

        $response->assertViewHas('totalJenisObat');
        $response->assertViewHas('obatMasukBulanIni');
        $response->assertViewHas('obatKeluarBulanIni');
        $response->assertViewHas('kadaluarsaDekat');
        $response->assertViewHas('labels');
        $response->assertViewHas('obatMasukData');
        $response->assertViewHas('obatKeluarData');
        $response->assertViewHas('monthOptions');
        $response->assertViewHas('filterMonth');
        $response->assertViewHas('filterYear');
    }

    public function test_dashboard_total_jenis_obat_counts_correctly(): void
    {
        $kategori = Kategori::create(['nama' => 'Antibiotik']);
        $satuan   = Satuan::create(['nama' => 'Tablet']);

        ObatMasuk::create([
            'nama_obat'          => 'Obat A',
            'kategori_id'        => $kategori->id,
            'satuan_id'          => $satuan->id,
            'kode_batch'         => 'B1',
            'stok'               => 10,
            'tanggal_penerimaan' => now()->toDateString(),
            'tanggal_kadaluarsa' => now()->addYear()->toDateString(),
            'user_id'            => $this->user->id,
        ]);

        ObatMasuk::create([
            'nama_obat'          => 'Obat B',
            'kategori_id'        => $kategori->id,
            'satuan_id'          => $satuan->id,
            'kode_batch'         => 'B2',
            'stok'               => 20,
            'tanggal_penerimaan' => now()->toDateString(),
            'tanggal_kadaluarsa' => now()->addYear()->toDateString(),
            'user_id'            => $this->user->id,
        ]);

        $response = $this->get(route('dashboard'));
        $response->assertViewHas('totalJenisObat', 2);
    }

    public function test_dashboard_month_options_has_12_entries(): void
    {
        $response = $this->get(route('dashboard'));

        $monthOptions = $response->viewData('monthOptions');
        $this->assertCount(12, $monthOptions);
    }

    public function test_dashboard_filter_by_chart_month_and_year(): void
    {
        $response = $this->get(route('dashboard', [
            'chart_month' => 1,
            'chart_year'  => 2025,
        ]));

        $response->assertStatus(200);
        $response->assertViewHas('filterMonth', '1');
        $response->assertViewHas('filterYear', '2025');
    }
}
