-- SQLite数据库初始化脚本
-- 适用于开发环境(ai_mip_dev.db)和生产环境(ai_mip_prod.db)

-- ----------------------------
-- Table structure for ai_mip_site
-- ----------------------------
DROP TABLE IF EXISTS ai_mip_site;
CREATE TABLE ai_mip_site (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  site_url TEXT NOT NULL UNIQUE,
  site_name TEXT,
  status TEXT CHECK(status IN ('active', 'inactive', 'pending')) DEFAULT 'active',
  frequency INTEGER DEFAULT 1,
  time_start TEXT DEFAULT '00:00:00',
  time_end TEXT DEFAULT '23:59:59',
  interval_minutes INTEGER DEFAULT 60,
  ad_feature TEXT,
  click_count INTEGER DEFAULT 0,
  reply_count INTEGER DEFAULT 0,
  site_dimension TEXT,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  created_by TEXT,
  updated_by TEXT,
  remark TEXT
);

-- 创建索引
CREATE UNIQUE INDEX idx_site_url ON ai_mip_site(site_url);
CREATE INDEX idx_status ON ai_mip_site(status);
CREATE INDEX idx_created_at ON ai_mip_site(created_at);

-- ----------------------------
-- Table structure for ai_mip_click
-- ----------------------------
DROP TABLE IF EXISTS ai_mip_click;
CREATE TABLE ai_mip_click (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  site_id INTEGER NOT NULL,
  site_url TEXT NOT NULL,
  click_time DATETIME NOT NULL,
  user_ip TEXT,
  user_agent TEXT,
  referer_url TEXT,
  device_type TEXT CHECK(device_type IN ('mobile', 'pc', 'tablet')),
  click_count INTEGER DEFAULT 1,
  is_valid INTEGER DEFAULT 1,
  task_id TEXT,
  operator TEXT,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (site_id) REFERENCES ai_mip_site(id)
);

-- 创建索引
CREATE INDEX idx_site_id ON ai_mip_click(site_id);
CREATE INDEX idx_click_time ON ai_mip_click(click_time);
CREATE INDEX idx_site_url_click ON ai_mip_click(site_url);
CREATE INDEX idx_click_time_site ON ai_mip_click(click_time, site_id);
CREATE INDEX idx_task_id ON ai_mip_click(task_id);

-- ----------------------------
-- Table structure for ai_mip_interaction
-- ----------------------------
DROP TABLE IF EXISTS ai_mip_interaction;
CREATE TABLE ai_mip_interaction (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  site_id INTEGER NOT NULL,
  click_id INTEGER,
  task_id TEXT,
  interaction_type TEXT CHECK(interaction_type IN ('reply', 'comment', 'message', 'form_submit', 'follow', 'like', 'share')) NOT NULL,
  interaction_time DATETIME NOT NULL,
  interaction_status TEXT CHECK(interaction_status IN ('pending', 'success', 'failed', 'skipped')) DEFAULT 'pending',
  reply_content TEXT,
  reply_template_id TEXT,
  ad_element_xpath TEXT,
  ad_element_selector TEXT,
  ad_text_content TEXT,
  execution_mode TEXT CHECK(execution_mode IN ('auto', 'manual', 'semi_auto')) DEFAULT 'auto',
  rpa_script TEXT,
  browser_type TEXT CHECK(browser_type IN ('headless', 'headed', 'playwright', 'selenium')),
  anti_detection_method TEXT,
  proxy_ip TEXT,
  user_agent TEXT,
  custom_headers TEXT,
  fingerprint_id TEXT,
  response_received INTEGER DEFAULT 0,
  response_content TEXT,
  response_time DATETIME,
  response_delay_seconds INTEGER,
  is_successful INTEGER DEFAULT 0,
  error_message TEXT,
  retry_count INTEGER DEFAULT 0,
  conversion_flag INTEGER DEFAULT 0,
  site_dimension TEXT,
  campaign_id TEXT,
  operator TEXT,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  remark TEXT,
  FOREIGN KEY (site_id) REFERENCES ai_mip_site(id),
  FOREIGN KEY (click_id) REFERENCES ai_mip_click(id)
);

-- 创建索引
CREATE INDEX idx_site_id_interaction ON ai_mip_interaction(site_id);
CREATE INDEX idx_click_id_interaction ON ai_mip_interaction(click_id);
CREATE INDEX idx_task_id_interaction ON ai_mip_interaction(task_id);
CREATE INDEX idx_interaction_time ON ai_mip_interaction(interaction_time);
CREATE INDEX idx_interaction_status ON ai_mip_interaction(interaction_status);
CREATE INDEX idx_composite ON ai_mip_interaction(site_id, interaction_time, interaction_status);
CREATE INDEX idx_response_received ON ai_mip_interaction(response_received);
CREATE INDEX idx_conversion ON ai_mip_interaction(conversion_flag);

-- 创建触发器：自动更新 updated_at
CREATE TRIGGER update_ai_mip_site_timestamp 
AFTER UPDATE ON ai_mip_site
BEGIN
  UPDATE ai_mip_site SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;

CREATE TRIGGER update_ai_mip_interaction_timestamp 
AFTER UPDATE ON ai_mip_interaction
BEGIN
  UPDATE ai_mip_interaction SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;
