<?php

namespace App\Console\Commands;

use App\Models\Task;
use App\Notifications\TaskDueSoonNotification;
use Illuminate\Console\Command;

class NotifyDueTasksCommand extends Command
{
    protected $signature = 'tasks:notify-due';

    protected $description = 'Notify assignees about tasks due within the next 24 hours';

    public function handle(): int
    {
        $tasks = Task::withoutGlobalScope('company')
            ->whereNull('completed_at')
            ->whereNotNull('due_at')
            ->whereBetween('due_at', [now(), now()->addDay()])
            ->with('assignee')
            ->get();

        foreach ($tasks as $task) {
            if ($task->assignee) {
                $task->assignee->notify(new TaskDueSoonNotification($task));
            }
        }

        $this->info('Notified for '.$tasks->count().' tasks.');

        return self::SUCCESS;
    }
}
