-- =====================================================================================
-- Schema: 用户标签表
-- 位置：docs/sql/10_schema/user/ak_user_labels_v1.sql
-- 对象类型：Schema (DDL)
-- 版本：v1
-- 说明：用户标签定义，支持逻辑删除与状态管理
-- =====================================================================================

CREATE TABLE IF NOT EXISTS public.ak_user_labels (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),

  name TEXT NOT NULL,
  color TEXT NULL,
  remark TEXT NULL,
  status INT NOT NULL DEFAULT 1,

  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  deleted_at TIMESTAMPTZ NULL,

  CONSTRAINT ak_user_labels_name_length CHECK (char_length(name) >= 1)
);

CREATE UNIQUE INDEX IF NOT EXISTS ak_user_labels_name_uniq_active
  ON public.ak_user_labels (name)
  WHERE deleted_at IS NULL;

CREATE INDEX IF NOT EXISTS ak_user_labels_status_idx
  ON public.ak_user_labels (status)
  WHERE deleted_at IS NULL;

CREATE INDEX IF NOT EXISTS ak_user_labels_created_at_idx
  ON public.ak_user_labels (created_at DESC);
