-- V016: parent workflow 관련 테이블에 prediction 코드가 요구하는 누락 컬럼 추가
-- gear_parent_inference.py, gear_parent_episode.py가 참조하는 컬럼들

-- === gear_parent_label_sessions ===
ALTER TABLE kcg.gear_parent_label_sessions
    ADD COLUMN IF NOT EXISTS label_parent_name VARCHAR(200),
    ADD COLUMN IF NOT EXISTS label_parent_vessel_id BIGINT,
    ADD COLUMN IF NOT EXISTS normalized_parent_name VARCHAR(100),
    ADD COLUMN IF NOT EXISTS duration_days INT DEFAULT 3,
    ADD COLUMN IF NOT EXISTS actor VARCHAR(100),
    ADD COLUMN IF NOT EXISTS comment TEXT,
    ADD COLUMN IF NOT EXISTS metadata JSONB,
    ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ DEFAULT now();

CREATE INDEX IF NOT EXISTS idx_label_session_norm
    ON kcg.gear_parent_label_sessions(normalized_parent_name, active_from DESC)
    WHERE status = 'ACTIVE';

-- === gear_group_parent_resolution ===
ALTER TABLE kcg.gear_group_parent_resolution
    ADD COLUMN IF NOT EXISTS parent_name VARCHAR(200),
    ADD COLUMN IF NOT EXISTS normalized_parent_name VARCHAR(100),
    ADD COLUMN IF NOT EXISTS selected_parent_name VARCHAR(200),
    ADD COLUMN IF NOT EXISTS selected_vessel_id BIGINT,
    ADD COLUMN IF NOT EXISTS confidence NUMERIC(7,4),
    ADD COLUMN IF NOT EXISTS decision_source VARCHAR(30),
    ADD COLUMN IF NOT EXISTS top_score NUMERIC(7,4),
    ADD COLUMN IF NOT EXISTS second_score NUMERIC(7,4),
    ADD COLUMN IF NOT EXISTS score_margin NUMERIC(7,4),
    ADD COLUMN IF NOT EXISTS stable_cycles INT DEFAULT 0,
    ADD COLUMN IF NOT EXISTS evidence_summary JSONB,
    ADD COLUMN IF NOT EXISTS episode_id VARCHAR(50),
    ADD COLUMN IF NOT EXISTS continuity_source VARCHAR(30),
    ADD COLUMN IF NOT EXISTS continuity_score NUMERIC(7,4),
    ADD COLUMN IF NOT EXISTS prior_bonus_total NUMERIC(7,4);

CREATE INDEX IF NOT EXISTS idx_parent_resolution_episode
    ON kcg.gear_group_parent_resolution(episode_id);

-- === gear_parent_candidate_exclusions ===
-- gear_parent_inference.py가 참조하는 추가 컬럼
-- 참고: 코드는 candidate_mmsi를 쿼리 — excluded_mmsi의 generated column으로 매핑
ALTER TABLE kcg.gear_parent_candidate_exclusions
    ADD COLUMN IF NOT EXISTS normalized_parent_name VARCHAR(100),
    ADD COLUMN IF NOT EXISTS reason_type VARCHAR(50),
    ADD COLUMN IF NOT EXISTS duration_days INT,
    ADD COLUMN IF NOT EXISTS metadata JSONB,
    ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ DEFAULT now(),
    ADD COLUMN IF NOT EXISTS active_from TIMESTAMPTZ DEFAULT now(),
    ADD COLUMN IF NOT EXISTS active_until TIMESTAMPTZ;

-- candidate_mmsi 별칭 (generated column)
ALTER TABLE kcg.gear_parent_candidate_exclusions
    ADD COLUMN IF NOT EXISTS candidate_mmsi VARCHAR(20)
    GENERATED ALWAYS AS (excluded_mmsi) STORED;

-- === gear_group_parent_resolution 추가 타임스탬프 컬럼 ===
ALTER TABLE kcg.gear_group_parent_resolution
    ADD COLUMN IF NOT EXISTS last_evaluated_at TIMESTAMPTZ,
    ADD COLUMN IF NOT EXISTS last_promoted_at TIMESTAMPTZ;

-- === gear_group_parent_candidate_snapshots ===
ALTER TABLE kcg.gear_group_parent_candidate_snapshots
    ADD COLUMN IF NOT EXISTS parent_name VARCHAR(200);
