<?php

namespace App\Models;

use App\Models\Concerns\HasIndonesianTimestamps;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class InventoryItem extends Model
{
    use HasIndonesianTimestamps;

    public const CREATED_AT = 'dibuat_pada';
    public const UPDATED_AT = 'diperbarui_pada';

    protected $fillable = [
        'id_kategori',
        'id_ruangan',
        'nama_barang',
        'kode_aset',
        'tahun_perolehan',
        'umur_manfaat_tahun',
        'status_barang',
        'tanggal_jadwal_pemeliharaan',
        'pengajuan_penggantian',
        'catatan',
        'lokasi_foto',
    ];

    protected $casts = [
        'tanggal_jadwal_pemeliharaan' => 'date',
        'pengajuan_penggantian' => 'boolean',
    ];

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class, 'id_kategori');
    }

    public function room(): BelongsTo
    {
        return $this->belongsTo(Room::class, 'id_ruangan');
    }

    public function scanActivities(): HasMany
    {
        return $this->hasMany(ScanActivity::class, 'id_barang');
    }

    public function getStatusLabelAttribute(): string
    {
        return match ($this->status_barang) {
            'maintenance' => 'Perawatan',
            'broken' => 'Rusak',
            'replace' => 'Ganti',
            default => 'Baik',
        };
    }

    public function getStatusClassAttribute(): string
    {
        return match ($this->status_barang) {
            'maintenance' => 'warning',
            'broken' => 'danger',
            'replace' => 'replace',
            default => 'ok',
        };
    }

    public function scanUrl(): string
    {
        return route('scan.items.show', $this->kode_aset);
    }

    public function getCategoryIdAttribute(): ?int
    {
        return $this->attributes['id_kategori'] ?? null;
    }

    public function getRoomIdAttribute(): ?int
    {
        return $this->attributes['id_ruangan'] ?? null;
    }

    public function getNameAttribute(): ?string
    {
        return $this->attributes['nama_barang'] ?? null;
    }

    public function getAssetCodeAttribute(): ?string
    {
        return $this->attributes['kode_aset'] ?? null;
    }

    public function getAcquisitionYearAttribute(): ?int
    {
        return $this->attributes['tahun_perolehan'] ?? null;
    }

    public function getUsefulLifeYearsAttribute(): ?int
    {
        return $this->attributes['umur_manfaat_tahun'] ?? null;
    }

    public function getStatusAttribute(): ?string
    {
        return $this->attributes['status_barang'] ?? null;
    }

    public function getMaintenanceDueDateAttribute()
    {
        return $this->tanggal_jadwal_pemeliharaan;
    }

    public function getReplacementRequestedAttribute(): bool
    {
        return (bool) ($this->attributes['pengajuan_penggantian'] ?? false);
    }

    public function getNotesAttribute(): ?string
    {
        return $this->attributes['catatan'] ?? null;
    }

    public function getPhotoUrlAttribute(): ?string
    {
        $path = $this->attributes['lokasi_foto'] ?? null;

        if (! $path) {
            return null;
        }

        return '/storage/' . ltrim($path, '/');
    }
}
