-- py 모듈 (지급) 스키마 - 지급 원장 / 지급계좌 / 지급파일 / 예약 / 조정 / 집계
-- Loaded into PostgreSQL on first boot (docker-entrypoint-initdb.d). Prefix 25.

-- ---------------------------------------------------------------------------
-- 지급 원장 (payout)
--   status: R=요청, H=보류, A=승인, J=반려, F=파일편성, P=지급완료,
--           C=취소, V=역분개, X=마감
-- ---------------------------------------------------------------------------
CREATE SEQUENCE IF NOT EXISTS py_payout_seq;
CREATE TABLE IF NOT EXISTS py_payout (
    payout_id      BIGINT PRIMARY KEY,
    merchant_id    TEXT NOT NULL DEFAULT '',
    account_no     TEXT NOT NULL DEFAULT '',
    amount         BIGINT NOT NULL,
    fee            BIGINT NOT NULL DEFAULT 0,
    net            BIGINT NOT NULL,
    status         TEXT NOT NULL,
    channel        TEXT NOT NULL DEFAULT 'STD',   -- STD/SPLIT/BULK/ADVANCE
    file_id        BIGINT NOT NULL DEFAULT 0,
    retry_count    INTEGER NOT NULL DEFAULT 0,
    reject_reason  TEXT NOT NULL DEFAULT '',
    biz_date       DATE NOT NULL,
    created_at     TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at     TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 지급계좌 (py_account)
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS py_account (
    account_no   TEXT PRIMARY KEY,
    merchant_id  TEXT NOT NULL DEFAULT '',
    bank_code    TEXT NOT NULL DEFAULT '',
    holder       TEXT NOT NULL DEFAULT '',
    balance      BIGINT NOT NULL DEFAULT 0,
    status       TEXT NOT NULL DEFAULT 'ACTIVE',
    updated_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 지급 파일 (py_file) - status: G=생성, C=확정
-- ---------------------------------------------------------------------------
CREATE SEQUENCE IF NOT EXISTS py_file_seq;
CREATE TABLE IF NOT EXISTS py_file (
    file_id       BIGINT PRIMARY KEY,
    biz_date      DATE NOT NULL,
    file_name     TEXT NOT NULL,
    record_count  BIGINT NOT NULL DEFAULT 0,
    total_amount  BIGINT NOT NULL DEFAULT 0,
    status        TEXT NOT NULL DEFAULT 'G',
    created_at    TIMESTAMPTZ NOT NULL DEFAULT now(),
    confirmed_at  TIMESTAMPTZ
);

-- ---------------------------------------------------------------------------
-- 지급 예약 (py_schedule) - status: S=예약, D=실행
-- ---------------------------------------------------------------------------
CREATE SEQUENCE IF NOT EXISTS py_schedule_seq;
CREATE TABLE IF NOT EXISTS py_schedule (
    schedule_id  BIGINT PRIMARY KEY,
    payout_id    BIGINT NOT NULL,
    run_date     DATE NOT NULL,
    status       TEXT NOT NULL DEFAULT 'S',
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 지급 조정 (py_adjust)
-- ---------------------------------------------------------------------------
CREATE SEQUENCE IF NOT EXISTS py_adjust_seq;
CREATE TABLE IF NOT EXISTS py_adjust (
    adj_id       BIGINT PRIMARY KEY,
    payout_id    BIGINT NOT NULL,
    delta        BIGINT NOT NULL,
    reason       TEXT NOT NULL DEFAULT '',
    biz_date     DATE NOT NULL,
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 지급 원장 상세 (py_ledger) - 이체/연계 원장
-- ---------------------------------------------------------------------------
CREATE SEQUENCE IF NOT EXISTS py_ledger_seq;
CREATE TABLE IF NOT EXISTS py_ledger (
    ledger_id    BIGINT PRIMARY KEY,
    payout_id    BIGINT NOT NULL,
    entry_type   TEXT NOT NULL,
    amount       BIGINT NOT NULL,
    biz_date     DATE NOT NULL,
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 가맹점 일별 지급 집계 (py_summary) - 배치/서비스가 채움
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS py_summary (
    merchant_id    TEXT NOT NULL,
    biz_date       DATE NOT NULL,
    payout_count   BIGINT NOT NULL DEFAULT 0,
    payout_amount  BIGINT NOT NULL DEFAULT 0,
    updated_at     TIMESTAMPTZ NOT NULL DEFAULT now(),
    PRIMARY KEY (merchant_id, biz_date)
);

-- Seed payout accounts so PAY_REMIT/PAY_ACCTCHK have counterparties.
INSERT INTO py_account (account_no, merchant_id, bank_code, holder, balance, status) VALUES
    ('110-1234-567890', 'M0001', '020', '클라로 커피', 500000000, 'ACTIVE'),
    ('333-9876-543210', 'M0002', '088', '포지 마트',   500000000, 'ACTIVE'),
    ('456-0000-111222', 'M0003', '004', '클라로 면세점', 900000000, 'ACTIVE')
ON CONFLICT (account_no) DO NOTHING;
