<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $fillable = [
        'name',
        'email',
        'password',
        'role',
        'phone',
        'is_active',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
            'is_active' => 'boolean',
        ];
    }

    // 👇 TAMBAHKAN RELASI INI!
    public function contractor()
    {
        return $this->hasOne(Contractor::class, 'user_id');
    }

    public function ppkPackages()
    {
        return $this->hasMany(JobPackage::class, 'ppk_id');
    }

    // ===== ROLE CHECK =====
    public function isPpk(): bool
    {
        return $this->role === 'ppk';
    }

    public function isAdmin(): bool
    {
        return $this->role === 'admin';
    }

    public function isContractor(): bool
    {
        return $this->role === 'contractor';
    }

    public function isAgent(): bool
    {
        return $this->role === 'agent';
    }

    public function isBank(): bool
    {
        return $this->role === 'bank';
    }

    public function isAuditor(): bool
    {
        return $this->role === 'auditor';
    }

    public function getRoleLabelAttribute(): string
    {
        return [
            'super_admin' => 'Super Admin',
            'admin' => 'Admin Sistem',
            'ppk' => 'PPK / Pejabat Pengadaan',
            'contractor' => 'Kontraktor OAP',
            'agent' => 'Agen Pengadaan',
            'bank' => 'Bank',
            'auditor' => 'Auditor / BPK',
        ][$this->role] ?? $this->role;
    }
}