<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class WorkOrder extends Model
{
    protected $fillable = [
        'wo_number','client_id','client_user_id','ticket_id','assigned_to','created_by',
        'division','type','item_name','item_brand','item_serial','reported_issue',
        'diagnosis','action_taken','estimated_cost','final_cost','spareparts','status',
        'customer_name','customer_phone','received_at','delivered_at','delivery_signature','notes'
    ];
    protected $casts = [
        'spareparts'=>'array','received_at'=>'datetime','delivered_at'=>'datetime',
        'estimated_cost'=>'decimal:2','final_cost'=>'decimal:2'
    ];

    public function client()     { return $this->belongsTo(Client::class); }
    public function assignedTo() { return $this->belongsTo(User::class, 'assigned_to'); }
    public function photos()     { return $this->hasMany(WorkOrderPhoto::class); }

    public static function generateNumber(): string
    {
        $year   = date('Y');
        $random = strtoupper(substr(uniqid(), -5));
        return "WO-{$year}-{$random}";
    }
}
