-- ============================================================================
-- V005: 모선 워크플로우 (운영자 의사결정 - HYBRID)
-- iran 백엔드 마이그레이션 012/014의 백엔드 쓰기 부분만 이식
-- ============================================================================

-- ----------------------------------------------------------------------------
-- 모선 확정 결과 (운영자 액션 결과)
-- ----------------------------------------------------------------------------
CREATE TABLE kcg.gear_group_parent_resolution (
    id                          BIGSERIAL PRIMARY KEY,
    group_key                   VARCHAR(255) NOT NULL,
    sub_cluster_id              INT NOT NULL,
    status                      VARCHAR(30) NOT NULL,    -- UNRESOLVED, MANUAL_CONFIRMED, REVIEW_REQUIRED
    selected_parent_mmsi        VARCHAR(20),
    rejected_candidate_mmsi     VARCHAR(20),
    approved_by                 UUID,
    approved_at                 TIMESTAMPTZ,
    rejected_at                 TIMESTAMPTZ,
    manual_comment              TEXT,
    created_at                  TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at                  TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE(group_key, sub_cluster_id)
);

CREATE INDEX idx_parent_resolution_status ON kcg.gear_group_parent_resolution(status);
CREATE INDEX idx_parent_resolution_group ON kcg.gear_group_parent_resolution(group_key);

COMMENT ON TABLE kcg.gear_group_parent_resolution IS '모선 확정 결과 (HYBRID: prediction 후보 + 운영자 결정)';

-- ----------------------------------------------------------------------------
-- 운영자 액션 로그 (도메인 컨텍스트 보존, audit_log와 별개)
-- ----------------------------------------------------------------------------
CREATE TABLE kcg.gear_group_parent_review_log (
    id                          BIGSERIAL PRIMARY KEY,
    group_key                   VARCHAR(255) NOT NULL,
    sub_cluster_id              INT,
    action                      VARCHAR(30) NOT NULL,    -- CONFIRM, REJECT, RESET, EXCLUDE_GROUP, EXCLUDE_GLOBAL, LABEL_PARENT, CANCEL_LABEL, RELEASE_EXCLUSION
    selected_parent_mmsi        VARCHAR(20),
    actor                       UUID,
    actor_acnt                  VARCHAR(50),
    comment                     TEXT,
    created_at                  TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX idx_review_log_group ON kcg.gear_group_parent_review_log(group_key, created_at DESC);
CREATE INDEX idx_review_log_actor ON kcg.gear_group_parent_review_log(actor, created_at DESC);
CREATE INDEX idx_review_log_action ON kcg.gear_group_parent_review_log(action, created_at DESC);

COMMENT ON TABLE kcg.gear_group_parent_review_log IS '모선 워크플로우 운영자 액션 로그';

-- ----------------------------------------------------------------------------
-- 후보 제외 (운영자 또는 관리자가 잘못된 후보 차단)
-- ----------------------------------------------------------------------------
CREATE TABLE kcg.gear_parent_candidate_exclusions (
    id                          BIGSERIAL PRIMARY KEY,
    scope_type                  VARCHAR(20) NOT NULL,    -- GROUP, GLOBAL
    group_key                   VARCHAR(255),            -- GLOBAL일 때는 NULL
    sub_cluster_id              INT,
    excluded_mmsi               VARCHAR(20) NOT NULL,
    reason                      TEXT,
    actor                       UUID,
    actor_acnt                  VARCHAR(50),
    created_at                  TIMESTAMPTZ NOT NULL DEFAULT now(),
    released_at                 TIMESTAMPTZ,
    released_by                 UUID,
    released_by_acnt            VARCHAR(50)
);

CREATE INDEX idx_exclusion_scope ON kcg.gear_parent_candidate_exclusions(scope_type, group_key, excluded_mmsi);
CREATE INDEX idx_exclusion_active ON kcg.gear_parent_candidate_exclusions(scope_type, released_at) WHERE released_at IS NULL;

COMMENT ON TABLE kcg.gear_parent_candidate_exclusions IS '모선 후보 제외 (그룹/전역 스코프)';

-- ----------------------------------------------------------------------------
-- 학습 세션 (운영자가 정답 라벨링)
-- ----------------------------------------------------------------------------
CREATE TABLE kcg.gear_parent_label_sessions (
    id                          BIGSERIAL PRIMARY KEY,
    group_key                   VARCHAR(255) NOT NULL,
    sub_cluster_id              INT NOT NULL,
    label_parent_mmsi           VARCHAR(20) NOT NULL,
    status                      VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',  -- ACTIVE, CANCELLED, COMPLETED
    active_from                 TIMESTAMPTZ NOT NULL DEFAULT now(),
    active_until                TIMESTAMPTZ,
    anchor_snapshot             JSONB,
    created_by                  UUID,
    created_by_acnt             VARCHAR(50),
    cancelled_by                UUID,
    cancelled_at                TIMESTAMPTZ,
    cancel_reason               TEXT,
    created_at                  TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX idx_label_session_group ON kcg.gear_parent_label_sessions(group_key, status);
CREATE INDEX idx_label_session_status ON kcg.gear_parent_label_sessions(status);

COMMENT ON TABLE kcg.gear_parent_label_sessions IS '모선 추론 학습 세션 (운영자 정답 라벨링)';
