<?php

namespace App\Commands;

use App\Services\NotificationService;
use App\Support\CommandLock;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;

class GenerateTaskNotifications extends BaseCommand
{
    protected $group       = 'Tasks';
    protected $name        = 'tasks:notifications';
    protected $description = 'Create due in-app notifications and skip duplicates.';

    public function run(array $params)
    {
        return CommandLock::run('tasks-notifications', static function (): int {
            $created = (new NotificationService())->generateDueNotifications();
            if ($created === 0) {
                CLI::write('No due notifications to create.', 'white');
            } else {
                CLI::write(
                    sprintf('Created %d due notification(s).', $created),
                    'green',
                );
            }

            return EXIT_SUCCESS;
        });
    }
}
