<?php

namespace App\Models\Entities;

use CodeIgniter\Model;

/**
 * V2ModifyInfoModel
 * 
 * v2_modify_info 테이블 - 수정정보 모델
 * 부동산 매물의 수정된 정보를 저장하는 테이블
 */
class V2modifyinfoModel extends Model
{
    protected $table            = 'v2_modify_info';
    protected $primaryKey       = 'vr_sq';
    protected $useAutoIncrement = false;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'vr_sq',
        'bildNo',
        'bild_nm',
        'rm_no',
        'floor',
        'floor2',
        'ugrodFloor',
        'address_code',
        'address2',
        'address2a',
        'address2b',
        'address3',
        'address4',
        'trade_type',
        'deal_amt',
        'wrrnt_amt',
        'lease_amt',
        'isale_amt',
        'prem_amt',
        'sply_spc',
        'excls_spc',
        'tot_spc',
        'grnd_spc',
        'bldg_spc',
        'hscp_no',
        'hscp_nm',
        'ptp_no',
        'ptp_nm',
        'modify_yn',
    ];

    // 검증 규칙
    // protected $validationRules    = [
    //     'vr_sq'         => 'required|integer',
    //     'bild_nm'       => 'string|max_length[60]',
    //     'rm_no'         => 'string|max_length[30]',
    //     'floor'         => 'integer',
    //     'floor2'        => 'integer',
    //     'ugrodFloor'    => 'integer',
    //     'address_code'  => 'string|max_length[10]',
    //     'address2'      => 'string|max_length[300]',
    //     'address2a'     => 'string|max_length[300]',
    //     'address2b'     => 'string|max_length[300]',
    //     'address3'      => 'string|max_length[300]',
    //     'address4'      => 'string|max_length[1000]',
    //     'trade_type'    => 'string|max_length[2]',
    //     'deal_amt'      => 'integer',
    //     'wrrnt_amt'     => 'integer',
    //     'lease_amt'     => 'integer',
    //     'isale_amt'     => 'integer',
    //     'prem_amt'      => 'integer',
    //     'sply_spc'      => 'numeric',
    //     'excls_spc'     => 'numeric',
    //     'tot_spc'       => 'numeric',
    //     'grnd_spc'      => 'numeric',
    //     'bldg_spc'      => 'numeric',
    //     'hscp_no'       => 'string|max_length[30]',
    //     'hscp_nm'       => 'string|max_length[60]',
    //     'ptp_no'        => 'string|max_length[30]',
    //     'ptp_nm'        => 'string|max_length[60]',
    //     'modify_yn'     => 'in_list[Y,N]',
    // ];

    protected $validationMessages = [];
    protected $skipValidation     = false;
    protected $cleanValidationRules = true;

    // 콜백
    protected $allowCallbacks = true;
    protected $beforeInsert   = [];
    protected $afterInsert    = [];
    protected $beforeUpdate   = [];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];

    /**
     * 검증요청순번(vr_sq)으로 수정정보 조회
     */
    public function getByVrSq(int $vrSq): ?array
    {
        return $this->find($vrSq);
    }

    /**
     * 수정정보 저장 (없으면 insert, 있으면 update)
     * REPLACE INTO 사용으로 효율성 증대
     */
    public function saveModifyInfo(int $vrSq, array $data): bool
    {
        $data['vr_sq'] = $vrSq;
        return $this->replace($data);
    }

    /**
     * 수정여부가 Y인 데이터 조회
     */
    public function getModifiedInfo(): array
    {
        return $this->where('modify_yn', 'Y')->findAll();
    }

    /**
     * 특정 검증요청의 수정여부 업데이트
     */
    public function updateModifyYn(int $vrSq, string $yn = 'Y'): bool
    {
        return $this->update($vrSq, ['modify_yn' => $yn]);
    }
}
