<?php

namespace App\Models;

use CodeIgniter\Model;

class TaskModel extends Model
{
    protected $table            = 'tasks';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = true;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'user_id',
        'category_id',
        'title',
        'priority',
        'due_date',
        'due_time',
        'is_recurring',
        'recurrence_ends_at',
    ];
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    public function findOwned(int $userId, int $taskId): ?array
    {
        $row = $this->where('user_id', $userId)->find($taskId);

        return $row === null ? null : $row;
    }

    public function countForCategory(int $userId, int $categoryId): int
    {
        return $this->where('user_id', $userId)
            ->where('category_id', $categoryId)
            ->countAllResults();
    }
}
