<?php

namespace Kylin\LawCase\Jobs;

use App\Models\User;
use Kylin\LawCase\Models\Schedule;
use Kylin\LawCase\Notifications\ScheduleReminderNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

/**
 * 日程提醒 Job
 *
 * 异步发送日程提醒通知。
 */
class SendScheduleReminderJob implements ShouldQueue
{
    use InteractsWithQueue, Queueable;

    public int $tries = 3;

    public function __construct(
        public User $user,
        public Schedule $schedule,
        public int $daysBefore
    ) {}

    public function handle(): void
    {
        $dayText = $this->daysBefore === 1 ? '明天' : "{$this->daysBefore}天后";
        Log::info("日程提醒: 用户{$this->user->id}, {$dayText}有日程：{$this->schedule->title}");

        // 发送站内信通知
        $this->user->notify(new ScheduleReminderNotification($this->schedule->id, $this->daysBefore));
    }
}
