-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "public";

-- CreateEnum
CREATE TYPE "DocumentStatus" AS ENUM ('DRAFT', 'PUBLISHED', 'ARCHIVED', 'SCHEDULED', 'SCHEDULED_UNPUBLISH');

-- CreateEnum
CREATE TYPE "UserRole" AS ENUM ('ADMIN', 'EDITOR', 'AUTHOR', 'CLIENT');

-- CreateEnum
CREATE TYPE "ChangeType" AS ENUM ('CREATE', 'UPDATE', 'PUBLISH', 'RESTORE');

-- CreateEnum
CREATE TYPE "AuditEvent" AS ENUM ('login_success', 'login_failed', 'logout', 'password_change', 'password_reset_request', 'password_reset_complete', 'totp_enabled', 'totp_disabled', 'api_key_created', 'api_key_revoked', 'document_created', 'document_updated', 'document_published', 'document_deleted', 'media_uploaded', 'media_deleted', 'user_created', 'user_updated', 'user_deactivated', 'settings_changed', 'export_requested', 'import_completed');

-- CreateEnum
CREATE TYPE "NotificationType" AS ENUM ('DOCUMENT_PUBLISHED', 'DOCUMENT_ASSIGNED', 'REVIEW_REQUESTED', 'REVIEW_APPROVED', 'COMMENT_ADDED', 'SYSTEM_ALERT', 'WORKFLOW_STEP');

-- CreateEnum
CREATE TYPE "OrderStatus" AS ENUM ('PENDING', 'CONFIRMED', 'PROCESSING', 'SHIPPED', 'DELIVERED', 'CANCELLED', 'REFUNDED');

-- CreateEnum
CREATE TYPE "PaymentStatus" AS ENUM ('PENDING', 'AUTHORIZED', 'CAPTURED', 'FAILED', 'REFUNDED', 'PARTIALLY_REFUNDED');

-- CreateEnum
CREATE TYPE "DiscountType" AS ENUM ('PERCENTAGE', 'FIXED_AMOUNT', 'FREE_SHIPPING');

-- CreateEnum
CREATE TYPE "ProductType" AS ENUM ('PHYSICAL', 'DIGITAL', 'SUBSCRIPTION', 'BUNDLE');

