<?php

namespace App\Models;

use App\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AssetAssignment extends Model
{
    use HasFactory, LogsActivity;

    protected $fillable = [
        'asset_id',
        'user_id',
        'assigned_at',
        'returned_at',
        'return_condition',
        'notes',
    ];

    protected $appends = [
        'assigned_at_formatted',
        'returned_at_formatted',
    ];

    protected function casts(): array
    {
        return [
            'assigned_at' => 'datetime',
            'returned_at' => 'datetime',
        ];
    }

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

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

    protected function getAssignedAtFormattedAttribute(): string
    {
        return $this->assigned_at ? $this->assigned_at->format('M d, Y') : '';
    }

    protected function getReturnedAtFormattedAttribute(): string
    {
        return $this->returned_at ? $this->returned_at->format('M d, Y') : '';
    }
}
