-- V98: 본사정산 (hq_settle_item)
--      GA 본사 자체의 수입/비용 정산 항목(설계사 지급과 별개의 GA 손익).
--      본사손익 = SUM(INCOME amount) - SUM(EXPENSE amount)

CREATE TABLE hq_settle_item (
    item_id      BIGSERIAL      PRIMARY KEY,
    settle_month CHAR(6)        NOT NULL,
    item_type    VARCHAR(20)    NOT NULL,
    item_name    VARCHAR(100)   NOT NULL,
    amount       NUMERIC(15,2)  NOT NULL,
    memo         VARCHAR(300),
    status       VARCHAR(20)    NOT NULL DEFAULT 'REGISTERED',
    created_at   TIMESTAMP      NOT NULL DEFAULT NOW(),
    created_by   BIGINT,
    updated_at   TIMESTAMP,
    CHECK (item_type IN ('INCOME','EXPENSE')),
    CHECK (status IN ('REGISTERED','CONFIRMED'))
);

COMMENT ON TABLE  hq_settle_item            IS '본사정산 항목 — GA 자체 수입/비용 정산';
COMMENT ON COLUMN hq_settle_item.settle_month IS '정산 귀속월 (YYYYMM)';
COMMENT ON COLUMN hq_settle_item.item_type    IS '항목유형 (INCOME=수입/EXPENSE=비용)';
COMMENT ON COLUMN hq_settle_item.item_name    IS '항목명';
COMMENT ON COLUMN hq_settle_item.amount       IS '금액 (원, HALF_UP)';
COMMENT ON COLUMN hq_settle_item.memo         IS '비고';
COMMENT ON COLUMN hq_settle_item.status       IS '상태 (REGISTERED=등록/CONFIRMED=확정)';

CREATE INDEX idx_hqi_month_type   ON hq_settle_item(settle_month, item_type);
CREATE INDEX idx_hqi_month_status ON hq_settle_item(settle_month, status);
