<?php

namespace Kylin\LawCase\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kylin\LawCase\Enums\ScheduleType;

/**
 * 日程模型
 *
 * 管理案件相关日程安排，包括开庭、会议、截止日期等。
 *
 * @property string $schedule_type 日程类型
 * @property string $title         日程标题
 * @property string $scheduled_at  安排时间
 * @property string $reminder_time 提醒时间
 */
class Schedule extends Model
{
    use SoftDeletes;

    protected $table = 'kylin_law_schedules';

    protected $fillable = [
        'schedule_type',
        'title',
        'case_id',
        'user_id',
        'start_time',
        'end_time',
        'reminder_type',
        'reminder_before',
        'location',
        'description',
    ];

    protected $casts = [
        'schedule_type' => ScheduleType::class,
        'start_time' => 'datetime',
        'end_time' => 'datetime',
        'reminder_before' => 'integer',
    ];

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

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

    public function scopeByDateRange($query, $startDate, $endDate)
    {
        return $query->whereBetween('start_time', [$startDate, $endDate]);
    }

    public function scopeByType($query, ScheduleType $type)
    {
        return $query->where('schedule_type', $type->value);
    }

    public function scopeUpcoming($query)
    {
        return $query->where('start_time', '>=', now())->orderBy('start_time');
    }
}
