<?php

namespace App\Models;

use CodeIgniter\Model;

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

    protected $allowedFields = [
        'company_id', 'title', 'category', 'file_path', 'original_filename',
        'file_size', 'mime_type', 'notes', 'status', 'created_by', 'updated_by',
    ];

    protected $validationRules = [
        'company_id' => 'required|is_natural_no_zero',
        'title'      => 'required|min_length[2]|max_length[150]',
        'status'     => 'permit_empty|in_list[active,archived]',
    ];

    public function scopeForList(
        ?string $search = null,
        ?string $status = null,
        ?int $companyId = null,
        ?string $category = null
    ): self {
        if ($search) {
            $this->groupStart()
                ->like('title', $search)
                ->orLike('original_filename', $search)
                ->orLike('category', $search)
                ->groupEnd();
        }

        if ($status) {
            $this->where('status', $status);
        }

        if ($companyId) {
            $this->where('company_id', $companyId);
        }

        if ($category) {
            $this->where('category', $category);
        }

        return $this;
    }
}
