<?php

namespace Tests\Unit;

use App\Models\ObatKeluar;
use Carbon\Carbon;
use Tests\TestCase;

class ObatKeluarModelTest extends TestCase
{
    // ─────────────────────────────────────────
    // getSisaHariAttribute()
    // ─────────────────────────────────────────

    public function test_sisa_hari_is_positive_for_future_expiry(): void
    {
        $obat = new ObatKeluar();
        $obat->setRawAttributes(['tanggal_kadaluarsa' => Carbon::now()->addDays(30)->toDateString()]);

        $this->assertGreaterThan(0, $obat->sisa_hari);
    }

    public function test_sisa_hari_is_negative_for_expired(): void
    {
        $obat = new ObatKeluar();
        $obat->setRawAttributes(['tanggal_kadaluarsa' => Carbon::now()->subDays(10)->toDateString()]);

        $this->assertLessThan(0, $obat->sisa_hari);
    }

    public function test_sisa_hari_returns_integer(): void
    {
        $obat = new ObatKeluar();
        $obat->setRawAttributes(['tanggal_kadaluarsa' => Carbon::now()->addDays(60)->toDateString()]);

        $this->assertIsInt($obat->sisa_hari);
    }

    // ─────────────────────────────────────────
    // Fillable & Casts
    // ─────────────────────────────────────────

    public function test_obat_keluar_has_correct_fillable_fields(): void
    {
        $obat = new ObatKeluar();
        $fillable = $obat->getFillable();

        $this->assertContains('obat_masuk_id', $fillable);
        $this->assertContains('nama_obat', $fillable);
        $this->assertContains('jumlah', $fillable);
        $this->assertContains('status', $fillable);
        $this->assertContains('nama_petugas', $fillable);
        $this->assertContains('nama_penerima', $fillable);
        $this->assertContains('tujuan_pemakaian', $fillable);
    }

    public function test_tanggal_columns_are_cast_as_date(): void
    {
        $obat = new ObatKeluar();
        $casts = $obat->getCasts();
        $this->assertEquals('date', $casts['tanggal_pengeluaran']);
        $this->assertEquals('date', $casts['tanggal_kadaluarsa']);
    }

    public function test_harga_columns_are_cast_as_decimal(): void
    {
        $obat = new ObatKeluar();
        $casts = $obat->getCasts();
        $this->assertArrayHasKey('harga', $casts);
        $this->assertArrayHasKey('harga_total', $casts);
    }

    // ─────────────────────────────────────────
    // Relationships
    // ─────────────────────────────────────────

    public function test_obat_keluar_belongs_to_obat_masuk(): void
    {
        $obat = new ObatKeluar();
        $this->assertInstanceOf(
            \Illuminate\Database\Eloquent\Relations\BelongsTo::class,
            $obat->obatMasuk()
        );
    }

    public function test_obat_keluar_belongs_to_user(): void
    {
        $obat = new ObatKeluar();
        $this->assertInstanceOf(
            \Illuminate\Database\Eloquent\Relations\BelongsTo::class,
            $obat->user()
        );
    }
}
