-- au 모듈 (승인/한도) 스키마 - 승인 원장 / 카드 한도 / 부정거래 / 예비승인 / 집계
-- Loaded into PostgreSQL on first boot (docker-entrypoint-initdb.d). Prefix 20.

-- ---------------------------------------------------------------------------
-- 승인 원장 (authorization)
--   status: A=승인, D=거절, C=취소, R=역전, H=보류, M=매입확정(capture),
--           S=정산반영, V=무효(void)
-- ---------------------------------------------------------------------------
CREATE SEQUENCE IF NOT EXISTS au_auth_seq;
CREATE TABLE IF NOT EXISTS au_authorization (
    auth_id        BIGINT PRIMARY KEY,
    card_no        TEXT NOT NULL,
    merchant_id    TEXT NOT NULL DEFAULT '',
    amount         BIGINT NOT NULL,
    approval_code  TEXT NOT NULL DEFAULT '',
    auth_type      TEXT NOT NULL DEFAULT 'AUTH',   -- AUTH/STANDIN/INSTALL/PREAUTH
    status         TEXT NOT NULL,
    decline_reason TEXT NOT NULL DEFAULT '',
    biz_date       DATE NOT NULL,
    captured_at    TIMESTAMPTZ,
    created_at     TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at     TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 카드 한도 (card_limit)
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS card_limit (
    card_no      TEXT PRIMARY KEY,
    daily_limit  BIGINT NOT NULL DEFAULT 1000000,
    used_amount  BIGINT NOT NULL DEFAULT 0,
    temp_limit   BIGINT NOT NULL DEFAULT 0,
    status       TEXT NOT NULL DEFAULT 'ACTIVE',
    updated_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 부정거래 로그 (fraud_log)
-- ---------------------------------------------------------------------------
CREATE SEQUENCE IF NOT EXISTS au_fraud_seq;
CREATE TABLE IF NOT EXISTS au_fraud_log (
    fraud_id     BIGINT PRIMARY KEY,
    auth_id      BIGINT NOT NULL DEFAULT 0,
    card_no      TEXT NOT NULL,
    score        BIGINT NOT NULL,
    reason       TEXT NOT NULL DEFAULT '',
    biz_date     DATE NOT NULL,
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 블랙리스트 (au_blacklist)
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS au_blacklist (
    card_no      TEXT PRIMARY KEY,
    reason       TEXT NOT NULL DEFAULT '',
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 예비승인 (au_preauth) - status: P=예비, C=확정, X=취소
-- ---------------------------------------------------------------------------
CREATE SEQUENCE IF NOT EXISTS au_preauth_seq;
CREATE TABLE IF NOT EXISTS au_preauth (
    preauth_id   BIGINT PRIMARY KEY,
    card_no      TEXT NOT NULL,
    merchant_id  TEXT NOT NULL DEFAULT '',
    amount       BIGINT NOT NULL,
    status       TEXT NOT NULL DEFAULT 'P',
    biz_date     DATE NOT NULL,
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 승인 취소 (au_cancel) - FULL / PARTIAL / VOID
-- ---------------------------------------------------------------------------
CREATE SEQUENCE IF NOT EXISTS au_cancel_seq;
CREATE TABLE IF NOT EXISTS au_cancel (
    cancel_id    BIGINT PRIMARY KEY,
    auth_id      BIGINT NOT NULL,
    cancel_type  TEXT NOT NULL,
    amount       BIGINT NOT NULL,
    reason       TEXT NOT NULL DEFAULT '',
    biz_date     DATE NOT NULL,
    created_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- ---------------------------------------------------------------------------
-- 가맹점 일별 승인 집계 (au_summary) - 배치/서비스가 채움
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS au_summary (
    merchant_id   TEXT NOT NULL,
    biz_date      DATE NOT NULL,
    auth_count    BIGINT NOT NULL DEFAULT 0,
    approve_count BIGINT NOT NULL DEFAULT 0,
    decline_count BIGINT NOT NULL DEFAULT 0,
    auth_amount   BIGINT NOT NULL DEFAULT 0,
    updated_at    TIMESTAMPTZ NOT NULL DEFAULT now(),
    PRIMARY KEY (merchant_id, biz_date)
);

-- Seed card limits + a blacklist entry so AUTH/LIMIT services have counterparties.
INSERT INTO card_limit (card_no, daily_limit, used_amount) VALUES
    ('4000123412341234', 1000000, 0),
    ('5100123412341234', 2000000, 0),
    ('3400123412341234', 5000000, 0)
ON CONFLICT (card_no) DO NOTHING;

INSERT INTO au_blacklist (card_no, reason) VALUES
    ('4000999999999999', 'REPORTED_STOLEN')
ON CONFLICT (card_no) DO NOTHING;
