-- ============================================================
-- V011: vessel_permit_master + patrol_ship_master + fleet_companies + 시드
-- ============================================================

-- ============================================================
-- 선단 회사 (중국 선단 회사 레지스트리)
-- ============================================================
CREATE TABLE kcg.fleet_companies (
    id          BIGSERIAL    PRIMARY KEY,
    name_cn     VARCHAR(200),
    name_en     VARCHAR(200),
    name_ko     VARCHAR(200),
    country     VARCHAR(10)  DEFAULT 'CN',
    is_active   BOOLEAN      DEFAULT true,
    created_at  TIMESTAMPTZ  DEFAULT now()
);

-- ============================================================
-- 어선 허가/등록 마스터
-- ============================================================
CREATE TABLE kcg.vessel_permit_master (
    mmsi                VARCHAR(20)  PRIMARY KEY,
    vessel_name         VARCHAR(100),
    vessel_name_cn      VARCHAR(100),
    flag_country        VARCHAR(10),                -- KR, CN, JP, RU, UNKNOWN
    vessel_type         VARCHAR(30),                -- FISHING, CARGO, TANKER, PATROL, UNKNOWN
    tonnage             NUMERIC(10,2),
    length_m            NUMERIC(6,2),
    build_year          INT,
    -- 허가 상태
    permit_status       VARCHAR(20)  DEFAULT 'UNKNOWN',   -- PERMITTED, EXPIRED, NONE, REVOKED, UNKNOWN
    permit_no           VARCHAR(50),
    permitted_gear_codes TEXT[],                    -- gear_type_master.gear_code 참조
    permitted_zones     TEXT[],                     -- zone_polygon_master.zone_code 참조
    permit_valid_from   DATE,
    permit_valid_to     DATE,
    -- 소속
    company_id          BIGINT       REFERENCES kcg.fleet_companies(id),
    -- 메타
    data_source         VARCHAR(50)  DEFAULT 'MANUAL',    -- KR_FISHERIES, CHINA_REGISTRY, IRAN_REGISTRY, MANUAL
    last_synced_at      TIMESTAMPTZ,
    updated_at          TIMESTAMPTZ  DEFAULT now()
);

CREATE INDEX idx_vessel_flag    ON kcg.vessel_permit_master(flag_country);
CREATE INDEX idx_vessel_permit  ON kcg.vessel_permit_master(permit_status);
CREATE INDEX idx_vessel_company ON kcg.vessel_permit_master(company_id);

-- ============================================================
-- 함정 마스터
-- ============================================================
CREATE TABLE kcg.patrol_ship_master (
    ship_id         BIGSERIAL    PRIMARY KEY,
    ship_code       VARCHAR(20)  UNIQUE NOT NULL,       -- '3001함'
    ship_name       VARCHAR(100),
    ship_class      VARCHAR(50),                        -- 태극급, 참수리급, 삼봉급
    tonnage         NUMERIC(10,2),
    max_speed_kn    NUMERIC(5,2),
    fuel_capacity_l NUMERIC(10,2),
    base_port       VARCHAR(50),
    -- 현재 상태 (실시간 갱신)
    current_status  VARCHAR(20)  DEFAULT 'STANDBY',     -- code_master PATROL_STATUS 참조
    current_lat     DOUBLE PRECISION,
    current_lon     DOUBLE PRECISION,
    current_zone_code VARCHAR(30),                      -- zone_polygon_master FK
    fuel_pct        INT,
    crew_count      INT,
    -- 메타
    is_active       BOOLEAN      DEFAULT true,
    updated_at      TIMESTAMPTZ  DEFAULT now()
);

-- ============================================================
-- 시드: 프론트엔드 더미 기반 선박 (9척)
-- ============================================================
INSERT INTO kcg.fleet_companies (id, name_cn, name_en, name_ko, country) VALUES
(1, '荣成远洋渔业', 'Rongcheng Ocean Fishery', '영성원양어업', 'CN'),
(2, '大连海洋', 'Dalian Ocean', '대련해양', 'CN');
SELECT setval('kcg.fleet_companies_id_seq', 10);

INSERT INTO kcg.vessel_permit_master (mmsi, vessel_name, vessel_name_cn, flag_country, vessel_type,
    tonnage, permit_status, permitted_gear_codes, company_id, data_source) VALUES
('412345678', '鲁荣渔56555',  '鲁荣渔56555',  'CN', 'FISHING', 450.0, 'NONE',    '{TRAWL_BOTTOM}', 1, 'IRAN_REGISTRY'),
('412345679', '鲁荣渔56556',  '鲁荣渔56556',  'CN', 'FISHING', 380.0, 'EXPIRED', '{GILLNET}',      1, 'IRAN_REGISTRY'),
('412345680', '辽大渔42881',  '辽大渔42881',  'CN', 'FISHING', 520.0, 'NONE',    '{PURSE_SEINE}',  2, 'IRAN_REGISTRY'),
('412345681', '浙象渔23166',  '浙象渔23166',  'CN', 'FISHING', 290.0, 'NONE',    '{LONGLINE}',     NULL, 'IRAN_REGISTRY'),
('412345682', '闽霞渔09876',  '闽霞渔09876',  'CN', 'FISHING', 410.0, 'NONE',    '{TRAP}',         NULL, 'IRAN_REGISTRY'),
('412345683', '苏赣渔05512',  '苏赣渔05512',  'CN', 'FISHING', 350.0, 'NONE',    '{GILLNET}',      NULL, 'IRAN_REGISTRY'),
('440012345', '제주해양호',   NULL,            'KR', 'FISHING', 85.0,  'PERMITTED','{LONGLINE,TRAP}',NULL, 'KR_FISHERIES'),
('440012346', '통영수산호',   NULL,            'KR', 'FISHING', 120.0, 'PERMITTED','{GILLNET}',      NULL, 'KR_FISHERIES'),
('000000001', 'Unknown-001',  NULL,            'UNKNOWN','UNKNOWN',NULL,'UNKNOWN',  NULL,             NULL, 'MANUAL');

-- ============================================================
-- 시드: 함정 6척 (프론트엔드 patrolStore 기반)
-- ============================================================
INSERT INTO kcg.patrol_ship_master (ship_code, ship_name, ship_class, tonnage, max_speed_kn,
    fuel_capacity_l, base_port, current_status, current_lat, current_lon,
    current_zone_code, fuel_pct, crew_count) VALUES
('3001',  '3001함', '태극급',    3000.0, 30.0, 500000, '인천',   'IN_PURSUIT',  37.52, 124.78, 'NLL',               78, 120),
('3005',  '3005함', '삼봉급',    1500.0, 25.0, 300000, '목포',   'ON_PATROL',   34.85, 125.42, 'SPECIAL_FISHING_1', 92, 80),
('3009',  '3009함', '참수리급',   500.0, 35.0, 100000, '속초',   'AVAILABLE',   38.12, 128.95, NULL,                100, 35),
('5001',  '5001함', '태극급',    3000.0, 30.0, 500000, '부산',   'ON_PATROL',   34.42, 129.38, 'SPECIAL_FISHING_3', 65, 120),
('1502',  '1502함', '참수리급',   500.0, 35.0, 100000, '인천',   'INSPECTING',  37.38, 124.55, 'WEST_5_ISLANDS',    45, 35),
('2003',  '2003함', '삼봉급',    1500.0, 25.0, 300000, '서귀포', 'STANDBY',     33.15, 126.58, NULL,                88, 80);
