-- =============================================================================
-- core schema DDL — 统一维度/事实最小字段集层
-- 说明：从 DWD 维度表和事实表提取跨系统共享的核心字段
--       仅包含 ID、名称、状态、site_id 等最小字段集
--       第一版保持精简，后续按需扩展
-- =============================================================================

CREATE SCHEMA IF NOT EXISTS core;
SET search_path TO core;

-- -----------------------------------------------------------------------------
-- 1. dim_site — 门店维度（核心字段）
-- 来源：dwd.dim_site（14 字段 → 6 字段）
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS dim_site (
    site_id         BIGINT      PRIMARY KEY,
    tenant_id       BIGINT      NOT NULL,
    shop_name       TEXT        NOT NULL,
    site_label      TEXT,
    shop_status     INTEGER,
    site_id_alias   BIGINT      GENERATED ALWAYS AS (site_id) STORED
);
COMMENT ON TABLE core.dim_site IS '门店维度核心表：仅保留跨系统共享的最小字段集。';
COMMENT ON COLUMN core.dim_site.site_id IS '门店 ID（主键）。';
COMMENT ON COLUMN core.dim_site.tenant_id IS '租户/品牌 ID。';
COMMENT ON COLUMN core.dim_site.shop_name IS '门店名称。';
COMMENT ON COLUMN core.dim_site.site_label IS '门店标签（如 A/B 店）。';
COMMENT ON COLUMN core.dim_site.shop_status IS '门店状态枚举。';
-- site_id_alias 用于 RLS 策略中统一使用 site_id 过滤

-- -----------------------------------------------------------------------------
-- 2. dim_member — 会员维度（核心字段）
-- 来源：dwd.dim_member（12 字段 → 8 字段）
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS dim_member (
    member_id               BIGINT      PRIMARY KEY,
    system_member_id        BIGINT,
    tenant_id               BIGINT      NOT NULL,
    register_site_id        BIGINT      NOT NULL,
    mobile                  TEXT,
    nickname                TEXT,
    member_card_grade_name  TEXT,
    status                  INTEGER
);
COMMENT ON TABLE core.dim_member IS '会员维度核心表：仅保留跨系统共享的最小字段集。';
COMMENT ON COLUMN core.dim_member.member_id IS '会员 ID（主键）。';
COMMENT ON COLUMN core.dim_member.system_member_id IS '系统级会员 ID（跨门店统一）。';
COMMENT ON COLUMN core.dim_member.tenant_id IS '租户/品牌 ID。';
COMMENT ON COLUMN core.dim_member.register_site_id IS '注册门店 ID（等同 site_id）。';
COMMENT ON COLUMN core.dim_member.mobile IS '手机号。';
COMMENT ON COLUMN core.dim_member.nickname IS '昵称/姓名。';
COMMENT ON COLUMN core.dim_member.member_card_grade_name IS '会员卡等级名称。';
COMMENT ON COLUMN core.dim_member.status IS '会员状态。';

-- -----------------------------------------------------------------------------
-- 3. dim_assistant — 助教维度（核心字段）
-- 来源：dwd.dim_assistant（16 字段 → 9 字段）
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS dim_assistant (
    assistant_id        BIGINT      PRIMARY KEY,
    tenant_id           BIGINT      NOT NULL,
    site_id             BIGINT      NOT NULL,
    real_name           TEXT        NOT NULL,
    nickname            TEXT,
    mobile              TEXT,
    level               INTEGER,
    assistant_status    INTEGER,
    leave_status        INTEGER
);
COMMENT ON TABLE core.dim_assistant IS '助教维度核心表：仅保留跨系统共享的最小字段集。';
COMMENT ON COLUMN core.dim_assistant.assistant_id IS '助教 ID（主键）。';
COMMENT ON COLUMN core.dim_assistant.tenant_id IS '租户/品牌 ID。';
COMMENT ON COLUMN core.dim_assistant.site_id IS '门店 ID。';
COMMENT ON COLUMN core.dim_assistant.real_name IS '真实姓名。';
COMMENT ON COLUMN core.dim_assistant.nickname IS '昵称。';
COMMENT ON COLUMN core.dim_assistant.mobile IS '手机号。';
COMMENT ON COLUMN core.dim_assistant.level IS '助教等级。';
COMMENT ON COLUMN core.dim_assistant.assistant_status IS '助教状态。';
COMMENT ON COLUMN core.dim_assistant.leave_status IS '离职状态。';

