<?php

namespace App\Models;

use CodeIgniter\Model;

class OperationsTaskChecklistItemModel extends Model
{
    protected $table         = 'operations_task_checklist_items';
    protected $primaryKey    = 'id';
    protected $returnType    = 'array';
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';

    protected $allowedFields = [
        'task_id', 'title', 'is_done', 'sort_order',
    ];

    /**
     * @return list<array>
     */
    public function getForTask(int $taskId): array
    {
        return $this->where('task_id', $taskId)
            ->orderBy('sort_order', 'ASC')
            ->orderBy('id', 'ASC')
            ->findAll();
    }

    public function nextSortOrder(int $taskId): int
    {
        $row = $this->selectMax('sort_order')
            ->where('task_id', $taskId)
            ->first();

        return (int) ($row['sort_order'] ?? 0) + 1;
    }
}
