<?php
  
  namespace App\Models;
  
  use CodeIgniter\Model;
  
  class VendorProductModel extends Model
  {
    protected $table = 'vendor_products';
    protected $primaryKey = 'id';
    protected $useAutoIncrement = true;
    protected $returnType = 'array';
    protected $useSoftDeletes = true;
    
    protected $allowedFields = [
      'vendor_id', 'name', 'description', 'category',
      'price', 'image_url', 'is_featured', 'status'
    ];
    
    protected $useTimestamps = true;
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';
    protected $deletedField = 'deleted_at';
    
    protected $validationRules = [
      'vendor_id' => 'required|integer|is_not_unique[vendors.id]',
      'name' => 'required|max_length[255]',
      'description' => 'permit_empty|max_length[65535]',
      'category' => 'permit_empty|max_length[100]',
      'price' => 'permit_empty|decimal|greater_than_equal_to[0]',
      'image_url' => 'permit_empty|max_length[500]|valid_url',
      'is_featured' => 'permit_empty|in_list[0,1]',
      'status' => 'required|in_list[ACTIVE,INACTIVE,DISCONTINUED]'
    ];
    
    protected $validationMessages = [
      'vendor_id' => [
        'required' => '벤더사 id는 필수입니다.',
        'is_not_unique' => '존재하지 않는 벤더사입니다.'
      ],
      'name' => [
        'required' => '제품명은 필수입니다.'
      ],
      'price' => [
        'greater_than_equal_to' => '가격은 0 이상이어야 합니다.'
      ],
      'image_url' => [
        'valid_url' => '유효하지 않은 이미지 URL입니다.'
      ]
    ];
    
    // 벤더사별 주요 제품 조회
    public function getFeaturedProducts($vendorId, $limit = 5)
    {
      return $this->where('vendor_id', $vendorId)
        ->where('status', 'ACTIVE')
        ->where('is_featured', 1)
        ->limit($limit)
        ->orderBy('created_at', 'DESC')
        ->findAll();
    }
  }