-- ============================================================
-- 01_create_tables.sql
-- 任务闭环系统：表结构创建
-- ============================================================

-- 门店整改任务表
CREATE TABLE IF NOT EXISTS analytics.store_task (
  task_id SERIAL PRIMARY KEY,
  plan_month DATE NOT NULL,
  store_code TEXT NOT NULL,
  store_name TEXT NOT NULL,
  priority TEXT NOT NULL,
  problem_indicator TEXT NOT NULL,
  current_value NUMERIC,
  benchmark_value NUMERIC,
  target_value NUMERIC,
  problem_description TEXT,
  action_required TEXT,
  owner TEXT NOT NULL,
  collaborators TEXT,
  deadline DATE NOT NULL,
  status TEXT DEFAULT '待启动',
  process_evidence TEXT,
  verification_indicator TEXT,
  verification_result TEXT,
  incomplete_reason TEXT,
  next_step TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- 任务状态变更日志
CREATE TABLE IF NOT EXISTS analytics.store_task_log (
  log_id SERIAL PRIMARY KEY,
  task_id INT REFERENCES analytics.store_task(task_id) ON DELETE CASCADE,
  action TEXT NOT NULL,
  old_status TEXT,
  new_status TEXT,
  operator TEXT NOT NULL,
  comment TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- 任务模板表
CREATE TABLE IF NOT EXISTS analytics.task_template (
  template_id SERIAL PRIMARY KEY,
  problem_type TEXT NOT NULL UNIQUE,
  problem_indicator TEXT NOT NULL,
  default_action TEXT NOT NULL,
  default_target_adjustment TEXT,
  verification_indicator TEXT,
  suggested_deadline_days INT DEFAULT 30
);

-- 指标字典表
CREATE TABLE IF NOT EXISTS analytics.indicator_dictionary (
  id SERIAL PRIMARY KEY,
  indicator_name TEXT NOT NULL UNIQUE,
  business_definition TEXT NOT NULL,
  formula TEXT NOT NULL,
  data_source TEXT NOT NULL,
  update_frequency TEXT NOT NULL,
  owner TEXT,
  scope TEXT,
  yellow_threshold NUMERIC,
  red_threshold NUMERIC,
  version_date DATE NOT NULL DEFAULT CURRENT_DATE
);

-- 门店主数据表
CREATE TABLE IF NOT EXISTS analytics.dim_store (
  store_code TEXT PRIMARY KEY,
  store_name TEXT NOT NULL,
  region TEXT,
  business_type TEXT,
  open_date DATE,
  close_date DATE,
  area_sqm NUMERIC,
  seat_count INT,
  business_area TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- 经验标准化表
CREATE TABLE IF NOT EXISTS analytics.standardized_practice (
  id SERIAL PRIMARY KEY,
  practice_module TEXT NOT NULL,
  benchmark_store_code TEXT NOT NULL,
  benchmark_store_name TEXT NOT NULL,
  key_actions TEXT NOT NULL,
  verification_indicators TEXT,
  status TEXT DEFAULT '待推广',
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- 经验推广试点结果表
CREATE TABLE IF NOT EXISTS analytics.practice_replication (
  id SERIAL PRIMARY KEY,
  practice_id INT REFERENCES analytics.standardized_practice(id) ON DELETE CASCADE,
  trial_store_code TEXT NOT NULL,
  trial_store_name TEXT NOT NULL,
  observation_weeks INT DEFAULT 4,
  before_value NUMERIC,
  after_value NUMERIC,
  revenue_impacted BOOLEAN DEFAULT FALSE,
  customer_impacted BOOLEAN DEFAULT FALSE,
  inventory_impacted BOOLEAN DEFAULT FALSE,
  status TEXT DEFAULT '观察中',
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- 门店升降级日志表
CREATE TABLE IF NOT EXISTS analytics.store_grade_change (
  id SERIAL PRIMARY KEY,
  change_month DATE NOT NULL,
  store_code TEXT NOT NULL,
  store_name TEXT NOT NULL,
  old_priority TEXT,
  new_priority TEXT,
  change_type TEXT,
  reason TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- 周度检查记录表
CREATE TABLE IF NOT EXISTS analytics.task_weekly_check (
  id SERIAL PRIMARY KEY,
  task_id INT REFERENCES analytics.store_task(task_id) ON DELETE CASCADE,
  store_code TEXT NOT NULL,
  store_name TEXT NOT NULL,
  problem_indicator TEXT NOT NULL,
  iso_week INT NOT NULL,
  this_week_value NUMERIC,
  last_week_value NUMERIC,
  change_direction TEXT,
  consecutive_no_improve_weeks INT DEFAULT 0,
  check_comment TEXT,
  checked_by TEXT,
  checked_at TIMESTAMPTZ DEFAULT NOW(),
  UNIQUE(task_id, iso_week)
);

-- 月度验收记录表
CREATE TABLE IF NOT EXISTS analytics.task_monthly_review (
  id SERIAL PRIMARY KEY,
  task_id INT REFERENCES analytics.store_task(task_id) ON DELETE CASCADE,
  store_code TEXT NOT NULL,
  store_name TEXT NOT NULL,
  plan_month DATE NOT NULL,
  problem_indicator TEXT NOT NULL,
  baseline_value NUMERIC,
  target_value NUMERIC,
  actual_value NUMERIC,
  review_result TEXT NOT NULL,
  revenue_stable BOOLEAN DEFAULT FALSE,
  margin_improved BOOLEAN DEFAULT FALSE,
  customer_stable BOOLEAN DEFAULT FALSE,
  anomaly_decreased BOOLEAN DEFAULT FALSE,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  UNIQUE(task_id)
);

-- 数据刷新日志表
CREATE TABLE IF NOT EXISTS analytics.data_refresh_log (
  id SERIAL PRIMARY KEY,
  refresh_month DATE NOT NULL,
  action TEXT NOT NULL,
  status TEXT NOT NULL,
  details TEXT,
  operator TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- 索引
CREATE INDEX IF NOT EXISTS idx_store_task_month ON analytics.store_task(plan_month);
CREATE INDEX IF NOT EXISTS idx_store_task_store ON analytics.store_task(store_code);
CREATE INDEX IF NOT EXISTS idx_store_task_priority ON analytics.store_task(priority);
CREATE INDEX IF NOT EXISTS idx_store_task_status ON analytics.store_task(status);
CREATE INDEX IF NOT EXISTS idx_store_task_log_task ON analytics.store_task_log(task_id);
CREATE INDEX IF NOT EXISTS idx_task_weekly_check_task ON analytics.task_weekly_check(task_id);
CREATE INDEX IF NOT EXISTS idx_task_monthly_review_task ON analytics.task_monthly_review(task_id);
