-- 微信营销管理系统 MVP 建表 SQL
-- 数据库: wxchat_sales

-- 销售人员
CREATE TABLE IF NOT EXISTS salesperson (
    id          SERIAL PRIMARY KEY,
    name        TEXT NOT NULL,
    team        TEXT,
    wx_account  TEXT NOT NULL,
    device_id   TEXT,
    created_at  TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- 联系人（微信好友）
CREATE TABLE IF NOT EXISTS contact (
    id              SERIAL PRIMARY KEY,
    salesperson_id  INT NOT NULL REFERENCES salesperson(id) ON DELETE CASCADE,
    wx_username     TEXT NOT NULL,
    nickname        TEXT,
    remark          TEXT,
    display_name    TEXT NOT NULL,
    is_group        BOOLEAN NOT NULL DEFAULT FALSE,
    created_at      TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE(salesperson_id, wx_username)
);

-- 会话
CREATE TABLE IF NOT EXISTS conversation (
    id              SERIAL PRIMARY KEY,
    salesperson_id  INT NOT NULL REFERENCES salesperson(id) ON DELETE CASCADE,
    contact_id      INT REFERENCES contact(id) ON DELETE CASCADE,
    wx_identifier   TEXT NOT NULL,
    conv_type       TEXT NOT NULL DEFAULT 'single',
    last_synced_at  TIMESTAMPTZ,
    created_at      TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE(salesperson_id, wx_identifier)
);

-- 消息
CREATE TABLE IF NOT EXISTS message (
    id                  BIGSERIAL PRIMARY KEY,
    salesperson_id      INT NOT NULL REFERENCES salesperson(id) ON DELETE CASCADE,
    conversation_id     INT NOT NULL REFERENCES conversation(id) ON DELETE CASCADE,
    sender_wx_username  TEXT NOT NULL,
    sender_display_name TEXT,
    message_type        TEXT NOT NULL,
    raw_content         TEXT,
    normalized_content  TEXT,
    created_at          TIMESTAMPTZ NOT NULL,
    source_shard        TEXT NOT NULL,
    source_table        TEXT NOT NULL,
    source_local_id     BIGINT NOT NULL,
    UNIQUE(source_shard, source_table, source_local_id)
);

CREATE INDEX IF NOT EXISTS idx_msg_conv_time ON message(conversation_id, created_at);
CREATE INDEX IF NOT EXISTS idx_msg_salesperson ON message(salesperson_id);
CREATE INDEX IF NOT EXISTS idx_msg_created_at ON message(created_at);

-- 客户（AI 识别）
CREATE TABLE IF NOT EXISTS customer (
    id              SERIAL PRIMARY KEY,
    salesperson_id  INT NOT NULL REFERENCES salesperson(id) ON DELETE CASCADE,
    contact_id      INT NOT NULL REFERENCES contact(id) ON DELETE CASCADE,
    customer_name   TEXT,
    industry        TEXT,
    intent_level    TEXT,
    key_needs       TEXT[],
    reason          TEXT,
    summary         TEXT,
    stage           TEXT,
    key_points      TEXT[],
    objections      TEXT[],
    next_action     TEXT,
    last_analysis   TIMESTAMPTZ,
    created_at      TIMESTAMPTZ NOT NULL DEFAULT now(),
    UNIQUE(salesperson_id, contact_id)
);

CREATE INDEX IF NOT EXISTS idx_customer_salesperson ON customer(salesperson_id);
CREATE INDEX IF NOT EXISTS idx_customer_intent ON customer(intent_level);

-- 成交记录
CREATE TABLE IF NOT EXISTS deal (
    id              SERIAL PRIMARY KEY,
    salesperson_id  INT NOT NULL REFERENCES salesperson(id) ON DELETE CASCADE,
    customer_id     INT REFERENCES customer(id) ON DELETE SET NULL,
    contact_id      INT REFERENCES contact(id) ON DELETE SET NULL,
    product_name    TEXT NOT NULL,
    amount          NUMERIC(12,2) NOT NULL,
    deal_date       DATE NOT NULL,
    status          TEXT NOT NULL DEFAULT 'closed',
    notes           TEXT,
    created_at      TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_deal_salesperson ON deal(salesperson_id);
CREATE INDEX IF NOT EXISTS idx_deal_customer ON deal(customer_id);
CREATE INDEX IF NOT EXISTS idx_deal_date ON deal(deal_date);

-- 预置 3 名销售人员（幂等）
INSERT INTO salesperson (id, name, team, wx_account, device_id) VALUES
    (1, '张伟', '华东团队', 'wxid_zhangwei', 'macbook-zw-001'),
    (2, '李娜', '华东团队', 'wxid_lina', 'macbook-ln-002'),
    (3, '王强', '华南团队', 'wxid_wangqiang', 'macbook-wq-003')
ON CONFLICT (id) DO NOTHING;
