<?php

namespace App\Models;

use App\Enums\WorkAssignmentStatus;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class WorkAssignment extends Model
{
    protected $fillable = [
        'work_id',
        'technician_id',
        'coordinator_id',
        'recorded_at',
        'status',
    ];

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

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

    public function technician(): BelongsTo
    {
        return $this->belongsTo(User::class, 'technician_id');
    }

    public function coordinator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'coordinator_id');
    }

    public function statusEnum(): ?WorkAssignmentStatus
    {
        return WorkAssignmentStatus::fromDatabase((string) $this->status);
    }
}
