<?php

namespace Tests\Unit;

use App\Models\User;
use Tests\DatabaseTestCase;

class UserModelTest extends DatabaseTestCase
{

    // ─────────────────────────────────────────
    // isDokter()
    // ─────────────────────────────────────────

    public function test_is_dokter_returns_true_when_role_is_dokter(): void
    {
        $user = User::factory()->create(['role' => 'dokter']);
        $this->assertTrue($user->isDokter());
    }

    public function test_is_dokter_returns_false_when_role_is_not_dokter(): void
    {
        $user = User::factory()->create(['role' => 'apoteker']);
        $this->assertFalse($user->isDokter());
    }

    public function test_is_dokter_returns_false_when_role_is_empty(): void
    {
        $user = User::factory()->create(['role' => null]);
        $this->assertFalse($user->isDokter());
    }

    // ─────────────────────────────────────────
    // isApoteker()
    // ─────────────────────────────────────────

    public function test_is_apoteker_returns_true_when_role_is_apoteker(): void
    {
        $user = User::factory()->create(['role' => 'apoteker']);
        $this->assertTrue($user->isApoteker());
    }

    public function test_is_apoteker_returns_false_when_role_is_dokter(): void
    {
        $user = User::factory()->create(['role' => 'dokter']);
        $this->assertFalse($user->isApoteker());
    }

    // ─────────────────────────────────────────
    // canManageResep()
    // ─────────────────────────────────────────

    public function test_can_manage_resep_returns_true_for_dokter(): void
    {
        $user = User::factory()->create(['role' => 'dokter']);
        $this->assertTrue($user->canManageResep());
    }

    public function test_can_manage_resep_returns_false_for_apoteker(): void
    {
        $user = User::factory()->create(['role' => 'apoteker']);
        $this->assertFalse($user->canManageResep());
    }

    public function test_can_manage_resep_returns_false_for_admin(): void
    {
        $user = User::factory()->create(['role' => 'admin']);
        $this->assertFalse($user->canManageResep());
    }

    // ─────────────────────────────────────────
    // Relasi reseps()
    // ─────────────────────────────────────────

    public function test_user_has_many_reseps_relationship(): void
    {
        $user = User::factory()->create(['role' => 'dokter']);
        $this->assertInstanceOf(
            \Illuminate\Database\Eloquent\Relations\HasMany::class,
            $user->reseps()
        );
    }

    // ─────────────────────────────────────────
    // Fillable / Hidden attributes
    // ─────────────────────────────────────────

    public function test_password_is_hidden_from_serialization(): void
    {
        $user = User::factory()->create(['password' => bcrypt('secret')]);
        $array = $user->toArray();
        $this->assertArrayNotHasKey('password', $array);
    }

    public function test_user_fillable_fields_are_correct(): void
    {
        $user = new User();
        $expected = ['name', 'nip', 'email', 'phone', 'gender', 'address', 'division', 'position', 'profile_photo', 'password', 'role'];
        $this->assertEquals($expected, $user->getFillable());
    }
}
