<?php

namespace Tests\Feature;

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

class LaporanTest extends DatabaseTestCase
{
    private User $user;
    private Kategori $kategori;
    private Satuan $satuan;

    protected function setUp(): void
    {
        parent::setUp();
        $this->user     = User::factory()->create(['role' => 'apoteker']);
        $this->aktegori = Kategori::create(['nama' => 'Antibiotik']);
        $this->satuan   = Satuan::create(['nama' => 'Tablet']);
        $this->kategori = $this->aktegori ?? Kategori::first();
        $this->actingAs($this->user);
    }

    private function createObatMasuk(string $tanggal): ObatMasuk
    {
        static $counter = 0;
        $counter++;

        return ObatMasuk::create([
            'nama_obat'          => 'Obat-' . $counter,
            'kategori_id'        => $this->kategori->id,
            'satuan_id'          => $this->satuan->id,
            'kode_batch'         => 'BTH-' . $counter,
            'stok'               => 50,
            'tanggal_penerimaan' => $tanggal,
            'tanggal_kadaluarsa' => now()->addYear()->toDateString(),
            'user_id'            => $this->user->id,
        ]);
    }

    private function createObatKeluar(string $tanggal, int $jumlah = 5): ObatKeluar
    {
        static $counter = 0;
        $counter++;
        $obatMasuk = $this->createObatMasuk(now()->subMonth()->toDateString());

        return ObatKeluar::create([
            'obat_masuk_id'       => $obatMasuk->id,
            'nama_obat'           => 'ObatKeluar-' . $counter,
            'kode_batch'          => 'BTH-KEL-' . $counter,
            'jumlah'              => $jumlah,
            'tujuan_pemakaian'    => 'Test',
            'tanggal_pengeluaran' => $tanggal,
            'tanggal_kadaluarsa'  => now()->addYear()->toDateString(),
            'nama_petugas'        => 'Petugas',
            'nama_penerima'       => 'Penerima',
            'status'              => 'proses',
            'user_id'             => $this->user->id,
        ]);
    }

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

    public function test_laporan_index_returns_200(): void
    {
        $response = $this->get(route('laporan.index'));
        $response->assertStatus(200);
    }

    public function test_laporan_redirects_guest_to_login(): void
    {
        auth()->logout();
        $response = $this->get(route('laporan.index'));
        $response->assertRedirect(route('login'));
    }

    // ─────────────────────────────────────────
    // Jenis masuk
    // ─────────────────────────────────────────

    public function test_laporan_jenis_masuk_filters_by_date_range(): void
    {
        $tanggalDalam  = '2025-03-05';
        $tanggalLuar   = '2025-01-10';

        $this->createObatMasuk($tanggalDalam);
        $this->createObatMasuk($tanggalLuar);

        $response = $this->get(route('laporan.index', [
            'jenis'          => 'masuk',
            'tanggal_mulai'  => '2025-03-01',
            'tanggal_akhir'  => '2025-03-31',
        ]));

        $response->assertStatus(200);
        $response->assertViewHas('jenis', 'masuk');
    }

    // ─────────────────────────────────────────
    // Jenis keluar
    // ─────────────────────────────────────────

    public function test_laporan_jenis_keluar_filters_by_date_range(): void
    {
        $tanggal = now()->toDateString();
        $this->createObatKeluar($tanggal, 5);

        $response = $this->get(route('laporan.index', [
            'jenis'         => 'keluar',
            'tanggal_mulai' => now()->startOfMonth()->toDateString(),
            'tanggal_akhir' => now()->endOfMonth()->toDateString(),
        ]));

        $response->assertStatus(200);
        $response->assertViewHas('jenis', 'keluar');
    }

    // ─────────────────────────────────────────
    // View variables
    // ─────────────────────────────────────────

    public function test_laporan_passes_required_view_variables(): void
    {
        $response = $this->get(route('laporan.index'));

        $response->assertViewHas('data');
        $response->assertViewHas('jenis');
        $response->assertViewHas('tanggalMulai');
        $response->assertViewHas('tanggalAkhir');
    }

    public function test_laporan_defaults_to_jenis_masuk(): void
    {
        $response = $this->get(route('laporan.index'));
        $response->assertViewHas('jenis', 'masuk');
    }

    public function test_laporan_defaults_tanggal_to_current_month(): void
    {
        $response = $this->get(route('laporan.index'));

        $tanggalMulai = $response->viewData('tanggalMulai');
        $tanggalAkhir = $response->viewData('tanggalAkhir');

        $this->assertEquals(now()->startOfMonth()->format('Y-m-d'), $tanggalMulai);
        $this->assertEquals(now()->endOfMonth()->format('Y-m-d'), $tanggalAkhir);
    }
}
