-- V41: 수금수수료 (collection_commission_rule + collection_commission_ledger)
--      보험료 수납 단위(매월)로 발생하는 수금수수료. 납입주기별 차등 적용.

-- ============================================================
-- 1. collection_commission_rule — 납입주기별 수금수수료율 마스터
-- ============================================================

CREATE TABLE collection_commission_rule (
    rule_id          BIGSERIAL      PRIMARY KEY,
    product_id       BIGINT         NOT NULL REFERENCES product(product_id),
    payment_cycle    VARCHAR(20)    NOT NULL,
    commission_rate  NUMERIC(7,4)   NOT NULL,
    effective_from   DATE           NOT NULL,
    effective_to     DATE,
    created_at       TIMESTAMP      NOT NULL DEFAULT NOW(),
    created_by       BIGINT,
    updated_at       TIMESTAMP,
    UNIQUE (product_id, payment_cycle, effective_from),
    CHECK (payment_cycle IN ('MONTHLY','QUARTERLY','SEMI','ANNUAL')),
    CHECK (commission_rate >= 0 AND commission_rate <= 1)
);

COMMENT ON TABLE  collection_commission_rule               IS '수금수수료율 마스터 — 상품/납입주기별 수금수수료율 정의';
COMMENT ON COLUMN collection_commission_rule.product_id    IS '적용 상품 FK';
COMMENT ON COLUMN collection_commission_rule.payment_cycle IS '납입주기 (MONTHLY=월납/QUARTERLY=분기납/SEMI=반기납/ANNUAL=연납)';
COMMENT ON COLUMN collection_commission_rule.commission_rate IS '수금수수료율 (소수, 예: 0.005 = 0.5%)';
COMMENT ON COLUMN collection_commission_rule.effective_from  IS '적용 시작일';
COMMENT ON COLUMN collection_commission_rule.effective_to    IS '적용 종료일 (NULL=현재 유효)';

CREATE INDEX idx_ccr_product ON collection_commission_rule(product_id, effective_from);

-- ============================================================
-- 2. collection_commission_ledger — 수금수수료 원장
--    매월 수납 확인 후 발생. paid_at 기준으로 정산월 귀속.
-- ============================================================

CREATE TABLE collection_commission_ledger (
    ledger_id         BIGSERIAL      PRIMARY KEY,
    contract_id       BIGINT         NOT NULL REFERENCES contract(contract_id),
    agent_id          BIGINT         NOT NULL REFERENCES agent(agent_id),
    settle_month      CHAR(6)        NOT NULL,
    premium_amount    NUMERIC(15,2)  NOT NULL,
    commission_rate   NUMERIC(7,4)   NOT NULL,
    commission_amount NUMERIC(15,2)  NOT NULL,
    payment_cycle     VARCHAR(20)    NOT NULL,
    paid_at           DATE           NOT NULL,
    status            VARCHAR(20)    NOT NULL DEFAULT 'CALCULATED',
    created_at        TIMESTAMP      NOT NULL DEFAULT NOW(),
    updated_at        TIMESTAMP,
    CHECK (payment_cycle IN ('MONTHLY','QUARTERLY','SEMI','ANNUAL')),
    CHECK (status IN ('CALCULATED','CONFIRMED','PAID','CANCELLED'))
);

COMMENT ON TABLE  collection_commission_ledger                IS '수금수수료 원장 — 매월 보험료 수납 단위로 발생';
COMMENT ON COLUMN collection_commission_ledger.contract_id    IS '대상 계약 FK';
COMMENT ON COLUMN collection_commission_ledger.agent_id       IS '수금 담당 설계사 FK';
COMMENT ON COLUMN collection_commission_ledger.settle_month   IS '정산 귀속월 (YYYYMM)';
COMMENT ON COLUMN collection_commission_ledger.premium_amount IS '수납 보험료';
COMMENT ON COLUMN collection_commission_ledger.commission_rate  IS '적용 수금수수료율';
COMMENT ON COLUMN collection_commission_ledger.commission_amount IS '수금수수료 = premium_amount * commission_rate';
COMMENT ON COLUMN collection_commission_ledger.payment_cycle  IS '납입주기';
COMMENT ON COLUMN collection_commission_ledger.paid_at        IS '실 보험료 납입일';

CREATE INDEX idx_ccl_agent_month ON collection_commission_ledger(agent_id, settle_month);
CREATE INDEX idx_ccl_contract    ON collection_commission_ledger(contract_id);
CREATE INDEX idx_ccl_status      ON collection_commission_ledger(status, settle_month);
