-- V97: 단계마감 (settlement_step_close)
--      월 정산을 단계별로 마감(수신→계산→검증→지급). 기존 PERIOD_CLOSE 보강.

CREATE TABLE settlement_step_close (
    step_close_id  BIGSERIAL     PRIMARY KEY,
    settle_month   CHAR(6)       NOT NULL,
    step_code      VARCHAR(20)   NOT NULL,
    step_seq       INT           NOT NULL,
    status         VARCHAR(20)   NOT NULL DEFAULT 'OPEN',
    closed_at      TIMESTAMP,
    closed_by      BIGINT,
    created_at     TIMESTAMP     NOT NULL DEFAULT NOW(),
    UNIQUE (settle_month, step_code),
    CHECK (step_code IN ('RECEIVE','CALC','VERIFY','PAY')),
    CHECK (status IN ('OPEN','CLOSED'))
);

COMMENT ON TABLE  settlement_step_close            IS '단계마감 — 월 정산 단계별 마감 상태 관리';
COMMENT ON COLUMN settlement_step_close.settle_month IS '정산 귀속월 (YYYYMM)';
COMMENT ON COLUMN settlement_step_close.step_code    IS '단계코드 (RECEIVE=수신/CALC=계산/VERIFY=검증/PAY=지급)';
COMMENT ON COLUMN settlement_step_close.step_seq     IS '단계 순서 1~4';
COMMENT ON COLUMN settlement_step_close.status       IS '상태 (OPEN=열림/CLOSED=마감)';
COMMENT ON COLUMN settlement_step_close.closed_at    IS '마감 처리 일시';
COMMENT ON COLUMN settlement_step_close.closed_by    IS '마감 처리자 FK(users)';

CREATE INDEX idx_ssc_month_status ON settlement_step_close(settle_month, status);
CREATE INDEX idx_ssc_step_seq     ON settlement_step_close(step_seq);
