<?php

namespace App\Models\Entities;

use CodeIgniter\Model;

class WatermarkModel extends Model
{
    protected $table = 'watermark';
    protected $primaryKey = ['cpid', 'wm_type']; // 복합키
    
    protected $useAutoIncrement = false;
    protected $returnType = 'array';
    protected $useSoftDeletes = false;
    
    protected $allowedFields = [
        'cpid',
        'wm_type',
        'wm_img_path',
        'wm_img_height',
        'wm_img_width',
        'wm_position',
        'wm_img_opacity',
        'wm_space',
        'text_font',
        'text_color',
        'text_opacity',
        'text_size',
        'text_pixel',
        'img_width_min',
        'img_width_max'
    ];
    
    protected $useTimestamps = false;
    
    protected $validationRules = [
        'cpid' => 'required|max_length[20]',
        'wm_type' => 'required|integer',
        'wm_img_path' => 'required|max_length[300]',
        'wm_img_height' => 'required|integer',
        'wm_img_width' => 'required|integer',
        'wm_position' => 'required|max_length[2]',
        'wm_img_opacity' => 'required|integer',
        'wm_space' => 'required|integer',
        'img_width_min' => 'required|integer',
        'img_width_max' => 'required|integer'
    ];
    
    protected $validationMessages = [];
    protected $skipValidation = false;
    
    /**
     * 워터마크 정보 조회 (복합키)
     * 
     * @param string $cpid
     * @param int $wm_type
     * @return array|null
     */
    public function getWatermark($cpid, $wm_type)
    {
        return $this->where('cpid', $cpid)
                    ->where('wm_type', $wm_type)
                    ->first();
    }
    
    /**
     * cpid로 워터마크 목록 조회
     * 
     * @param string $cpid
     * @return array
     */
    public function getWatermarksByCpid($cpid)
    {
        return $this->where('cpid', $cpid)->findAll();
    }
    
    /**
     * 워터마크 타입별 조회
     * 
     * @param int $wm_type
     * @return array
     */
    public function getWatermarksByType($wm_type)
    {
        return $this->where('wm_type', $wm_type)->findAll();
    }
    
    /**
     * 워터마크 정보 저장/업데이트
     * 
     * @param array $data
     * @return bool
     */
    public function saveWatermark($data)
    {
        if (!isset($data['cpid']) || !isset($data['wm_type'])) {
            return false;
        }
        
        // 복합키로 기존 데이터 확인
        $existing = $this->getWatermark($data['cpid'], $data['wm_type']);
        
        if ($existing) {
            // 업데이트
            return $this->where('cpid', $data['cpid'])
                        ->where('wm_type', $data['wm_type'])
                        ->set($data)
                        ->update();
        } else {
            // 삽입
            return $this->insert($data);
        }
    }
    
    /**
     * 워터마크 삭제 (복합키)
     * 
     * @param string $cpid
     * @param int $wm_type
     * @return bool
     */
    public function deleteWatermark($cpid, $wm_type)
    {
        return $this->where('cpid', $cpid)
                    ->where('wm_type', $wm_type)
                    ->delete();
    }
    
    /**
     * cpid로 모든 워터마크 삭제
     * 
     * @param string $cpid
     * @return bool
     */
    public function deleteAllByCpid($cpid)
    {
        return $this->where('cpid', $cpid)->delete();
    }
    
    /**
     * 이미지 크기 범위로 워터마크 조회
     * 
     * @param string $cpid
     * @param int $img_width
     * @return array|null
     */
    public function getWatermarkByImageSize($cpid, $img_width)
    {
        return $this->where('cpid', $cpid)
                    ->where('img_width_min <=', $img_width)
                    ->where('img_width_max >=', $img_width)
                    ->findAll();
    }
}
