-- 创建目标表
CREATE TABLE IF NOT EXISTS t_goals (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id VARCHAR(255) NOT NULL,
  title VARCHAR(255) NOT NULL,
  description TEXT,
  repeat_type VARCHAR(20) NOT NULL DEFAULT 'daily' CHECK (repeat_type IN ('daily', 'weekly', 'monthly', 'custom')),
  frequency INTEGER NOT NULL DEFAULT 1 CHECK (frequency > 0 AND frequency <= 100),
  custom_repeat_rule JSONB,
  start_date DATE NOT NULL,
  end_date DATE,
  status VARCHAR(20) NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'paused', 'completed', 'cancelled')),
  completed_count INTEGER NOT NULL DEFAULT 0,
  target_count INTEGER CHECK (target_count > 0),
  category VARCHAR(100),
  priority INTEGER NOT NULL DEFAULT 0 CHECK (priority >= 0 AND priority <= 10),
  has_reminder BOOLEAN NOT NULL DEFAULT false,
  reminder_time TIME,
  reminder_settings JSONB,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  deleted BOOLEAN NOT NULL DEFAULT false
);

-- 创建目标完成记录表
CREATE TABLE IF NOT EXISTS t_goal_completions (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  goal_id UUID NOT NULL REFERENCES t_goals(id) ON DELETE CASCADE,
  user_id VARCHAR(255) NOT NULL,
  completed_at TIMESTAMP WITH TIME ZONE NOT NULL,
  completion_count INTEGER NOT NULL DEFAULT 1 CHECK (completion_count > 0),
  notes TEXT,
  metadata JSONB,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  deleted BOOLEAN NOT NULL DEFAULT false
);

-- 创建索引
CREATE INDEX IF NOT EXISTS idx_goals_user_id ON t_goals(user_id);
CREATE INDEX IF NOT EXISTS idx_goals_status ON t_goals(status);
CREATE INDEX IF NOT EXISTS idx_goals_repeat_type ON t_goals(repeat_type);
CREATE INDEX IF NOT EXISTS idx_goals_category ON t_goals(category);
CREATE INDEX IF NOT EXISTS idx_goals_start_date ON t_goals(start_date);
CREATE INDEX IF NOT EXISTS idx_goals_deleted ON t_goals(deleted);

CREATE INDEX IF NOT EXISTS idx_goal_completions_goal_id ON t_goal_completions(goal_id);
CREATE INDEX IF NOT EXISTS idx_goal_completions_user_id ON t_goal_completions(user_id);
CREATE INDEX IF NOT EXISTS idx_goal_completions_completed_at ON t_goal_completions(completed_at);
CREATE INDEX IF NOT EXISTS idx_goal_completions_deleted ON t_goal_completions(deleted);

-- 创建复合索引
CREATE INDEX IF NOT EXISTS idx_goals_user_status ON t_goals(user_id, status);
CREATE INDEX IF NOT EXISTS idx_goal_completions_goal_completed ON t_goal_completions(goal_id, completed_at);

-- 添加注释
COMMENT ON TABLE t_goals IS '用户目标表';
COMMENT ON COLUMN t_goals.id IS '目标ID';
COMMENT ON COLUMN t_goals.user_id IS '用户ID';
COMMENT ON COLUMN t_goals.title IS '目标标题';
COMMENT ON COLUMN t_goals.description IS '目标描述';
COMMENT ON COLUMN t_goals.repeat_type IS '重复周期类型：daily-每日，weekly-每周，monthly-每月，custom-自定义';
COMMENT ON COLUMN t_goals.frequency IS '频率（每天/每周/每月多少次）';
COMMENT ON COLUMN t_goals.custom_repeat_rule IS '自定义重复规则（如每周几）';
COMMENT ON COLUMN t_goals.start_date IS '目标开始日期';
COMMENT ON COLUMN t_goals.end_date IS '目标结束日期';
COMMENT ON COLUMN t_goals.status IS '目标状态：active-激活，paused-暂停，completed-已完成，cancelled-已取消';
COMMENT ON COLUMN t_goals.completed_count IS '已完成次数';
COMMENT ON COLUMN t_goals.target_count IS '目标总次数（null表示无限制）';
COMMENT ON COLUMN t_goals.category IS '目标分类标签';
COMMENT ON COLUMN t_goals.priority IS '优先级（数字越大优先级越高）';
COMMENT ON COLUMN t_goals.has_reminder IS '是否提醒';
COMMENT ON COLUMN t_goals.reminder_time IS '提醒时间';
COMMENT ON COLUMN t_goals.reminder_settings IS '提醒设置（如每周几提醒）';

COMMENT ON TABLE t_goal_completions IS '目标完成记录表';
COMMENT ON COLUMN t_goal_completions.id IS '完成记录ID';
COMMENT ON COLUMN t_goal_completions.goal_id IS '目标ID';
COMMENT ON COLUMN t_goal_completions.user_id IS '用户ID';
COMMENT ON COLUMN t_goal_completions.completed_at IS '完成日期';
COMMENT ON COLUMN t_goal_completions.completion_count IS '完成次数';
COMMENT ON COLUMN t_goal_completions.notes IS '完成备注';
COMMENT ON COLUMN t_goal_completions.metadata IS '完成时的额外数据';
