DO $$
BEGIN
  CREATE TYPE "platform_knowledge"."ragflow_source_type" AS ENUM ('SP_DOCUMENT', 'ARTICLE');
EXCEPTION
  WHEN duplicate_object THEN NULL;
END $$;

CREATE TABLE "platform_knowledge"."sync_task_skipped_items" (
  "id" UUID NOT NULL DEFAULT gen_random_uuid(),
  "task_id" UUID NOT NULL,
  "source_type" "platform_knowledge"."ragflow_source_type" NOT NULL,
  "source_id" UUID NOT NULL,
  "filename" VARCHAR(255) NOT NULL,
  "file_extension" VARCHAR(20),
  "mime_type" VARCHAR(255),
  "reason" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT NOW(),

  CONSTRAINT "sync_task_skipped_items_pkey" PRIMARY KEY ("id")
);

CREATE INDEX "sync_task_skipped_items_task_id_idx" ON "platform_knowledge"."sync_task_skipped_items"("task_id");
CREATE INDEX "sync_task_skipped_items_source_type_idx" ON "platform_knowledge"."sync_task_skipped_items"("source_type");

ALTER TABLE "platform_knowledge"."sync_task_skipped_items"
ADD CONSTRAINT "sync_task_skipped_items_task_id_fkey" FOREIGN KEY ("task_id")
REFERENCES "platform_knowledge"."sync_tasks"("id") ON DELETE CASCADE ON UPDATE CASCADE;
