-- AIA 附件持久化：数据库真相源 + 缓存加速
CREATE TABLE IF NOT EXISTS "aia_schema"."attachments" (
  "id" TEXT NOT NULL,
  "user_id" TEXT NOT NULL,
  "conversation_id" TEXT NOT NULL,
  "filename" TEXT NOT NULL,
  "mime_type" TEXT,
  "size" INTEGER NOT NULL,
  "oss_url" TEXT NOT NULL,
  "text_content" TEXT,
  "extract_status" TEXT NOT NULL DEFAULT 'success',
  "extract_error" TEXT,
  "token_count" INTEGER NOT NULL DEFAULT 0,
  "truncated" BOOLEAN NOT NULL DEFAULT false,
  "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updated_at" TIMESTAMP(3) NOT NULL,
  CONSTRAINT "attachments_pkey" PRIMARY KEY ("id")
);

CREATE INDEX IF NOT EXISTS "idx_aia_attachments_conversation_id"
  ON "aia_schema"."attachments"("conversation_id");

CREATE INDEX IF NOT EXISTS "idx_aia_attachments_user_id"
  ON "aia_schema"."attachments"("user_id");

CREATE INDEX IF NOT EXISTS "idx_aia_attachments_extract_status"
  ON "aia_schema"."attachments"("extract_status");

CREATE INDEX IF NOT EXISTS "idx_aia_attachments_created_at"
  ON "aia_schema"."attachments"("created_at");
