-- V91: 인정실적 + 환산보험료 (product_conversion_rate + recognized_performance_ledger)
--      환산보험료 = 보험료 × 상품별 환산율.
--      인정실적 = 환산보험료 × 인정율.
--      시상/등급 산정의 기초 데이터.

-- ============================================================
-- 1. product_conversion_rate — 상품별 환산율 마스터
-- ============================================================

CREATE TABLE product_conversion_rate (
    rate_id          BIGSERIAL      PRIMARY KEY,
    product_id       BIGINT         NOT NULL REFERENCES product(product_id),
    conversion_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, effective_from),
    CHECK (conversion_rate >= 0 AND conversion_rate <= 1)
);

COMMENT ON TABLE  product_conversion_rate                   IS '상품별 환산율 마스터 — 인정실적 산출을 위한 보험료 환산율';
COMMENT ON COLUMN product_conversion_rate.rate_id           IS '환산율 PK';
COMMENT ON COLUMN product_conversion_rate.product_id        IS '적용 상품 FK';
COMMENT ON COLUMN product_conversion_rate.conversion_rate   IS '환산율 (소수, 예: 0.5 = 50%)';
COMMENT ON COLUMN product_conversion_rate.effective_from    IS '적용 시작일';
COMMENT ON COLUMN product_conversion_rate.effective_to      IS '적용 종료일 (NULL=현재 유효)';

CREATE INDEX idx_pcr_product_from ON product_conversion_rate(product_id, effective_from);

-- ============================================================
-- 2. recognized_performance_ledger — 인정실적 원장
-- ============================================================

CREATE TABLE recognized_performance_ledger (
    ledger_id           BIGSERIAL      PRIMARY KEY,
    agent_id            BIGINT         NOT NULL REFERENCES agent(agent_id),
    contract_id         BIGINT         NOT NULL REFERENCES contract(contract_id),
    settle_month        CHAR(6)        NOT NULL,
    premium_amount      NUMERIC(15,2)  NOT NULL,
    conversion_rate     NUMERIC(7,4)   NOT NULL,
    converted_premium   NUMERIC(15,2)  NOT NULL,
    recognition_rate    NUMERIC(7,4)   NOT NULL,
    recognized_amount   NUMERIC(15,2)  NOT NULL,
    status              VARCHAR(20)    NOT NULL DEFAULT 'CALCULATED',
    created_at          TIMESTAMP      NOT NULL DEFAULT NOW(),
    updated_at          TIMESTAMP,
    CHECK (status IN ('CALCULATED','CONFIRMED','CANCELLED')),
    CHECK (premium_amount >= 0),
    CHECK (conversion_rate >= 0 AND conversion_rate <= 1),
    CHECK (converted_premium >= 0),
    CHECK (recognition_rate >= 0 AND recognition_rate <= 1),
    CHECK (recognized_amount >= 0)
);

COMMENT ON TABLE  recognized_performance_ledger                   IS '인정실적 원장 — 설계사별 계약 단위 환산보험료 및 인정실적';
COMMENT ON COLUMN recognized_performance_ledger.ledger_id         IS '원장 PK';
COMMENT ON COLUMN recognized_performance_ledger.agent_id          IS '설계사 FK';
COMMENT ON COLUMN recognized_performance_ledger.contract_id       IS '대상 계약 FK';
COMMENT ON COLUMN recognized_performance_ledger.settle_month      IS '정산 귀속월 (YYYYMM)';
COMMENT ON COLUMN recognized_performance_ledger.premium_amount    IS '보험료';
COMMENT ON COLUMN recognized_performance_ledger.conversion_rate   IS '적용 환산율 (product_conversion_rate 스냅샷)';
COMMENT ON COLUMN recognized_performance_ledger.converted_premium IS '환산보험료 = premium_amount × conversion_rate';
COMMENT ON COLUMN recognized_performance_ledger.recognition_rate  IS '인정율 (설계사 등급/정책 기준)';
COMMENT ON COLUMN recognized_performance_ledger.recognized_amount IS '인정실적 = converted_premium × recognition_rate';
COMMENT ON COLUMN recognized_performance_ledger.status            IS '상태 (CALCULATED/CONFIRMED/CANCELLED)';

CREATE INDEX idx_rpl_agent_month ON recognized_performance_ledger(agent_id, settle_month);
CREATE INDEX idx_rpl_contract    ON recognized_performance_ledger(contract_id);
