CREATE TABLE "crm_document_libraries" (
  "id" text PRIMARY KEY NOT NULL,
  "organization_id" text NOT NULL,
  "code" text NOT NULL,
  "name" text NOT NULL,
  "document_type" text NOT NULL,
  "description" text,
  "brand" text NOT NULL,
  "language" text NOT NULL,
  "product_type" text NOT NULL,
  "status" text DEFAULT 'active' NOT NULL,
  "is_active" boolean DEFAULT true NOT NULL,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL,
  "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
  "deleted_at" timestamp with time zone,
  "created_by" text NOT NULL,
  "updated_by" text NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX "crm_document_libraries_org_code_idx" ON "crm_document_libraries" USING btree ("organization_id","code");
--> statement-breakpoint
CREATE TABLE "crm_document_library_versions" (
  "id" text PRIMARY KEY NOT NULL,
  "organization_id" text NOT NULL,
  "library_id" text NOT NULL,
  "version" text NOT NULL,
  "file_name" text NOT NULL,
  "storage_provider" text NOT NULL,
  "storage_key" text NOT NULL,
  "mime_type" text NOT NULL,
  "file_size" integer NOT NULL,
  "checksum" text NOT NULL,
  "page_count" integer NOT NULL,
  "status" text DEFAULT 'draft' NOT NULL,
  "is_active" boolean DEFAULT false NOT NULL,
  "published_at" timestamp with time zone,
  "published_by" text,
  "archived_at" timestamp with time zone,
  "archived_by" text,
  "created_at" timestamp with time zone DEFAULT now() NOT NULL,
  "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
  "deleted_at" timestamp with time zone,
  "created_by" text NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX "crm_document_library_versions_library_version_idx" ON "crm_document_library_versions" USING btree ("library_id","version");
--> statement-breakpoint
CREATE UNIQUE INDEX "crm_document_library_versions_active_library_idx"
  ON "crm_document_library_versions" USING btree ("library_id")
  WHERE "is_active" = true AND "deleted_at" IS NULL;