-- -----------------------------------------------------------------------------
-- 4. dim_table — 台桌维度（核心字段）
-- 来源：dwd.dim_table（8 字段 → 5 字段）
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS dim_table (
    table_id                BIGINT      PRIMARY KEY,
    site_id                 BIGINT      NOT NULL,
    table_name              TEXT        NOT NULL,
    site_table_area_name    TEXT,
    table_price             NUMERIC(18,2)
);
COMMENT ON TABLE core.dim_table IS '台桌维度核心表：仅保留跨系统共享的最小字段集。';
COMMENT ON COLUMN core.dim_table.table_id IS '台桌 ID（主键）。';
COMMENT ON COLUMN core.dim_table.site_id IS '门店 ID。';
COMMENT ON COLUMN core.dim_table.table_name IS '台桌名称。';
COMMENT ON COLUMN core.dim_table.site_table_area_name IS '区域名称。';
COMMENT ON COLUMN core.dim_table.table_price IS '台桌单价。';

-- -----------------------------------------------------------------------------
-- 5. dim_goods_category — 商品分类维度（核心字段）
-- 来源：dwd.dim_goods_category（10 字段 → 5 字段）
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS dim_goods_category (
    category_id     BIGINT      PRIMARY KEY,
    tenant_id       BIGINT      NOT NULL,
    category_name   TEXT        NOT NULL,
    parent_id       BIGINT,
    level           INTEGER
);
COMMENT ON TABLE core.dim_goods_category IS '商品分类维度核心表。';

-- -----------------------------------------------------------------------------
-- 6. fact_settlement — 结算事实（核心字段）
-- 来源：dwd.dwd_settlement_head（约 30 字段 → 12 字段）
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS fact_settlement (
    order_settle_id     BIGINT      PRIMARY KEY,
    site_id             BIGINT      NOT NULL,
    tenant_id           BIGINT      NOT NULL,
    order_trade_no      BIGINT,
    member_id           BIGINT,
    total_amount        NUMERIC(18,2),
    actual_amount       NUMERIC(18,2),
    discount_amount     NUMERIC(18,2),
    pay_status          INTEGER,
    settle_time         TIMESTAMPTZ,
    created_at          TIMESTAMPTZ,
    updated_at          TIMESTAMPTZ
);
COMMENT ON TABLE core.fact_settlement IS '结算事实核心表：仅保留跨系统共享的最小字段集。';
COMMENT ON COLUMN core.fact_settlement.order_settle_id IS '结账单 ID（主键）。';
COMMENT ON COLUMN core.fact_settlement.site_id IS '门店 ID。';
COMMENT ON COLUMN core.fact_settlement.tenant_id IS '租户 ID。';
COMMENT ON COLUMN core.fact_settlement.total_amount IS '应收总额。';
COMMENT ON COLUMN core.fact_settlement.actual_amount IS '实收金额。';
COMMENT ON COLUMN core.fact_settlement.discount_amount IS '优惠金额。';

-- -----------------------------------------------------------------------------
-- 7. fact_payment — 支付事实（核心字段）
-- 来源：dwd.dwd_payment（约 12 字段 → 7 字段）
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS fact_payment (
    payment_id      BIGINT      PRIMARY KEY,
    site_id         BIGINT      NOT NULL,
    order_settle_id BIGINT,
    pay_type        INTEGER,
    pay_amount      NUMERIC(18,2),
    pay_time        TIMESTAMPTZ,
    status          INTEGER
);
COMMENT ON TABLE core.fact_payment IS '支付事实核心表。';

-- -----------------------------------------------------------------------------
-- 索引
-- -----------------------------------------------------------------------------
CREATE INDEX IF NOT EXISTS idx_core_member_site ON core.dim_member (register_site_id);
CREATE INDEX IF NOT EXISTS idx_core_assistant_site ON core.dim_assistant (site_id);
CREATE INDEX IF NOT EXISTS idx_core_table_site ON core.dim_table (site_id);
CREATE INDEX IF NOT EXISTS idx_core_settlement_site ON core.fact_settlement (site_id);
CREATE INDEX IF NOT EXISTS idx_core_settlement_time ON core.fact_settlement (settle_time);
CREATE INDEX IF NOT EXISTS idx_core_payment_site ON core.fact_payment (site_id);
