<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class Item extends Model
{
    use HasFactory;

    protected $fillable = [
        'location_id',
        'category_id',
        'import_row_id',
        'name',
        'slug',
        'code',
        'tracking_mode',
        'unit_of_measure',
        'total_quantity',
        'good_count',
        'minor_damage_count',
        'major_damage_count',
        'lost_count',
        'notes',
        'last_checked_at',
        'qr_token',
        'is_active',
    ];

    protected $casts = [
        'last_checked_at' => 'datetime',
        'is_active' => 'boolean',
    ];

    public function location(): BelongsTo
    {
        return $this->belongsTo(Location::class);
    }

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

    public function importRow(): BelongsTo
    {
        return $this->belongsTo(ImportRow::class);
    }

    public function stock(): HasOne
    {
        return $this->hasOne(ItemStock::class);
    }

    public function assetUnits(): HasMany
    {
        return $this->hasMany(AssetUnit::class);
    }

    public function maintenanceLogs(): MorphMany
    {
        return $this->morphMany(MaintenanceLog::class, 'maintainable');
    }
}
