-- st 모듈 (정산/수수료) 상세 테이블. Prefix 35 (30-rc 다음).
-- ECPG 서버/배치가 참조하는 정산 상세/수수료율/조정/지급/가맹점집계 테이블 + 시퀀스.

-- ---------------------------------------------------------------------------
-- 정산 상세 (st_settle_dtl) - 수수료 종류별 정산 라인
-- ---------------------------------------------------------------------------
CREATE SEQUENCE IF NOT EXISTS st_dtl_seq;
CREATE TABLE IF NOT EXISTS st_settle_dtl (
    dtl_id        BIGINT PRIMARY KEY,
    settlement_id BIGINT NOT NULL DEFAULT 0,
    purchase_id   BIGINT NOT NULL DEFAULT 0,
    merchant_id   TEXT NOT NULL DEFAULT '',
    fee_type      TEXT NOT NULL,                 -- MDR/VAN/RELAY/WHT/VAT/ADJ/DELAYINT/CAP/RATE
    amount        BIGINT NOT NULL DEFAULT 0,
    biz_date      DATE NOT NULL,
    created_at    TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 수수료 요율표 (st_fee_rate) - 구간별 요율 (basis points)
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS st_fee_rate (
    fee_type     TEXT NOT NULL,
    band_min     BIGINT NOT NULL DEFAULT 0,      -- 이 금액 이상에 적용
    rate_bps     BIGINT NOT NULL DEFAULT 0,
    PRIMARY KEY (fee_type, band_min)
);

-- ---------------------------------------------------------------------------
-- 정산 조정 (st_adjust) - 가감/상계/취소 조정
-- ---------------------------------------------------------------------------
CREATE SEQUENCE IF NOT EXISTS st_adj_seq;
CREATE TABLE IF NOT EXISTS st_adjust (
    adj_id        BIGINT PRIMARY KEY,
    settlement_id BIGINT NOT NULL DEFAULT 0,
    purchase_id   BIGINT NOT NULL DEFAULT 0,
    delta         BIGINT NOT NULL DEFAULT 0,
    reason        TEXT NOT NULL DEFAULT '',
    biz_date      DATE NOT NULL,
    created_at    TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 지급 (st_payment) - 가맹점 지급 예정/보류/완료
-- ---------------------------------------------------------------------------
CREATE SEQUENCE IF NOT EXISTS st_pay_seq;
CREATE TABLE IF NOT EXISTS st_payment (
    pay_id       BIGINT PRIMARY KEY,
    merchant_id  TEXT NOT NULL,
    net_amount   BIGINT NOT NULL DEFAULT 0,
    pay_date     DATE NOT NULL,
    status       TEXT NOT NULL DEFAULT 'SCHEDULED', -- SCHEDULED/HELD/PAID/CANCELLED
    biz_date     DATE NOT NULL,
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 가맹점 정산 집계 (st_merch_settle) - 배치/ST_BYMERCH가 upsert
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS st_merch_settle (
    merchant_id  TEXT NOT NULL,
    biz_date     DATE NOT NULL,
    txn_count    BIGINT NOT NULL DEFAULT 0,
    gross_amount BIGINT NOT NULL DEFAULT 0,
    fee_amount   BIGINT NOT NULL DEFAULT 0,
    net_amount   BIGINT NOT NULL DEFAULT 0,
    updated_at   TIMESTAMPTZ NOT NULL DEFAULT now(),
    PRIMARY KEY (merchant_id, biz_date)
);

-- 수수료 구간 요율 시드 (ST_BANDRATE / ST_RATEAPPLY 가 조회)
INSERT INTO st_fee_rate (fee_type, band_min, rate_bps) VALUES
    ('MDR',   0,        25),
    ('MDR',   10000000, 20),
    ('VAN',   0,        10),
    ('RELAY', 0,        5)
ON CONFLICT (fee_type, band_min) DO NOTHING;
