-- ============================================================
-- V010: zone_polygon_master (해역 폴리곤 마스터)
-- PostGIS GEOMETRY 사용, MapControl 화면에서 관리자가 편집
-- prediction location.py가 매 사이클 참조
-- ============================================================

CREATE TABLE kcg.zone_polygon_master (
    zone_code           VARCHAR(30)  PRIMARY KEY,
    zone_name_ko        VARCHAR(100) NOT NULL,
    zone_name_en        VARCHAR(100),
    zone_type           VARCHAR(30)  NOT NULL,      -- TERRITORIAL, EEZ, SPECIAL_FISHING, NLL, BUFFER, PROHIBITED, PATROL_SECTOR
    parent_zone_code    VARCHAR(30)  REFERENCES kcg.zone_polygon_master(zone_code),
    polygon_geom        GEOMETRY(MULTIPOLYGON, 4326),
    -- 단속 정책
    baseline_distance_nm NUMERIC(8,2),
    enforcement_priority INT         DEFAULT 5,     -- 1=최우선
    default_risk_level   VARCHAR(20),               -- 진입만으로 부여되는 기본 위험 레벨
    -- 어구 정책
    allowed_gear_codes  TEXT[],
    prohibited_gear_codes TEXT[],
    -- 표시
    display_color       VARCHAR(7),
    display_opacity     NUMERIC(3,2) DEFAULT 0.3,
    display_order       INT          DEFAULT 0,
    description         TEXT,
    metadata            JSONB,                      -- 관할서, 면적, 기타
    is_active           BOOLEAN      DEFAULT true,
    created_by          UUID,
    updated_at          TIMESTAMPTZ  DEFAULT now()
);

CREATE INDEX idx_zone_geom   ON kcg.zone_polygon_master USING GIST(polygon_geom);
CREATE INDEX idx_zone_type   ON kcg.zone_polygon_master(zone_type);
CREATE INDEX idx_zone_parent ON kcg.zone_polygon_master(parent_zone_code);

-- ============================================================
-- 시드: 주요 한국 해역 (간략 폴리곤 — 향후 정밀 GeoJSON import)
-- prediction의 data/zones/ GeoJSON이 정밀 데이터 소스
-- ============================================================

-- 한국 영해 (12해리, 간략 바운딩)
INSERT INTO kcg.zone_polygon_master (zone_code, zone_name_ko, zone_name_en, zone_type,
    enforcement_priority, default_risk_level, display_color, display_opacity, display_order, description)
VALUES
('TERRITORIAL_SEA', '영해', 'Territorial Sea', 'TERRITORIAL',
    3, NULL, '#3B82F6', 0.15, 10, '기선으로부터 12해리 이내');

-- 한국 EEZ
INSERT INTO kcg.zone_polygon_master (zone_code, zone_name_ko, zone_name_en, zone_type,
    enforcement_priority, default_risk_level, display_color, display_opacity, display_order, description)
VALUES
('EEZ_KR', '한국 EEZ', 'Korea EEZ', 'EEZ',
    2, 'LOW', '#06B6D4', 0.1, 20, '한국 배타적 경제수역');

-- NLL
INSERT INTO kcg.zone_polygon_master (zone_code, zone_name_ko, zone_name_en, zone_type,
    enforcement_priority, default_risk_level, display_color, display_opacity, display_order, description)
VALUES
('NLL', '북방한계선', 'Northern Limit Line', 'NLL',
    1, 'CRITICAL', '#EF4444', 0.3, 5, '서해 북방한계선. 최우선 감시 구역');

-- 특정어업수역 Ⅰ~Ⅳ (iran prediction의 zones/ GeoJSON 참조)
INSERT INTO kcg.zone_polygon_master (zone_code, zone_name_ko, zone_name_en, zone_type,
    parent_zone_code, enforcement_priority, default_risk_level,
    display_color, display_opacity, display_order, description) VALUES
('SPECIAL_FISHING_1', '특정어업수역 Ⅰ', 'Special Fishing Zone 1', 'SPECIAL_FISHING',
    'EEZ_KR', 2, 'HIGH', '#F59E0B', 0.25, 30, '한중 잠정조치수역 인접'),
('SPECIAL_FISHING_2', '특정어업수역 Ⅱ', 'Special Fishing Zone 2', 'SPECIAL_FISHING',
    'EEZ_KR', 2, 'HIGH', '#F97316', 0.25, 31, '서해 중부 어업 수역'),
('SPECIAL_FISHING_3', '특정어업수역 Ⅲ', 'Special Fishing Zone 3', 'SPECIAL_FISHING',
    'EEZ_KR', 3, 'MEDIUM', '#8B5CF6', 0.2, 32, '남해 어업 수역'),
('SPECIAL_FISHING_4', '특정어업수역 Ⅳ', 'Special Fishing Zone 4', 'SPECIAL_FISHING',
    'EEZ_KR', 3, 'MEDIUM', '#EC4899', 0.2, 33, '동해 어업 수역');

-- 서해 5도 (patrol store 더미 참조)
INSERT INTO kcg.zone_polygon_master (zone_code, zone_name_ko, zone_name_en, zone_type,
    enforcement_priority, default_risk_level, display_color, display_opacity, display_order, description)
VALUES
('WEST_5_ISLANDS', '서해 5도', 'West Sea 5 Islands', 'PATROL_SECTOR',
    1, 'HIGH', '#EF4444', 0.2, 6, '백령도/대청도/소청도/연평도/우도 인근');
