<?php

namespace Kylin\LawCase\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 任务模型
 *
 * 管理案件相关任务，包括任务分配、优先级、截止日期等。
 *
 * @property string $title       任务标题
 * @property string $priority    优先级
 * @property string $due_date    截止日期
 * @property string $status      任务状态
 */
class Task extends Model
{
    use SoftDeletes;

    protected $table = 'kylin_law_tasks';

    protected $fillable = [
        'case_id',
        'title',
        'description',
        'assigned_to',
        'assigned_by',
        'priority',
        'status',
        'due_date',
        'completed_at',
    ];

    protected $casts = [
        'due_date' => 'date',
        'completed_at' => 'datetime',
    ];

    public function case()
    {
        return $this->belongsTo(CaseModel::class);
    }

    public function assignee()
    {
        return $this->belongsTo(\App\Models\User::class, 'assigned_to');
    }

    public function assigner()
    {
        return $this->belongsTo(\App\Models\User::class, 'assigned_by');
    }

    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopeOverdue($query)
    {
        return $query->where('status', '!=', 'completed')
            ->where('due_date', '<', now()->toDateString());
    }

    public function scopeByPriority($query, string $priority)
    {
        return $query->where('priority', $priority);
    }
}
