<?php

declare(strict_types=1);

namespace App\Models;

use CodeIgniter\Model;

class DocumentModel extends Model
{
    protected $table            = 'documents';
    protected $primaryKey       = 'id';
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'category_id',
        'title',
        'description',
        'type',
        'file_path',
        'file_size',
        'mime_type',
        'url',
        'content_text',
        'summary',
        'embedding_status',
        'visibility',
    ];
    protected $useTimestamps    = true;
    protected $createdField     = 'created_at';
    protected $updatedField     = 'updated_at';
    protected $deletedField     = 'deleted_at';
    protected $validationRules  = [];
    protected $validationMessages = [];
    protected $skipValidation     = false;

    public function getWithCategory(?int $id = null)
    {
        $builder = $this->builder();
        $builder->select('documents.*, categories.name as category_name');
        $builder->join('categories', 'categories.id = documents.category_id', 'left');

        if ($id !== null) {
            $builder->where('documents.id', $id);
            return $builder->get()->getRowArray();
        }

        return $builder->get()->getResultArray();
    }
}
