-- V026: 한중어업협정 중국어선 허가현황 원본 테이블 + fleet_vessels 연도 컬럼
-- 출처: docs/중국어선_허가현황_YYYYMMDD.xls (연단위 갱신)

-- ===== 1. fishery_permit_cn : 허가현황 원본 스냅샷 =====
CREATE TABLE IF NOT EXISTS kcg.fishery_permit_cn (
    id BIGSERIAL PRIMARY KEY,
    permit_year INTEGER NOT NULL,
    permit_no VARCHAR(30) NOT NULL,
    fishery_type VARCHAR(60),                -- 업종 (2척식저인망어업 등)
    fishery_code VARCHAR(10) NOT NULL,       -- 업종코드 (PT/PT-S/GN/FC/PS/OT)
    name_cn VARCHAR(100) NOT NULL,
    name_en VARCHAR(200) NOT NULL,
    applicant_cn VARCHAR(100),
    applicant_en VARCHAR(200),
    applicant_addr_cn VARCHAR(300),
    applicant_addr_en VARCHAR(300),
    registration_no VARCHAR(100),
    tonnage NUMERIC(10,2),
    port_cn VARCHAR(100),
    port_en VARCHAR(200),
    callsign VARCHAR(40),
    engine_power NUMERIC(10,2),
    length_m NUMERIC(6,2),
    beam_m NUMERIC(6,2),
    depth_m NUMERIC(6,2),
    fishing_zones VARCHAR(30),               -- Ⅱ,Ⅲ 등
    fishing_period_1 VARCHAR(50),
    fishing_period_2 VARCHAR(50),
    catch_quota_t NUMERIC(10,2),
    cumulative_quota_t NUMERIC(10,2),
    refrig_hold_count INTEGER,
    freezer_hold_count INTEGER,
    admin_sanction TEXT,
    parent_permit_no VARCHAR(30),            -- 부속선(PT-S)이 참조하는 본선 허가번호
    volume_enclosed NUMERIC(10,2),
    volume_above_deck NUMERIC(10,2),
    volume_below_deck NUMERIC(10,2),
    volume_excluded NUMERIC(10,2),
    raw_data JSONB,
    source_file VARCHAR(255),
    loaded_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE (permit_year, permit_no)
);

CREATE INDEX idx_fishery_permit_cn_name_cn ON kcg.fishery_permit_cn(permit_year, name_cn);
CREATE INDEX idx_fishery_permit_cn_name_en ON kcg.fishery_permit_cn(permit_year, LOWER(name_en));
CREATE INDEX idx_fishery_permit_cn_code ON kcg.fishery_permit_cn(permit_year, fishery_code);
CREATE INDEX idx_fishery_permit_cn_parent ON kcg.fishery_permit_cn(permit_year, parent_permit_no);

COMMENT ON TABLE kcg.fishery_permit_cn IS '한중어업협정 중국어선 허가현황 원본 스냅샷 (연단위 갱신)';
COMMENT ON COLUMN kcg.fishery_permit_cn.permit_year IS '허가 연도 (파일명 YYYY에서 추출)';
COMMENT ON COLUMN kcg.fishery_permit_cn.fishery_code IS 'PT(쌍끌이 본선)/PT-S(부속선)/GN(자망)/FC(운반)/PS(선망)/OT(외끌이)';
COMMENT ON COLUMN kcg.fishery_permit_cn.parent_permit_no IS 'PT-S(부속선)가 소속된 본선의 허가번호';

-- ===== 2. fleet_vessels 확장 : 연도 + 업종코드 추적 =====
ALTER TABLE kcg.fleet_vessels ADD COLUMN IF NOT EXISTS permit_year INTEGER;
ALTER TABLE kcg.fleet_vessels ADD COLUMN IF NOT EXISTS fishery_code VARCHAR(10);

CREATE INDEX IF NOT EXISTS idx_fleet_vessels_permit_year ON kcg.fleet_vessels(permit_year);
CREATE INDEX IF NOT EXISTS idx_fleet_vessels_fishery_code ON kcg.fleet_vessels(fishery_code);

COMMENT ON COLUMN kcg.fleet_vessels.permit_year IS '허가 연도. fleet_tracker는 현재 연도만 조회';
COMMENT ON COLUMN kcg.fleet_vessels.fishery_code IS 'fishery_permit_cn.fishery_code 복제 (PT/PT-S/GN/FC/PS/OT)';

-- ===== 3. V014 데모 seed 제거 =====
-- 기존 6행 데모 vessels 제거 (실제 허가현황만 남김).
-- fleet_companies id=1,2는 vessel_permit_master가 FK로 참조하여 삭제 불가 — 잔존 허용
-- (loader 실행 시 실 허가 신청인 회사가 별도 id로 upsert됨)
DELETE FROM kcg.fleet_vessels WHERE permit_no IN (
    'ZY-2024-001','ZY-2024-002','ZY-2024-003',
    'ZY-2024-010','ZY-2024-011','ZY-2024-012'
);
