<?php
  
  namespace App\Models;
  
  use CodeIgniter\Model;
  
  class VendorCategoryModel extends Model
  {
    protected $table = 'vendor_categories';
    protected $primaryKey = 'id';
    protected $useAutoIncrement = true;
    protected $returnType = 'array';
    protected $useSoftDeletes = false;
    
    protected $allowedFields = [
      'code', 'name_ko', 'name_en', 'description', 'sort_order', 'is_active'
    ];
    
    protected $useTimestamps = true;
    protected $createdField = 'created_at';
    protected $updatedField = null;
    
    protected $validationRules = [
      'code' => 'required|max_length[50]|is_unique[vendor_categories.code,id,{id}]',
      'name_ko' => 'required|max_length[100]',
      'name_en' => 'permit_empty|max_length[100]',
      'description' => 'permit_empty|max_length[65535]',
      'sort_order' => 'permit_empty|integer',
      'is_active' => 'permit_empty|in_list[0,1]'
    ];
    
    // 활성화된 카테고리만 조회
    public function getActiveCategories()
    {
      return $this->where('is_active', 1)
        ->orderBy('sort_order', 'ASC')
        ->findAll();
    }
    
    // 코드로 카테고리 조회
    public function getCategoryByCode($code)
    {
      return $this->where('code', $code)->first();
    }
  }