<?php

namespace App\Models;

use App\Models\User\User;
use App\Trait\BelongsToSolicitor;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class WorkboardItem extends Model
{
    use BelongsToSolicitor, SoftDeletes;

    protected $fillable = [
        'solicitor_id',
        'workboard_category_id',
        'owner_type',
        'owner_id',
        'title',
        'description',
        'due_at',
        'claimed_by_user_id',
        'claimed_at',
        'completed_at',
    ];

    protected $casts = [
        'due_at'      => 'datetime',
        'claimed_at'  => 'datetime',
        'completed_at' => 'datetime',
    ];

    protected static function boot(): void
    {
        parent::boot();

        static::addGlobalScope('incomplete', function (Builder $builder) {
            $builder->whereNull('completed_at');
        });
    }

    public function category()
    {
        return $this->belongsTo(WorkboardCategory::class, 'workboard_category_id');
    }

    public function claimedBy()
    {
        return $this->belongsTo(User::class, 'claimed_by_user_id');
    }

    public function owner()
    {
        return $this->morphTo();
    }
}