-- CreateTable
CREATE TABLE "actuate_users" (
    "id" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "avatarUrl" TEXT,
    "role" "UserRole" NOT NULL DEFAULT 'AUTHOR',
    "passwordHash" TEXT,
    "totpSecret" TEXT,
    "authProvider" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "isApproved" BOOLEAN NOT NULL DEFAULT false,
    "emailVerified" BOOLEAN NOT NULL DEFAULT false,
    "lastLoginAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "actuate_oauth_accounts" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "provider" TEXT NOT NULL,
    "providerAccountId" TEXT NOT NULL,
    "accessToken" TEXT,
    "refreshToken" TEXT,
    "expiresAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "actuate_sessions" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "expiresAt" TIMESTAMP(3) NOT NULL,
    "revokedAt" TIMESTAMP(3),
    "ipAddress" TEXT,
    "userAgent" TEXT,
    "fingerprintHash" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "actuate_api_keys" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "keyHash" TEXT NOT NULL,
    "keyPrefix" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "scopes" JSONB NOT NULL,
    "ipRestrictions" JSONB,
    "expiresAt" TIMESTAMP(3),
    "lastUsedAt" TIMESTAMP(3),
    "revokedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "actuate_audit_logs" (
    "id" TEXT NOT NULL,
    "event" TEXT NOT NULL,
    "userId" TEXT,
    "ipAddress" TEXT,
    "userAgent" TEXT,
    "details" JSONB,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "actuate_documents" (
    "id" TEXT NOT NULL,
    "collection" TEXT NOT NULL,
    "data" JSONB NOT NULL,
    "status" "DocumentStatus" NOT NULL DEFAULT 'DRAFT',
    "locale" TEXT,
    "localeGroupId" TEXT,
    "createdById" TEXT NOT NULL,
    "updatedById" TEXT NOT NULL,
    "publishedAt" TIMESTAMP(3),
    "scheduledAt" TIMESTAMP(3),
    "scheduledUnpublishAt" TIMESTAMP(3),
    "deletedAt" TIMESTAMP(3),
    "searchVector" TEXT,
    "plainText" TEXT,
    "contentHash" TEXT,
    "structuredData" JSONB,
    "contentGraph" JSONB,
    "siteId" TEXT,
    "templateId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "actuate_versions" (
    "id" TEXT NOT NULL,
    "documentId" TEXT NOT NULL,
    "data" JSONB NOT NULL,
    "changedById" TEXT NOT NULL,
    "changeType" "ChangeType" NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "actuate_media" (
    "id" TEXT NOT NULL,
    "filename" TEXT NOT NULL,
    "storageKey" TEXT NOT NULL,
    "mimeType" TEXT NOT NULL,
    "fileSize" INTEGER NOT NULL,
    "width" INTEGER,
    "height" INTEGER,
    "altText" TEXT,
    "title" TEXT,
    "uploadedById" TEXT NOT NULL,
    "focalPointX" DOUBLE PRECISION,
    "focalPointY" DOUBLE PRECISION,
    "blurHash" TEXT,
    "aiAltText" TEXT,
    "aiTags" JSONB,
    "aiDescription" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "actuate_media_usages" (
    "id" TEXT NOT NULL,
    "mediaId" TEXT NOT NULL,
    "documentId" TEXT NOT NULL,
    "fieldName" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "actuate_content_locks" (
    "id" TEXT NOT NULL,
    "documentId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "lockedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expiresAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "actuate_notifications" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "message" TEXT,
    "link" TEXT,
    "read" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "actuate_content_templates" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "collection" TEXT NOT NULL,
    "templateData" JSONB NOT NULL,
    "createdById" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "actuate_sites" (
    "id" TEXT NOT NULL,
    "domain" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "configOverrides" JSONB,
    "isDefault" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "actuate_workflow_states" (
    "id" TEXT NOT NULL,
    "documentId" TEXT NOT NULL,
    "currentStep" INTEGER NOT NULL DEFAULT 0,
    "assignedToId" TEXT,
    "status" TEXT NOT NULL DEFAULT 'pending',
    "definition" JSONB NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "actuate_redirects" (
    "id" TEXT NOT NULL,
    "source" TEXT NOT NULL,
    "destination" TEXT NOT NULL,
    "statusCode" INTEGER NOT NULL DEFAULT 301,
    "isRegex" BOOLEAN NOT NULL DEFAULT false,
    "notes" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "actuate_form_submissions" (
    "id" TEXT NOT NULL,
    "formId" TEXT NOT NULL,
    "data" JSONB NOT NULL,
    "attribution" JSONB,
    "status" TEXT NOT NULL DEFAULT 'new',
    "submittedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "actuate_backup_records" (
    "id" TEXT NOT NULL,
    "filename" TEXT NOT NULL,
    "storageKey" TEXT NOT NULL,
    "sizeBytes" INTEGER NOT NULL,
    "type" TEXT NOT NULL,
    "metadata" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "actuate_users_email_key" ON "actuate_users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "actuate_oauth_accounts_provider_providerAccountId_key" ON "actuate_oauth_accounts"("provider", "providerAccountId");

-- CreateIndex
CREATE UNIQUE INDEX "actuate_sessions_token_key" ON "actuate_sessions"("token");

-- CreateIndex
CREATE UNIQUE INDEX "actuate_api_keys_keyHash_key" ON "actuate_api_keys"("keyHash");

-- CreateIndex
CREATE INDEX "actuate_audit_logs_event_idx" ON "actuate_audit_logs"("event");

-- CreateIndex
CREATE INDEX "actuate_audit_logs_userId_idx" ON "actuate_audit_logs"("userId");

-- CreateIndex
CREATE INDEX "actuate_audit_logs_timestamp_idx" ON "actuate_audit_logs"("timestamp");

-- CreateIndex
CREATE INDEX "actuate_documents_collection_idx" ON "actuate_documents"("collection");

-- CreateIndex
CREATE INDEX "actuate_documents_status_idx" ON "actuate_documents"("status");

-- CreateIndex
CREATE INDEX "actuate_documents_deletedAt_idx" ON "actuate_documents"("deletedAt");

-- CreateIndex
CREATE INDEX "actuate_documents_publishedAt_idx" ON "actuate_documents"("publishedAt");

-- CreateIndex
CREATE UNIQUE INDEX "actuate_media_storageKey_key" ON "actuate_media"("storageKey");

-- CreateIndex
CREATE UNIQUE INDEX "actuate_sites_domain_key" ON "actuate_sites"("domain");

-- CreateIndex
CREATE UNIQUE INDEX "actuate_workflow_states_documentId_key" ON "actuate_workflow_states"("documentId");

-- CreateIndex
CREATE UNIQUE INDEX "actuate_redirects_source_key" ON "actuate_redirects"("source");

-- CreateIndex
CREATE INDEX "actuate_form_submissions_formId_idx" ON "actuate_form_submissions"("formId");

-- CreateIndex
CREATE INDEX "actuate_form_submissions_status_idx" ON "actuate_form_submissions"("status");

-- AddForeignKey
ALTER TABLE "actuate_oauth_accounts" ADD CONSTRAINT "actuate_oauth_accounts_userId_fkey" FOREIGN KEY ("userId") REFERENCES "actuate_users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_sessions" ADD CONSTRAINT "actuate_sessions_userId_fkey" FOREIGN KEY ("userId") REFERENCES "actuate_users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_api_keys" ADD CONSTRAINT "actuate_api_keys_userId_fkey" FOREIGN KEY ("userId") REFERENCES "actuate_users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_documents" ADD CONSTRAINT "actuate_documents_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "actuate_users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_documents" ADD CONSTRAINT "actuate_documents_updatedById_fkey" FOREIGN KEY ("updatedById") REFERENCES "actuate_users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_versions" ADD CONSTRAINT "actuate_versions_documentId_fkey" FOREIGN KEY ("documentId") REFERENCES "actuate_documents"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_versions" ADD CONSTRAINT "actuate_versions_changedById_fkey" FOREIGN KEY ("changedById") REFERENCES "actuate_users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_media" ADD CONSTRAINT "actuate_media_uploadedById_fkey" FOREIGN KEY ("uploadedById") REFERENCES "actuate_users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_media_usages" ADD CONSTRAINT "actuate_media_usages_mediaId_fkey" FOREIGN KEY ("mediaId") REFERENCES "actuate_media"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_media_usages" ADD CONSTRAINT "actuate_media_usages_documentId_fkey" FOREIGN KEY ("documentId") REFERENCES "actuate_documents"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_content_locks" ADD CONSTRAINT "actuate_content_locks_documentId_fkey" FOREIGN KEY ("documentId") REFERENCES "actuate_documents"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_content_locks" ADD CONSTRAINT "actuate_content_locks_userId_fkey" FOREIGN KEY ("userId") REFERENCES "actuate_users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_notifications" ADD CONSTRAINT "actuate_notifications_userId_fkey" FOREIGN KEY ("userId") REFERENCES "actuate_users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_content_templates" ADD CONSTRAINT "actuate_content_templates_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "actuate_users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "actuate_workflow_states" ADD CONSTRAINT "actuate_workflow_states_assignedToId_fkey" FOREIGN KEY ("assignedToId") REFERENCES "actuate_users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

