-- ============================================================
-- 13_v3_phase1_remaining.sql
-- Phase 1 剩余表: 产品生命周期 + 评价归因 + 数据管道 + 审计
-- ============================================================

-- T-170: 产品生命周期
CREATE TABLE IF NOT EXISTS analytics.v3_product_lifecycle (
  id SERIAL PRIMARY KEY,
  sku_code TEXT NOT NULL,
  sku_name TEXT,
  launch_date DATE NOT NULL,
  lifecycle_stage TEXT DEFAULT '爬坡',  -- 爬坡/成熟/衰退/淘汰
  survival_status TEXT,                  -- 存活/淘汰/待判定
  first_week_report_date DATE,
  day90_report_date DATE,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  UNIQUE(sku_code)
);

-- T-171: 顾客评价数据
CREATE TABLE IF NOT EXISTS analytics.v3_customer_review (
  id SERIAL PRIMARY KEY,
  review_source TEXT NOT NULL,           -- 美团/饿了么/京东/门店
  store_code TEXT,
  store_name TEXT,
  sku_code TEXT,
  sku_name TEXT,
  rating INT,
  content TEXT,
  nlp_category TEXT,                     -- 口味/服务/环境/异物/分量
  nlp_sentiment TEXT,                    -- 正面/负面/中性
  review_date DATE NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- T-150: 数据导入日志
CREATE TABLE IF NOT EXISTS analytics.v3_data_import_log (
  id SERIAL PRIMARY KEY,
  import_type TEXT NOT NULL,             -- POS/外卖/库存/费用/薪资
  status TEXT DEFAULT 'pending',         -- pending/running/success/failed
  records_count INT DEFAULT 0,
  error_message TEXT,
  started_at TIMESTAMPTZ,
  completed_at TIMESTAMPTZ,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- T-153: 数据清洗规则
CREATE TABLE IF NOT EXISTS analytics.v3_data_quality_rule (
  id SERIAL PRIMARY KEY,
  rule_name TEXT NOT NULL,
  table_name TEXT NOT NULL,
  column_name TEXT,
  rule_type TEXT NOT NULL,               -- range/null_check/regex/custom
  rule_config JSONB,
  is_enabled BOOLEAN DEFAULT true,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- T-154: 库存快照
CREATE TABLE IF NOT EXISTS analytics.v3_inventory_snapshot (
  id SERIAL PRIMARY KEY,
  snapshot_date DATE NOT NULL,
  store_code TEXT NOT NULL,
  sku_code TEXT NOT NULL,
  sku_name TEXT,
  quantity NUMERIC(12,2),
  unit TEXT,
  expiry_date DATE,
  is_near_expiry BOOLEAN DEFAULT false,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  UNIQUE(snapshot_date, store_code, sku_code)
);

-- T-195: 月度经营分析报告
CREATE TABLE IF NOT EXISTS analytics.v3_monthly_report (
  id SERIAL PRIMARY KEY,
  report_month DATE NOT NULL,
  report_type TEXT NOT NULL,             -- monthly/mid_month/weekly/daily
  store_code TEXT,
  region TEXT,
  content JSONB,
  summary TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  UNIQUE(report_month, report_type, store_code)
);

-- T-112: 目标偏差分析
CREATE TABLE IF NOT EXISTS analytics.v3_target_variance (
  id SERIAL PRIMARY KEY,
  report_month DATE NOT NULL,
  store_code TEXT NOT NULL,
  store_name TEXT,
  revenue_target NUMERIC(14,2),
  revenue_actual NUMERIC(14,2),
  achievement_pct NUMERIC(5,2),
  variance_amount NUMERIC(14,2),
  variance_reasons TEXT,                 -- 客流/客单/成本/损耗
  created_at TIMESTAMPTZ DEFAULT NOW(),
  UNIQUE(report_month, store_code)
);

-- T-113: 预算锁定
CREATE TABLE IF NOT EXISTS analytics.v3_budget_lock (
  id SERIAL PRIMARY KEY,
  lock_month DATE NOT NULL,
  store_code TEXT NOT NULL,
  lock_reason TEXT,
  root_cause_analysis TEXT,
  approval_status TEXT DEFAULT 'pending', -- pending/approved/rejected
  approved_by TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  UNIQUE(lock_month, store_code)
);

-- 索引
CREATE INDEX IF NOT EXISTS idx_v3_product_lifecycle_stage ON analytics.v3_product_lifecycle(lifecycle_stage);
CREATE INDEX IF NOT EXISTS idx_v3_customer_review_date ON analytics.v3_customer_review(review_date);
CREATE INDEX IF NOT EXISTS idx_v3_customer_review_store ON analytics.v3_customer_review(store_code);
CREATE INDEX IF NOT EXISTS idx_v3_customer_review_nlp ON analytics.v3_customer_review(nlp_category);
CREATE INDEX IF NOT EXISTS idx_v3_data_import_log_type ON analytics.v3_data_import_log(import_type);
CREATE INDEX IF NOT EXISTS idx_v3_inventory_snapshot_date ON analytics.v3_inventory_snapshot(snapshot_date);
CREATE INDEX IF NOT EXISTS idx_v3_monthly_report_month ON analytics.v3_monthly_report(report_month);
CREATE INDEX IF NOT EXISTS idx_v3_target_variance_month ON analytics.v3_target_variance(report_month);
CREATE INDEX IF NOT EXISTS idx_v3_budget_lock_month ON analytics.v3_budget_lock(lock_month);
