-- CreateEnum
CREATE TYPE "CampaignModerationStatus" AS ENUM ('PENDING_REVIEW', 'APPROVED', 'REJECTED', 'CHANGES_REQUESTED');

-- CreateEnum
CREATE TYPE "BuildingType" AS ENUM ('SINGLE_FAMILY', 'MULTI_UNIT', 'MIXED_USE', 'COMMERCIAL');

-- CreateEnum
CREATE TYPE "LocationHistoryAction" AS ENUM ('CREATED', 'UPDATED', 'GEOCODED', 'BULK_GEOCODED', 'MOVED_ON_MAP', 'IMPORTED_CSV', 'IMPORTED_NAR');

-- CreateEnum
CREATE TYPE "RecurrenceFrequency" AS ENUM ('DAILY', 'WEEKLY', 'MONTHLY');

-- CreateEnum
CREATE TYPE "EmailTemplateCategory" AS ENUM ('INFLUENCE', 'MAP', 'SYSTEM', 'PAYMENT');

-- CreateEnum
CREATE TYPE "EmailTemplateVariableType" AS ENUM ('TEXT', 'VIDEO');

-- CreateEnum
CREATE TYPE "DirectoryType" AS ENUM ('studios', 'gifs', 'private', 'inbox', 'curated', 'playback', 'compilations', 'videos', 'highlights');

-- CreateEnum
CREATE TYPE "ResourceCategory" AS ENUM ('gpu_ai', 'gpu_encode', 'cpu');

-- CreateEnum
CREATE TYPE "JobStatus" AS ENUM ('pending', 'queued', 'running', 'completed', 'failed', 'cancelled');

-- CreateEnum
CREATE TYPE "ReactionType" AS ENUM ('like', 'love', 'laugh', 'wow', 'sad', 'angry');

-- CreateEnum
CREATE TYPE "WordFilterLevel" AS ENUM ('low', 'medium', 'high', 'custom');

-- CreateEnum
CREATE TYPE "ReportType" AS ENUM ('inappropriate', 'spam', 'copyright', 'illegal', 'false_info', 'other');

-- CreateEnum
CREATE TYPE "ReportStatus" AS ENUM ('pending', 'reviewed', 'actioned', 'dismissed');

-- CreateEnum
CREATE TYPE "FriendshipStatus" AS ENUM ('pending', 'accepted', 'declined');

-- CreateEnum
CREATE TYPE "SocialPlatform" AS ENUM ('twitter', 'instagram', 'onlyfans', 'fansly', 'reddit', 'discord', 'tiktok', 'youtube', 'twitch', 'snapchat', 'linktree', 'custom');

-- CreateEnum
CREATE TYPE "UserUploadStatus" AS ENUM ('pending', 'approved', 'rejected');

-- CreateEnum
CREATE TYPE "UploadInviteStatus" AS ENUM ('active', 'inactive', 'expired');

-- CreateEnum
CREATE TYPE "DigestStatus" AS ENUM ('pending', 'scene_detection', 'extracting', 'analyzing', 'face_detection', 'transcribing', 'segmenting', 'synthesizing', 'completed', 'failed', 'cancelled');

-- CreateEnum
CREATE TYPE "ClipType" AS ENUM ('hook', 'intro', 'action', 'climax', 'highlight');

-- CreateEnum
CREATE TYPE "ClipStatus" AS ENUM ('pending', 'processing', 'completed', 'failed');

-- CreateEnum
CREATE TYPE "ClipSource" AS ENUM ('machine', 'manual');

-- CreateEnum
CREATE TYPE "CaptionPosition" AS ENUM ('bottom', 'top');

-- CreateEnum
CREATE TYPE "CaptionSize" AS ENUM ('small', 'medium', 'large');

-- CreateEnum
CREATE TYPE "SegmentType" AS ENUM ('scene', 'tag_change', 'vocal', 'fixed_interval');

-- CreateEnum
CREATE TYPE "VocalCategory" AS ENUM ('dialogue', 'dirty_talk', 'climax', 'interview', 'moan');

-- CreateEnum
CREATE TYPE "SuggestedTagStatus" AS ENUM ('pending', 'approved', 'rejected', 'mapped');

-- CreateEnum
CREATE TYPE "WatchPartyStatus" AS ENUM ('active', 'completed', 'cancelled');

-- CreateEnum
CREATE TYPE "PipelineStatus" AS ENUM ('draft', 'active', 'paused', 'completed', 'failed');

-- CreateEnum
CREATE TYPE "PipelineStepStatus" AS ENUM ('pending', 'running', 'completed', 'failed', 'skipped');

-- CreateEnum
CREATE TYPE "SubscriptionStatus" AS ENUM ('none', 'active', 'grace_period', 'delinquent', 'lifetime', 'cancelled');

-- CreateEnum
CREATE TYPE "InvoiceStatus" AS ENUM ('pending', 'paid', 'failed', 'refunded');

-- CreateEnum
CREATE TYPE "PaymentStatus" AS ENUM ('pending', 'succeeded', 'failed', 'refunded');

-- CreateEnum
CREATE TYPE "PaymentMethod" AS ENUM ('card', 'bank_transfer', 'crypto', 'stripe');

-- CreateEnum
CREATE TYPE "ProductType" AS ENUM ('DIGITAL', 'EVENT', 'DONATION');

-- CreateEnum
CREATE TYPE "OrderStatus" AS ENUM ('PENDING', 'COMPLETED', 'FAILED', 'REFUNDED');

-- CreateEnum
CREATE TYPE "NotificationType" AS ENUM ('friend_request', 'friend_accepted', 'poke', 'comment', 'upload_approved', 'upload_rejected', 'achievement', 'system');

-- CreateEnum
CREATE TYPE "DonationPageStatus" AS ENUM ('DRAFT', 'ACTIVE', 'PAUSED', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "DocsCommentStatus" AS ENUM ('PENDING', 'APPROVED', 'REJECTED');

-- CreateEnum
CREATE TYPE "SmsContactListStatus" AS ENUM ('ACTIVE', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "SmsCampaignStatus" AS ENUM ('DRAFT', 'RUNNING', 'PAUSED', 'COMPLETED', 'FAILED');

-- CreateEnum
CREATE TYPE "SmsMessageDirection" AS ENUM ('OUTBOUND', 'INBOUND');

-- CreateEnum
CREATE TYPE "SmsMessageStatus" AS ENUM ('PENDING', 'SENT', 'FAILED', 'DELIVERED');

-- CreateEnum
CREATE TYPE "SmsResponseType" AS ENUM ('POSITIVE', 'NEGATIVE', 'QUESTION', 'OPT_OUT', 'NEUTRAL');

-- CreateEnum
CREATE TYPE "SmsConversationStatus" AS ENUM ('ACTIVE', 'OPTED_OUT', 'CLOSED');

-- CreateEnum
CREATE TYPE "ContactSource" AS ENUM ('USER', 'ADDRESS_OCCUPANT', 'CAMPAIGN_SENDER', 'SHIFT_SIGNUP', 'SMS_CONTACT', 'DONATION', 'MANUAL');

-- CreateEnum
CREATE TYPE "ConnectionType" AS ENUM ('HOUSEHOLD', 'FAMILY', 'COLLEAGUE', 'REFERRED_BY', 'CUSTOM');

-- CreateEnum
CREATE TYPE "ContactActivityType" AS ENUM ('EMAIL_SENT', 'RESPONSE_SUBMITTED', 'SHIFT_SIGNUP', 'CANVASS_VISIT', 'DONATION', 'PURCHASE', 'SMS_SENT', 'SMS_RECEIVED', 'VIDEO_VIEW', 'NOTE_ADDED', 'CONTACT_MERGED', 'PROFILE_SELF_EDIT', 'PROFILE_PHOTO_UPDATED', 'USER_LOGIN');

-- AlterEnum
ALTER TYPE "UserCreatedVia" ADD VALUE 'SELF_REGISTRATION';

-- AlterEnum
-- This migration adds more than one value to an enum.
-- With PostgreSQL versions 11 and earlier, this is not possible
-- in a single migration. This can be worked around by creating
-- multiple migrations, each migration adding only one value to
-- the enum.


ALTER TYPE "UserStatus" ADD VALUE 'PENDING_VERIFICATION';
ALTER TYPE "UserStatus" ADD VALUE 'PENDING_APPROVAL';

-- DropForeignKey
ALTER TABLE "canvass_visits" DROP CONSTRAINT "canvass_visits_locationId_fkey";

-- DropIndex
DROP INDEX "canvass_visits_locationId_idx";

-- AlterTable
ALTER TABLE "campaigns" ADD COLUMN     "coverVideoId" INTEGER,
ADD COLUMN     "isUserGenerated" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "moderationNotes" TEXT,
ADD COLUMN     "moderationStatus" "CampaignModerationStatus",
ADD COLUMN     "rejectionReason" TEXT,
ADD COLUMN     "reviewedAt" TIMESTAMP(3),
ADD COLUMN     "reviewedByUserId" TEXT;

-- AlterTable
ALTER TABLE "canvass_visits" DROP COLUMN "locationId",
ADD COLUMN     "addressId" TEXT NOT NULL;

-- AlterTable
ALTER TABLE "landing_pages" ADD COLUMN     "mkdocsSkipExport" BOOLEAN NOT NULL DEFAULT false;

-- AlterTable
ALTER TABLE "locations" DROP COLUMN "email",
DROP COLUMN "firstName",
DROP COLUMN "lastName",
DROP COLUMN "notes",
DROP COLUMN "phone",
DROP COLUMN "sign",
DROP COLUMN "signSize",
DROP COLUMN "supportLevel",
DROP COLUMN "unitNumber",
ADD COLUMN     "buildingNotes" TEXT,
ADD COLUMN     "buildingType" "BuildingType" NOT NULL DEFAULT 'SINGLE_FAMILY',
ADD COLUMN     "locGuid" TEXT,
ADD COLUMN     "totalUnits" INTEGER NOT NULL DEFAULT 1,
ALTER COLUMN "latitude" SET NOT NULL,
ALTER COLUMN "longitude" SET NOT NULL,
ALTER COLUMN "address" SET NOT NULL;

-- AlterTable
ALTER TABLE "map_settings" ADD COLUMN     "publicMapEnabled" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "publicShowAddresses" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "publicShowCuts" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "publicShowEvents" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "publicShowLocations" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "publicShowSignInfo" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "publicShowSupportLevels" BOOLEAN NOT NULL DEFAULT true;

-- AlterTable
ALTER TABLE "shifts" ADD COLUMN     "isException" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "seriesId" TEXT;

-- AlterTable
ALTER TABLE "site_settings" ADD COLUMN     "autoApproveVerifiedUsers" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "auto_sync_people_to_map" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "enableEmailVerification" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "enablePayments" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "enablePublicRegistration" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "enable_chat" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "enable_docs_comments" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "enable_gallery_ads" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "enable_media_features" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "enable_people" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "enable_sms" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "enable_user_provisioning" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "giteaApiToken" TEXT NOT NULL DEFAULT '',
ADD COLUMN     "giteaCommentsRepoName" TEXT NOT NULL DEFAULT 'docs-comments',
ADD COLUMN     "giteaCommentsRepoOwner" TEXT NOT NULL DEFAULT '',
ADD COLUMN     "giteaOauthClientId" TEXT NOT NULL DEFAULT '',
ADD COLUMN     "giteaOauthClientSecret" TEXT NOT NULL DEFAULT '',
ADD COLUMN     "nav_config" JSONB,
ADD COLUMN     "notifyAdminResponseSubmitted" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "notifyAdminShiftSignup" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "notifyAdminSignRequested" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "notifyVolunteerCancellation" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "notifyVolunteerSessionSummary" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "notifyVolunteerShiftReminder" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "provision_gitea" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "provision_gitea_timing" TEXT NOT NULL DEFAULT 'lazy',
ADD COLUMN     "provision_listmonk" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "provision_listmonk_timing" TEXT NOT NULL DEFAULT 'eager',
ADD COLUMN     "provision_vaultwarden" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "provision_vaultwarden_timing" TEXT NOT NULL DEFAULT 'lazy',
ADD COLUMN     "sms_tailscale_api_key" TEXT NOT NULL DEFAULT '',
ADD COLUMN     "sms_tailscale_device_id" TEXT NOT NULL DEFAULT '',
ADD COLUMN     "sms_tailscale_device_name" TEXT NOT NULL DEFAULT '',
ADD COLUMN     "sms_tailscale_tailnet" TEXT NOT NULL DEFAULT '',
ADD COLUMN     "sms_termux_api_key" TEXT NOT NULL DEFAULT '',
ADD COLUMN     "sms_termux_api_url" TEXT NOT NULL DEFAULT '',
ADD COLUMN     "smtpActiveProvider" TEXT NOT NULL DEFAULT 'mailhog';

-- AlterTable
ALTER TABLE "users" ADD COLUMN     "roles" JSONB NOT NULL DEFAULT '[]';

-- CreateTable
CREATE TABLE "addresses" (
    "id" TEXT NOT NULL,
    "locationId" TEXT NOT NULL,
    "unitNumber" TEXT,
    "addrGuid" TEXT,
    "firstName" TEXT,
    "lastName" TEXT,
    "email" TEXT,
    "phone" TEXT,
    "supportLevel" "SupportLevel",
    "sign" BOOLEAN NOT NULL DEFAULT false,
    "signSize" TEXT,
    "notes" TEXT,
    "createdByUserId" TEXT,
    "updatedByUserId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "location_history" (
    "id" TEXT NOT NULL,
    "locationId" TEXT NOT NULL,
    "userId" TEXT,
    "action" "LocationHistoryAction" NOT NULL,
    "field" TEXT,
    "oldValue" TEXT,
    "newValue" TEXT,
    "metadata" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "shift_series" (
    "id" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "startTime" TEXT NOT NULL,
    "endTime" TEXT NOT NULL,
    "location" TEXT,
    "maxVolunteers" INTEGER NOT NULL,
    "isPublic" BOOLEAN NOT NULL DEFAULT false,
    "cutId" TEXT,
    "frequency" "RecurrenceFrequency" NOT NULL,
    "daysOfWeek" JSONB,
    "startDate" DATE NOT NULL,
    "endDate" DATE,
    "createdBy" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "email_templates" (
    "id" TEXT NOT NULL,
    "key" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "category" "EmailTemplateCategory" NOT NULL,
    "subjectLine" TEXT NOT NULL,
    "htmlContent" TEXT NOT NULL,
    "textContent" TEXT NOT NULL,
    "isSystem" BOOLEAN NOT NULL DEFAULT false,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdByUserId" TEXT NOT NULL,
    "updatedByUserId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "email_template_variables" (
    "id" TEXT NOT NULL,
    "templateId" TEXT NOT NULL,
    "key" TEXT NOT NULL,
    "label" TEXT NOT NULL,
    "description" TEXT,
    "type" "EmailTemplateVariableType" NOT NULL DEFAULT 'TEXT',
    "videoId" INTEGER,
    "isRequired" BOOLEAN NOT NULL DEFAULT true,
    "isConditional" BOOLEAN NOT NULL DEFAULT false,
    "sampleValue" TEXT,
    "sortOrder" INTEGER NOT NULL DEFAULT 0,

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

-- CreateTable
CREATE TABLE "email_template_versions" (
    "id" TEXT NOT NULL,
    "templateId" TEXT NOT NULL,
    "versionNumber" INTEGER NOT NULL,
    "subjectLine" TEXT NOT NULL,
    "htmlContent" TEXT NOT NULL,
    "textContent" TEXT NOT NULL,
    "changeNotes" TEXT,
    "createdByUserId" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "email_template_test_logs" (
    "id" TEXT NOT NULL,
    "templateId" TEXT NOT NULL,
    "recipientEmail" TEXT NOT NULL,
    "testData" JSONB NOT NULL,
    "success" BOOLEAN NOT NULL,
    "errorMessage" TEXT,
    "messageId" TEXT,
    "sentByUserId" TEXT NOT NULL,
    "sentAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "videos" (
    "id" SERIAL NOT NULL,
    "path" TEXT NOT NULL,
    "filename" TEXT NOT NULL,
    "producer" TEXT,
    "creator" TEXT,
    "title" TEXT,
    "duration_seconds" INTEGER,
    "quality" TEXT,
    "orientation" TEXT,
    "has_audio" BOOLEAN DEFAULT true,
    "file_size" BIGINT,
    "file_hash" TEXT,
    "width" INTEGER,
    "height" INTEGER,
    "last_validated" TIMESTAMP(3),
    "is_valid" BOOLEAN DEFAULT true,
    "thumbnail_path" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "tags" JSONB,
    "directory_type" "DirectoryType",
    "public_view_count" INTEGER,
    "public_upvote_count" INTEGER,
    "public_comment_count" INTEGER,
    "public_completion_count" INTEGER,
    "public_total_watch_time" INTEGER,
    "moved_from_public_at" TIMESTAMP(3),
    "original_filename" TEXT,
    "original_path" TEXT,
    "standardized_at" TIMESTAMP(3),
    "is_published" BOOLEAN NOT NULL DEFAULT false,
    "published_at" TIMESTAMP(3),
    "category" TEXT,
    "is_short" BOOLEAN NOT NULL DEFAULT false,
    "is_locked" BOOLEAN NOT NULL DEFAULT false,
    "locked_at" TIMESTAMP(3),
    "locked_by_id" TEXT,
    "view_count" INTEGER NOT NULL DEFAULT 0,
    "upvote_count" INTEGER NOT NULL DEFAULT 0,
    "comment_count" INTEGER NOT NULL DEFAULT 0,
    "finish_count" INTEGER NOT NULL DEFAULT 0,
    "total_watch_time" INTEGER NOT NULL DEFAULT 0,
    "scheduled_publish_at" TIMESTAMP(3),
    "scheduled_unpublish_at" TIMESTAMP(3),
    "unique_viewers" INTEGER NOT NULL DEFAULT 0,
    "total_watch_time_seconds" INTEGER NOT NULL DEFAULT 0,
    "average_watch_time_seconds" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "completion_rate" DECIMAL(5,2) NOT NULL DEFAULT 0,
    "access_level" TEXT NOT NULL DEFAULT 'free',
    "position" INTEGER DEFAULT 0,
    "uploader_id" TEXT,

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

-- CreateTable
CREATE TABLE "compilations" (
    "id" SERIAL NOT NULL,
    "filename" TEXT NOT NULL,
    "path" TEXT,
    "duration_seconds" INTEGER,
    "video_ids" JSONB,
    "settings" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "jobs" (
    "id" SERIAL NOT NULL,
    "type" TEXT NOT NULL,
    "status" "JobStatus" DEFAULT 'pending',
    "progress" INTEGER DEFAULT 0,
    "log" TEXT,
    "params" JSONB,
    "started_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "resource_category" "ResourceCategory" DEFAULT 'cpu',
    "vram_required" INTEGER DEFAULT 0,
    "queue_position" INTEGER,
    "waiting_reason" TEXT,
    "priority" INTEGER DEFAULT 5,
    "pipeline_id" INTEGER,
    "pipeline_step_id" INTEGER,

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

-- CreateTable
CREATE TABLE "sessions" (
    "id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "last_seen_at" TIMESTAMP(3),
    "ip_address" TEXT,
    "user_agent" TEXT,
    "device_type" TEXT,
    "browser" TEXT,
    "os" TEXT,
    "country" TEXT,
    "country_name" TEXT,
    "region" TEXT,
    "city" TEXT,
    "timezone" TEXT,
    "latitude" REAL,
    "longitude" REAL,
    "user_id" TEXT,
    "first_seen_at" TIMESTAMP(3),
    "visit_count" INTEGER DEFAULT 1,

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

-- CreateTable
CREATE TABLE "upvotes" (
    "id" SERIAL NOT NULL,
    "media_id" INTEGER NOT NULL,
    "session_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "comments" (
    "id" SERIAL NOT NULL,
    "media_id" INTEGER NOT NULL,
    "session_id" TEXT NOT NULL,
    "user_id" TEXT,
    "content" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "safety_status" TEXT DEFAULT 'pending',
    "safety_checked_at" TIMESTAMP(3),
    "safety_categories" JSONB,
    "safety_reasoning" TEXT,
    "is_hidden" BOOLEAN DEFAULT false,
    "hidden_at" TIMESTAMP(3),
    "hidden_reason" TEXT,
    "moderation_notes" TEXT,

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

-- CreateTable
CREATE TABLE "views" (
    "id" SERIAL NOT NULL,
    "media_id" INTEGER NOT NULL,
    "session_id" TEXT NOT NULL,
    "watch_time_seconds" INTEGER DEFAULT 0,
    "last_updated" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "auth_tokens" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "session_bans" (
    "id" SERIAL NOT NULL,
    "session_id" TEXT NOT NULL,
    "reason" TEXT,
    "banned_by" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expires_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "comment_moderation" (
    "id" SERIAL NOT NULL,
    "comment_id" INTEGER NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'pending',
    "moderated_by" TEXT,
    "moderated_at" TIMESTAMP(3),
    "reason" TEXT,

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

-- CreateTable
CREATE TABLE "email_verification_tokens" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "password_reset_tokens" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "used_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "email_change_tokens" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "new_email" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "user_achievements" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "achievement_id" TEXT NOT NULL,
    "unlocked_at" TIMESTAMP(3) NOT NULL,
    "progress" INTEGER DEFAULT 0,
    "notified" BOOLEAN DEFAULT false,

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

-- CreateTable
CREATE TABLE "user_stats" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "total_watch_time_seconds" INTEGER DEFAULT 0,
    "total_videos_watched" INTEGER DEFAULT 0,
    "total_upvotes_given" INTEGER DEFAULT 0,
    "total_comments_made" INTEGER DEFAULT 0,
    "total_finishes" INTEGER DEFAULT 0,
    "current_day_streak" INTEGER DEFAULT 0,
    "longest_day_streak" INTEGER DEFAULT 0,
    "last_active_date" TEXT,
    "longest_single_session" INTEGER DEFAULT 0,
    "categories_completed" JSONB,
    "night_owl_count" INTEGER DEFAULT 0,
    "early_bird_count" INTEGER DEFAULT 0,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "user_finishes" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "media_id" INTEGER,
    "session_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "video_reactions" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "media_id" INTEGER NOT NULL,
    "reaction_type" "ReactionType" NOT NULL,
    "video_timestamp" INTEGER NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "highlight_cooldowns" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "last_generated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "user_daily_activity" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "activity_date" TEXT NOT NULL,
    "watch_time_seconds" INTEGER DEFAULT 0,
    "videos_watched" INTEGER DEFAULT 0,
    "first_activity_hour" INTEGER,
    "created_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "chat_thread_read_status" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "media_id" INTEGER NOT NULL,
    "last_seen_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "app_settings" (
    "key" TEXT NOT NULL,
    "value" TEXT NOT NULL,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "app_settings_pkey" PRIMARY KEY ("key")
);

-- CreateTable
CREATE TABLE "rate_limits" (
    "id" SERIAL NOT NULL,
    "key" TEXT NOT NULL,
    "count" INTEGER NOT NULL DEFAULT 1,
    "reset_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "moderation_word_lists" (
    "id" SERIAL NOT NULL,
    "level" "WordFilterLevel" NOT NULL,
    "word" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_by" TEXT,

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

-- CreateTable
CREATE TABLE "content_reports" (
    "id" SERIAL NOT NULL,
    "media_id" INTEGER NOT NULL,
    "session_id" TEXT,
    "user_id" TEXT,
    "report_type" "ReportType" NOT NULL,
    "description" TEXT,
    "status" "ReportStatus" NOT NULL DEFAULT 'pending',
    "resolved_by" TEXT,
    "resolved_at" TIMESTAMP(3),
    "resolution_notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "playlists" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "is_public" BOOLEAN DEFAULT false,
    "share_token" TEXT,
    "thumbnail_media_id" INTEGER,
    "video_count" INTEGER DEFAULT 0,
    "total_duration_seconds" INTEGER DEFAULT 0,
    "view_count" INTEGER DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "playlist_videos" (
    "id" SERIAL NOT NULL,
    "playlist_id" INTEGER NOT NULL,
    "media_id" INTEGER NOT NULL,
    "position" INTEGER NOT NULL DEFAULT 0,
    "added_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "featured_playlists" (
    "id" SERIAL NOT NULL,
    "playlist_id" INTEGER NOT NULL,
    "position" INTEGER NOT NULL DEFAULT 0,
    "featured_by" TEXT,
    "featured_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "playlist_views" (
    "id" SERIAL NOT NULL,
    "playlist_id" INTEGER NOT NULL,
    "session_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "ads" (
    "id" SERIAL NOT NULL,
    "type" TEXT NOT NULL,
    "variant" TEXT,
    "image_path" TEXT,
    "link_url" TEXT,
    "title" TEXT,
    "subtitle" TEXT,
    "cta_text" TEXT,
    "cta_style" TEXT DEFAULT 'primary',
    "bg_color" TEXT,
    "icon_emoji" TEXT,
    "is_system_ad" BOOLEAN NOT NULL DEFAULT false,
    "frequency" INTEGER NOT NULL DEFAULT 6,
    "visibility" TEXT NOT NULL DEFAULT 'everyone',
    "is_active" BOOLEAN DEFAULT true,
    "position" INTEGER DEFAULT 0,
    "impression_count" INTEGER DEFAULT 0,
    "click_count" INTEGER DEFAULT 0,
    "start_date" TIMESTAMP(3),
    "end_date" TIMESTAMP(3),
    "product_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "ad_impressions" (
    "id" SERIAL NOT NULL,
    "ad_id" INTEGER NOT NULL,
    "session_id" TEXT,
    "user_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "ad_clicks" (
    "id" SERIAL NOT NULL,
    "ad_id" INTEGER NOT NULL,
    "session_id" TEXT,
    "user_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "friendships" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "friend_id" TEXT NOT NULL,
    "status" "FriendshipStatus" NOT NULL DEFAULT 'pending',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "accepted_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "user_blocks" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "blocked_user_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "pokes" (
    "id" SERIAL NOT NULL,
    "from_user_id" TEXT NOT NULL,
    "to_user_id" TEXT NOT NULL,
    "is_read" BOOLEAN DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "video_recommendations" (
    "id" SERIAL NOT NULL,
    "from_user_id" TEXT NOT NULL,
    "to_user_id" TEXT NOT NULL,
    "media_id" INTEGER NOT NULL,
    "message" TEXT,
    "is_read" BOOLEAN DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "user_presence" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "is_online" BOOLEAN DEFAULT false,
    "current_media_id" INTEGER,
    "last_activity_at" TIMESTAMP(3),
    "last_video_change_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "user_gallery_images" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "filename" TEXT NOT NULL,
    "original_filename" TEXT,
    "position" INTEGER NOT NULL DEFAULT 0,
    "uploaded_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "user_social_links" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "platform" "SocialPlatform" NOT NULL,
    "url" TEXT NOT NULL,
    "display_name" TEXT,
    "position" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "privacy_settings" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "show_online_status" BOOLEAN DEFAULT true,
    "show_currently_watching" BOOLEAN DEFAULT true,
    "show_in_friend_activity" BOOLEAN DEFAULT true,
    "anonymize_public_comments" BOOLEAN DEFAULT false,
    "hide_public_reactions" BOOLEAN DEFAULT false,
    "hide_public_finishes" BOOLEAN DEFAULT false,
    "allow_friend_requests" BOOLEAN DEFAULT true,
    "close_friends_only_watching" BOOLEAN DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "close_friends" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "close_friend_id" TEXT NOT NULL,
    "added_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "user_uploads" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "filename" TEXT NOT NULL,
    "original_filename" TEXT,
    "path" TEXT NOT NULL,
    "duration_seconds" INTEGER,
    "quality" TEXT,
    "orientation" TEXT,
    "file_size" BIGINT,
    "thumbnail_path" TEXT,
    "status" "UserUploadStatus" NOT NULL DEFAULT 'pending',
    "reviewed_by" TEXT,
    "reviewed_at" TIMESTAMP(3),
    "review_notes" TEXT,
    "public_media_id" INTEGER,
    "upload_invite_id" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "upload_invites" (
    "id" SERIAL NOT NULL,
    "code" TEXT NOT NULL,
    "label" TEXT,
    "created_by" TEXT NOT NULL,
    "status" "UploadInviteStatus" NOT NULL DEFAULT 'active',
    "max_uploads" INTEGER,
    "upload_count" INTEGER NOT NULL DEFAULT 0,
    "expires_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "tag_categories" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "display_order" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "tags" (
    "id" SERIAL NOT NULL,
    "category_id" INTEGER NOT NULL,
    "name" TEXT NOT NULL,
    "display_order" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "public_media_tags" (
    "id" SERIAL NOT NULL,
    "media_id" INTEGER NOT NULL,
    "tag_id" INTEGER NOT NULL,
    "added_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "user_upload_suggested_tags" (
    "id" SERIAL NOT NULL,
    "upload_id" INTEGER NOT NULL,
    "tag_id" INTEGER NOT NULL,
    "suggested_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "user_tag_preferences" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "tag_id" INTEGER NOT NULL,
    "saved_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "public_media_performers" (
    "id" SERIAL NOT NULL,
    "media_id" INTEGER NOT NULL,
    "performer_id" INTEGER NOT NULL,
    "added_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "video_digests" (
    "id" SERIAL NOT NULL,
    "video_id" INTEGER NOT NULL,
    "job_id" INTEGER,
    "status" "DigestStatus" NOT NULL DEFAULT 'pending',
    "progress" INTEGER DEFAULT 0,
    "frame_count" INTEGER,
    "config" JSONB,
    "frame_analyses" JSONB,
    "transcript" JSONB,
    "tags" JSONB,
    "suggested_clips" JSONB,
    "ad_cut_spec" JSONB,
    "stage_results" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "started_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),
    "error" TEXT,

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

-- CreateTable
CREATE TABLE "digest_video_tags" (
    "id" SERIAL NOT NULL,
    "digest_id" INTEGER NOT NULL,
    "video_id" INTEGER NOT NULL,
    "category" TEXT NOT NULL,
    "value" TEXT NOT NULL,
    "confidence" INTEGER,
    "source" TEXT DEFAULT 'digest',
    "evidence" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "digest_selected_clips" (
    "id" SERIAL NOT NULL,
    "digest_id" INTEGER NOT NULL,
    "video_id" INTEGER NOT NULL,
    "clip_type" "ClipType" NOT NULL,
    "start_time" INTEGER NOT NULL,
    "end_time" INTEGER NOT NULL,
    "duration" INTEGER NOT NULL,
    "reason" TEXT,
    "interest_score" INTEGER,
    "position" TEXT,
    "transcript_hint" TEXT,
    "tags" JSONB,
    "source" "ClipSource" NOT NULL DEFAULT 'machine',
    "is_included" INTEGER NOT NULL DEFAULT 1,
    "is_hook" INTEGER NOT NULL DEFAULT 0,
    "sequence_order" INTEGER NOT NULL DEFAULT 0,
    "hook_source_clip_id" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "digest_generated_clips" (
    "id" SERIAL NOT NULL,
    "selected_clip_id" INTEGER NOT NULL,
    "digest_id" INTEGER NOT NULL,
    "folder_id" INTEGER,
    "clip_path" TEXT,
    "gif_path" TEXT,
    "status" "ClipStatus" NOT NULL DEFAULT 'pending',
    "error" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMP(3),
    "published_to_public_media_id" INTEGER,
    "published_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "digest_compilations" (
    "id" SERIAL NOT NULL,
    "digest_id" INTEGER NOT NULL,
    "video_id" INTEGER NOT NULL,
    "folder_id" INTEGER,
    "filename" TEXT NOT NULL,
    "name" TEXT,
    "path" TEXT NOT NULL,
    "duration_seconds" INTEGER,
    "orientation" TEXT,
    "status" TEXT NOT NULL DEFAULT 'pending',
    "error" TEXT,
    "has_captions" INTEGER NOT NULL DEFAULT 0,
    "caption_style" JSONB,
    "closing_ad_path" TEXT,
    "closing_ad_duration" INTEGER,
    "tags" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "digest_output_folders" (
    "id" SERIAL NOT NULL,
    "digest_id" INTEGER NOT NULL,
    "video_id" INTEGER NOT NULL,
    "folder_path" TEXT NOT NULL,
    "folder_name" TEXT NOT NULL,
    "folder_type" TEXT NOT NULL DEFAULT 'clips',
    "clip_count" INTEGER DEFAULT 0,
    "compilation_count" INTEGER DEFAULT 0,
    "total_size" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "digest_generated_scenes" (
    "id" SERIAL NOT NULL,
    "digest_id" INTEGER NOT NULL,
    "video_id" INTEGER NOT NULL,
    "folder_id" INTEGER,
    "scene_number" INTEGER NOT NULL,
    "scene_path" TEXT,
    "start_time" REAL NOT NULL,
    "end_time" REAL NOT NULL,
    "duration" REAL NOT NULL,
    "tags" JSONB,
    "dominant_position" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "published_to_public_media_id" INTEGER,
    "published_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "video_scene_cuts" (
    "id" SERIAL NOT NULL,
    "video_id" INTEGER NOT NULL,
    "cuts" JSONB NOT NULL,
    "scene_count" INTEGER NOT NULL,
    "duration" REAL NOT NULL,
    "detector" TEXT NOT NULL DEFAULT 'content',
    "threshold" REAL NOT NULL DEFAULT 27.0,
    "transnet_cuts" JSONB,
    "pyscenedetect_cuts" JSONB,
    "clip_cuts" JSONB,
    "merged_cuts" JSONB,
    "analysis_metadata" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "video_tag_timeline" (
    "id" SERIAL NOT NULL,
    "video_id" INTEGER NOT NULL,
    "category" TEXT NOT NULL,
    "value" TEXT NOT NULL,
    "start_time" REAL NOT NULL,
    "end_time" REAL NOT NULL,
    "confidence" INTEGER,
    "source" TEXT DEFAULT 'digest',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "video_segments" (
    "id" SERIAL NOT NULL,
    "video_id" INTEGER NOT NULL,
    "segment_type" "SegmentType" NOT NULL,
    "start_time" REAL NOT NULL,
    "end_time" REAL NOT NULL,
    "duration" REAL NOT NULL,
    "tags" JSONB,
    "vocal_category" "VocalCategory",
    "transcript" TEXT,
    "dominant_position" TEXT,
    "interest_score" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "video_tags" (
    "id" SERIAL NOT NULL,
    "video_id" INTEGER NOT NULL,
    "category" TEXT NOT NULL,
    "value" TEXT NOT NULL,
    "confidence" INTEGER,
    "source" TEXT DEFAULT 'digest',
    "evidence" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "digest_suggested_tags" (
    "id" SERIAL NOT NULL,
    "digest_id" INTEGER NOT NULL,
    "category" TEXT NOT NULL,
    "value" TEXT NOT NULL,
    "confidence" INTEGER,
    "evidence" JSONB,
    "status" "SuggestedTagStatus" NOT NULL DEFAULT 'pending',
    "mapped_tag_id" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "reviewed_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "digest_clip_tags" (
    "id" SERIAL NOT NULL,
    "digest_id" INTEGER NOT NULL,
    "clip_id" INTEGER NOT NULL,
    "video_id" INTEGER NOT NULL,
    "category" TEXT NOT NULL,
    "value" TEXT NOT NULL,
    "confidence" INTEGER,
    "source" TEXT DEFAULT 'clip_analysis',
    "evidence" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "watch_party_sessions" (
    "id" SERIAL NOT NULL,
    "host_user_id" TEXT NOT NULL,
    "media_id" INTEGER NOT NULL,
    "status" "WatchPartyStatus" NOT NULL DEFAULT 'active',
    "invite_code" TEXT NOT NULL,
    "current_time" INTEGER NOT NULL DEFAULT 0,
    "is_playing" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "ended_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "watch_party_participants" (
    "id" SERIAL NOT NULL,
    "session_id" INTEGER NOT NULL,
    "user_id" TEXT NOT NULL,
    "joined_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "left_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "watch_party_chat_messages" (
    "id" SERIAL NOT NULL,
    "session_id" INTEGER NOT NULL,
    "user_id" TEXT NOT NULL,
    "comment_id" INTEGER NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "watch_party_reactions" (
    "id" SERIAL NOT NULL,
    "session_id" INTEGER NOT NULL,
    "user_id" TEXT NOT NULL,
    "reaction_type" "ReactionType" NOT NULL,
    "video_timestamp" INTEGER NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "watch_party_invites" (
    "id" SERIAL NOT NULL,
    "session_id" INTEGER NOT NULL,
    "user_id" TEXT NOT NULL,
    "invited_by" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'pending',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "accepted_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "tag_generation_jobs" (
    "id" SERIAL NOT NULL,
    "video_id" INTEGER NOT NULL,
    "job_id" INTEGER,
    "status" TEXT NOT NULL DEFAULT 'pending',
    "progress" INTEGER DEFAULT 0,
    "generated_tags" JSONB,
    "raw_response" TEXT,
    "error" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "started_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "creators" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "stage_name" TEXT,
    "performer_gender" TEXT,
    "face_embedding" JSONB,
    "reference_image_path" TEXT,
    "performer_category" TEXT,
    "status" TEXT NOT NULL DEFAULT 'active',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "performer_faces" (
    "id" SERIAL NOT NULL,
    "performer_id" INTEGER NOT NULL,
    "source_path" TEXT NOT NULL,
    "frame_timestamp" REAL,
    "bbox" JSONB NOT NULL,
    "embedding" JSONB NOT NULL,
    "gender" INTEGER NOT NULL,
    "age" INTEGER NOT NULL,
    "det_score" REAL NOT NULL,
    "verified_by_user" BOOLEAN NOT NULL DEFAULT false,
    "confidence" REAL,
    "is_reference_image" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "performer_discrepancies" (
    "id" SERIAL NOT NULL,
    "video_path" TEXT NOT NULL,
    "frame_timestamp" REAL NOT NULL,
    "assigned_performer_id" INTEGER NOT NULL,
    "detected_performer_id" INTEGER,
    "similarity" REAL NOT NULL,
    "face_data" JSONB NOT NULL,
    "resolution_status" TEXT NOT NULL DEFAULT 'pending',
    "resolution_action" TEXT,
    "resolution_notes" TEXT,
    "resolved_by" TEXT,
    "resolved_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "pipelines" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "status" "PipelineStatus" NOT NULL DEFAULT 'draft',
    "config" JSONB,
    "template_id" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "started_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),
    "error" TEXT,

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

-- CreateTable
CREATE TABLE "pipeline_steps" (
    "id" SERIAL NOT NULL,
    "pipeline_id" INTEGER NOT NULL,
    "step_name" TEXT NOT NULL,
    "step_type" TEXT NOT NULL,
    "sequence_order" INTEGER NOT NULL,
    "status" "PipelineStepStatus" NOT NULL DEFAULT 'pending',
    "config" JSONB,
    "inputs" JSONB,
    "outputs" JSONB,
    "depends_on" JSONB,
    "error" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "started_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "pipeline_step_events" (
    "id" SERIAL NOT NULL,
    "step_id" INTEGER NOT NULL,
    "event_type" TEXT NOT NULL,
    "message" TEXT,
    "data" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "resource_snapshots" (
    "id" SERIAL NOT NULL,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "cpu_percent" REAL,
    "memory_mb" INTEGER,
    "gpu_percent" REAL,
    "vram_mb" INTEGER,
    "disk_usage_mb" INTEGER,
    "active_jobs" INTEGER,
    "queued_jobs" INTEGER,
    "metadata" JSONB,

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

-- CreateTable
CREATE TABLE "pipeline_templates" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "category" TEXT,
    "default_config" JSONB,
    "step_definitions" JSONB NOT NULL,
    "is_public" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "subscription_plans" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "price_cad" INTEGER NOT NULL,
    "duration_days" INTEGER NOT NULL,
    "features" JSONB,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "stripe_product_id" TEXT,
    "stripe_price_id" TEXT,
    "stripe_yearly_price_id" TEXT,
    "yearly_price_cad" INTEGER,
    "description" TEXT,
    "tier" INTEGER NOT NULL DEFAULT 0,
    "display_order" INTEGER NOT NULL DEFAULT 0,
    "slug" TEXT,
    "cover_photo" TEXT,
    "cover_video_id" INTEGER,
    "rich_description" TEXT,
    "cta_text" TEXT,
    "cta_subtext" TEXT,
    "highlight_plan" BOOLEAN NOT NULL DEFAULT false,

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

-- CreateTable
CREATE TABLE "user_subscriptions" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "plan_id" INTEGER NOT NULL,
    "status" "SubscriptionStatus" NOT NULL DEFAULT 'active',
    "start_date" TIMESTAMP(3) NOT NULL,
    "end_date" TIMESTAMP(3) NOT NULL,
    "cancelled_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "stripe_subscription_id" TEXT,
    "stripe_customer_id" TEXT,
    "current_period_end" TIMESTAMP(3),
    "cancel_at_period_end" BOOLEAN NOT NULL DEFAULT false,

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

-- CreateTable
CREATE TABLE "invoices" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "subscription_id" INTEGER,
    "amount_cad" INTEGER NOT NULL,
    "status" "InvoiceStatus" NOT NULL DEFAULT 'pending',
    "issued_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "paid_at" TIMESTAMP(3),
    "due_date" TIMESTAMP(3) NOT NULL,
    "description" TEXT,
    "metadata" JSONB,
    "stripe_invoice_id" TEXT,
    "invoice_type" TEXT NOT NULL DEFAULT 'subscription',

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

-- CreateTable
CREATE TABLE "payments" (
    "id" SERIAL NOT NULL,
    "invoice_id" INTEGER NOT NULL,
    "user_id" TEXT NOT NULL,
    "amount_cad" INTEGER NOT NULL,
    "method" "PaymentMethod" NOT NULL,
    "status" "PaymentStatus" NOT NULL DEFAULT 'pending',
    "external_id" TEXT,
    "metadata" JSONB,
    "processed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "stripe_payment_intent_id" TEXT,
    "stripe_checkout_session_id" TEXT,

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

-- CreateTable
CREATE TABLE "payment_audit_log" (
    "id" SERIAL NOT NULL,
    "payment_id" INTEGER NOT NULL,
    "action" TEXT NOT NULL,
    "old_status" TEXT,
    "new_status" TEXT,
    "user_id" TEXT,
    "metadata" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "products" (
    "id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "price_cad" INTEGER NOT NULL,
    "type" "ProductType" NOT NULL,
    "stripe_product_id" TEXT,
    "stripe_price_id" TEXT,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "image_url" TEXT,
    "photo_id" INTEGER,
    "video_id" INTEGER,
    "gallery_photo_ids" JSONB,
    "download_url" TEXT,
    "metadata" JSONB,
    "max_purchases" INTEGER,
    "purchase_count" INTEGER NOT NULL DEFAULT 0,
    "created_by_user_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "orders" (
    "id" TEXT NOT NULL,
    "user_id" TEXT,
    "product_id" TEXT,
    "amount_cad" INTEGER NOT NULL,
    "status" "OrderStatus" NOT NULL DEFAULT 'PENDING',
    "stripe_checkout_session_id" TEXT,
    "stripe_payment_intent_id" TEXT,
    "order_type" TEXT NOT NULL DEFAULT 'product',
    "buyer_email" TEXT NOT NULL,
    "buyer_name" TEXT,
    "donor_message" TEXT,
    "is_anonymous" BOOLEAN NOT NULL DEFAULT false,
    "completed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "donation_page_id" TEXT,

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

-- CreateTable
CREATE TABLE "donation_pages" (
    "id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "status" "DonationPageStatus" NOT NULL DEFAULT 'DRAFT',
    "suggestedAmounts" JSONB NOT NULL DEFAULT '[1000, 2500, 5000, 10000]',
    "minimum_amount" INTEGER NOT NULL DEFAULT 500,
    "thank_you_message" TEXT NOT NULL DEFAULT 'Thank you for your support!',
    "cover_photo" TEXT,
    "cover_video_id" INTEGER,
    "highlight_page" BOOLEAN NOT NULL DEFAULT false,
    "show_donor_count" BOOLEAN NOT NULL DEFAULT true,
    "show_total_raised" BOOLEAN NOT NULL DEFAULT false,
    "goal_amount" INTEGER,
    "created_by_user_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "payment_settings" (
    "id" TEXT NOT NULL,
    "stripe_secret_key" TEXT NOT NULL DEFAULT '',
    "stripe_publishable_key" TEXT NOT NULL DEFAULT '',
    "stripe_webhook_secret" TEXT NOT NULL DEFAULT '',
    "default_currency" TEXT NOT NULL DEFAULT 'cad',
    "enable_donations" BOOLEAN NOT NULL DEFAULT true,
    "donation_suggested_amounts" JSONB NOT NULL DEFAULT '[1000, 2500, 5000, 10000]',
    "donation_minimum" INTEGER NOT NULL DEFAULT 500,
    "donation_page_title" TEXT NOT NULL DEFAULT 'Support Our Work',
    "donation_page_description" TEXT,
    "thank_you_message" TEXT NOT NULL DEFAULT 'Thank you for your support!',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "notifications" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "type" "NotificationType" NOT NULL,
    "title" TEXT NOT NULL,
    "message" TEXT NOT NULL,
    "metadata" JSONB,
    "is_read" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "read_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "notification_preferences" (
    "id" SERIAL NOT NULL,
    "user_id" TEXT NOT NULL,
    "enable_friend_requests" BOOLEAN NOT NULL DEFAULT true,
    "enable_comments" BOOLEAN NOT NULL DEFAULT true,
    "enable_upload_approvals" BOOLEAN NOT NULL DEFAULT true,
    "enable_achievements" BOOLEAN NOT NULL DEFAULT true,
    "enable_system_updates" BOOLEAN NOT NULL DEFAULT true,
    "email_notifications" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "geo_blocking_rules" (
    "id" SERIAL NOT NULL,
    "country_code" TEXT NOT NULL,
    "action" TEXT NOT NULL,
    "reason" TEXT,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "published_inbox_files" (
    "id" SERIAL NOT NULL,
    "original_inbox_path" TEXT NOT NULL,
    "published_to_public_media_id" INTEGER NOT NULL,
    "published_at" TIMESTAMP(3) NOT NULL,
    "moved_to_path" TEXT,
    "metadata_snapshot" JSONB,

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

-- CreateTable
CREATE TABLE "video_ocr_results" (
    "id" SERIAL NOT NULL,
    "video_id" INTEGER NOT NULL,
    "full_text" TEXT NOT NULL,
    "structured_data" JSONB,
    "confidence" REAL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "video_views" (
    "id" SERIAL NOT NULL,
    "video_id" INTEGER NOT NULL,
    "user_id" TEXT,
    "ip_address_hash" VARCHAR(64),
    "user_agent_hash" VARCHAR(64),
    "referer" TEXT,
    "watch_time_seconds" INTEGER NOT NULL DEFAULT 0,
    "completed" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "video_events" (
    "id" SERIAL NOT NULL,
    "video_id" INTEGER NOT NULL,
    "view_id" INTEGER,
    "event_type" VARCHAR(50) NOT NULL,
    "timestamp" DECIMAL(10,2) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "video_schedule_history" (
    "id" SERIAL NOT NULL,
    "video_id" INTEGER NOT NULL,
    "action" VARCHAR(20) NOT NULL,
    "scheduled_for" TIMESTAMP(3) NOT NULL,
    "executed_at" TIMESTAMP(3),
    "status" VARCHAR(20) NOT NULL,
    "error" TEXT,
    "scheduled_by_user_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "docs_page_views" (
    "id" TEXT NOT NULL,
    "path" TEXT NOT NULL,
    "referrer" TEXT,
    "sessionHash" TEXT,
    "userAgent" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "photos" (
    "id" SERIAL NOT NULL,
    "path" TEXT NOT NULL,
    "filename" TEXT NOT NULL,
    "original_filename" TEXT,
    "title" TEXT,
    "description" TEXT,
    "producer" TEXT,
    "creator" TEXT,
    "tags" JSONB,
    "width" INTEGER,
    "height" INTEGER,
    "orientation" TEXT,
    "file_size" BIGINT,
    "format" TEXT,
    "color_space" TEXT,
    "has_alpha" BOOLEAN DEFAULT false,
    "dpi" INTEGER,
    "camera_make" TEXT,
    "camera_model" TEXT,
    "focal_length" TEXT,
    "aperture" TEXT,
    "shutter_speed" TEXT,
    "iso" INTEGER,
    "taken_at" TIMESTAMP(3),
    "gps_latitude" REAL,
    "gps_longitude" REAL,
    "thumbnail_path" TEXT,
    "medium_path" TEXT,
    "large_path" TEXT,
    "webp_path" TEXT,
    "is_published" BOOLEAN NOT NULL DEFAULT false,
    "published_at" TIMESTAMP(3),
    "category" TEXT,
    "access_level" TEXT NOT NULL DEFAULT 'free',
    "position" INTEGER DEFAULT 0,
    "is_locked" BOOLEAN NOT NULL DEFAULT false,
    "scheduled_publish_at" TIMESTAMP(3),
    "scheduled_unpublish_at" TIMESTAMP(3),
    "view_count" INTEGER NOT NULL DEFAULT 0,
    "upvote_count" INTEGER NOT NULL DEFAULT 0,
    "comment_count" INTEGER NOT NULL DEFAULT 0,
    "album_id" INTEGER,
    "album_position" INTEGER DEFAULT 0,
    "uploader_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "photo_albums" (
    "id" SERIAL NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "cover_photo_id" INTEGER,
    "is_published" BOOLEAN NOT NULL DEFAULT false,
    "published_at" TIMESTAMP(3),
    "category" TEXT,
    "access_level" TEXT NOT NULL DEFAULT 'free',
    "position" INTEGER DEFAULT 0,
    "is_locked" BOOLEAN NOT NULL DEFAULT false,
    "view_count" INTEGER NOT NULL DEFAULT 0,
    "upvote_count" INTEGER NOT NULL DEFAULT 0,
    "photo_count" INTEGER NOT NULL DEFAULT 0,
    "scheduled_publish_at" TIMESTAMP(3),
    "scheduled_unpublish_at" TIMESTAMP(3),
    "creator_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "photo_upvotes" (
    "id" SERIAL NOT NULL,
    "photo_id" INTEGER NOT NULL,
    "session_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "photo_comments" (
    "id" SERIAL NOT NULL,
    "photo_id" INTEGER NOT NULL,
    "session_id" TEXT NOT NULL,
    "user_id" TEXT,
    "content" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "safety_status" TEXT NOT NULL DEFAULT 'approved',
    "is_hidden" BOOLEAN NOT NULL DEFAULT false,

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

-- CreateTable
CREATE TABLE "photo_views" (
    "id" SERIAL NOT NULL,
    "photo_id" INTEGER NOT NULL,
    "session_id" TEXT,
    "user_id" TEXT,
    "ip_address_hash" TEXT,
    "viewed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "photo_reactions" (
    "id" SERIAL NOT NULL,
    "photo_id" INTEGER NOT NULL,
    "session_id" TEXT NOT NULL,
    "reaction_type" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "docs_comments" (
    "id" TEXT NOT NULL,
    "pagePath" TEXT NOT NULL,
    "giteaIssueNumber" INTEGER NOT NULL,
    "giteaCommentId" BIGINT NOT NULL,
    "authorName" TEXT NOT NULL,
    "authorEmail" TEXT,
    "status" "DocsCommentStatus" NOT NULL DEFAULT 'PENDING',
    "reviewedAt" TIMESTAMP(3),
    "reviewedBy" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "sms_contact_lists" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "originalFilename" TEXT,
    "totalContacts" INTEGER NOT NULL DEFAULT 0,
    "status" "SmsContactListStatus" NOT NULL DEFAULT 'ACTIVE',
    "createdByUserId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "sms_contact_list_entries" (
    "id" TEXT NOT NULL,
    "listId" TEXT NOT NULL,
    "phone" TEXT NOT NULL,
    "name" TEXT,
    "email" TEXT,
    "customFields" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "sms_campaigns" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "messageTemplate" TEXT NOT NULL,
    "status" "SmsCampaignStatus" NOT NULL DEFAULT 'DRAFT',
    "totalRecipients" INTEGER NOT NULL DEFAULT 0,
    "totalSent" INTEGER NOT NULL DEFAULT 0,
    "totalFailed" INTEGER NOT NULL DEFAULT 0,
    "totalResponded" INTEGER NOT NULL DEFAULT 0,
    "delayBetweenMs" INTEGER NOT NULL DEFAULT 3000,
    "startedAt" TIMESTAMP(3),
    "completedAt" TIMESTAMP(3),
    "contactListId" TEXT NOT NULL,
    "advocacyCampaignId" TEXT,
    "createdByUserId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "sms_campaign_recipients" (
    "id" TEXT NOT NULL,
    "campaignId" TEXT NOT NULL,
    "phone" TEXT NOT NULL,
    "name" TEXT,
    "status" "SmsMessageStatus" NOT NULL DEFAULT 'PENDING',
    "sentAt" TIMESTAMP(3),
    "errorMessage" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "sms_messages" (
    "id" TEXT NOT NULL,
    "phone" TEXT NOT NULL,
    "message" TEXT NOT NULL,
    "direction" "SmsMessageDirection" NOT NULL,
    "status" "SmsMessageStatus" NOT NULL DEFAULT 'PENDING',
    "connectionType" TEXT,
    "campaignId" TEXT,
    "conversationId" TEXT,
    "responseType" "SmsResponseType",
    "isRead" BOOLEAN NOT NULL DEFAULT false,
    "sentAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "sms_conversations" (
    "id" TEXT NOT NULL,
    "phone" TEXT NOT NULL,
    "contactName" TEXT,
    "campaignId" TEXT,
    "status" "SmsConversationStatus" NOT NULL DEFAULT 'ACTIVE',
    "totalMessages" INTEGER NOT NULL DEFAULT 0,
    "totalResponses" INTEGER NOT NULL DEFAULT 0,
    "unreadCount" INTEGER NOT NULL DEFAULT 0,
    "lastMessageAt" TIMESTAMP(3),
    "lastResponseAt" TIMESTAMP(3),
    "notes" TEXT,
    "tags" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "sms_message_templates" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "template" TEXT NOT NULL,
    "description" TEXT,
    "category" TEXT,
    "isFavorite" BOOLEAN NOT NULL DEFAULT false,
    "usageCount" INTEGER NOT NULL DEFAULT 0,
    "createdByUserId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "sms_device_status" (
    "id" TEXT NOT NULL,
    "isConnected" BOOLEAN NOT NULL DEFAULT false,
    "connectionType" TEXT,
    "batteryLevel" INTEGER,
    "batteryStatus" TEXT,
    "totalSent" INTEGER NOT NULL DEFAULT 0,
    "lastCheckedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "contacts" (
    "id" TEXT NOT NULL,
    "displayName" TEXT NOT NULL,
    "firstName" TEXT,
    "lastName" TEXT,
    "email" TEXT,
    "phone" TEXT,
    "tags" JSONB NOT NULL DEFAULT '[]',
    "notes" TEXT,
    "supportLevel" "SupportLevel",
    "signRequested" BOOLEAN NOT NULL DEFAULT false,
    "emailOptOut" BOOLEAN NOT NULL DEFAULT false,
    "smsOptOut" BOOLEAN NOT NULL DEFAULT false,
    "doNotContact" BOOLEAN NOT NULL DEFAULT false,
    "profileToken" TEXT,
    "profileTokenExpiresAt" TIMESTAMP(3),
    "profilePasswordHash" TEXT,
    "coverPhotoPath" TEXT,
    "lastSelfEditAt" TIMESTAMP(3),
    "primarySource" "ContactSource" NOT NULL DEFAULT 'MANUAL',
    "userId" TEXT,
    "mergedIntoId" TEXT,
    "createdByUserId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "crm_tags" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "description" TEXT,
    "color" TEXT,
    "listmonkListId" INTEGER,
    "contactCount" INTEGER NOT NULL DEFAULT 0,
    "createdByUserId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "contact_addresses" (
    "id" TEXT NOT NULL,
    "contactId" TEXT NOT NULL,
    "addressId" TEXT NOT NULL,
    "isPrimary" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "contact_emails" (
    "id" TEXT NOT NULL,
    "contactId" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "label" TEXT,
    "isPrimary" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "contact_phones" (
    "id" TEXT NOT NULL,
    "contactId" TEXT NOT NULL,
    "phone" TEXT NOT NULL,
    "label" TEXT,
    "isPrimary" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "contact_connections" (
    "id" TEXT NOT NULL,
    "fromContactId" TEXT NOT NULL,
    "toContactId" TEXT NOT NULL,
    "type" "ConnectionType" NOT NULL,
    "label" TEXT,
    "notes" TEXT,
    "isBidirectional" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "contact_activities" (
    "id" TEXT NOT NULL,
    "contactId" TEXT NOT NULL,
    "type" "ContactActivityType" NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "metadata" JSONB,
    "occurredAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "addresses_addrGuid_key" ON "addresses"("addrGuid");

-- CreateIndex
CREATE INDEX "addresses_locationId_idx" ON "addresses"("locationId");

-- CreateIndex
CREATE INDEX "addresses_locationId_id_idx" ON "addresses"("locationId", "id");

-- CreateIndex
CREATE INDEX "addresses_locationId_unitNumber_idx" ON "addresses"("locationId", "unitNumber");

-- CreateIndex
CREATE INDEX "location_history_locationId_idx" ON "location_history"("locationId");

-- CreateIndex
CREATE INDEX "location_history_userId_idx" ON "location_history"("userId");

-- CreateIndex
CREATE INDEX "location_history_createdAt_idx" ON "location_history"("createdAt");

-- CreateIndex
CREATE INDEX "shift_series_cutId_idx" ON "shift_series"("cutId");

-- CreateIndex
CREATE UNIQUE INDEX "email_templates_key_key" ON "email_templates"("key");

-- CreateIndex
CREATE INDEX "email_templates_category_idx" ON "email_templates"("category");

-- CreateIndex
CREATE INDEX "email_templates_isActive_idx" ON "email_templates"("isActive");

-- CreateIndex
CREATE INDEX "email_template_variables_templateId_idx" ON "email_template_variables"("templateId");

-- CreateIndex
CREATE INDEX "email_template_variables_type_idx" ON "email_template_variables"("type");

-- CreateIndex
CREATE UNIQUE INDEX "email_template_variables_templateId_key_key" ON "email_template_variables"("templateId", "key");

-- CreateIndex
CREATE INDEX "email_template_versions_templateId_createdAt_idx" ON "email_template_versions"("templateId", "createdAt" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "email_template_versions_templateId_versionNumber_key" ON "email_template_versions"("templateId", "versionNumber");

-- CreateIndex
CREATE INDEX "email_template_test_logs_templateId_sentAt_idx" ON "email_template_test_logs"("templateId", "sentAt" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "videos_path_key" ON "videos"("path");

-- CreateIndex
CREATE INDEX "idx_orientation" ON "videos"("orientation");

-- CreateIndex
CREATE INDEX "idx_producer" ON "videos"("producer");

-- CreateIndex
CREATE INDEX "idx_is_valid" ON "videos"("is_valid");

-- CreateIndex
CREATE INDEX "idx_directory_type" ON "videos"("directory_type");

-- CreateIndex
CREATE INDEX "idx_videos_fingerprint" ON "videos"("duration_seconds", "file_size", "width", "height");

-- CreateIndex
CREATE INDEX "idx_videos_directory_valid_orientation" ON "videos"("directory_type", "is_valid", "orientation");

-- CreateIndex
CREATE INDEX "idx_videos_published_locked" ON "videos"("is_published", "is_locked");

-- CreateIndex
CREATE INDEX "idx_videos_category_published" ON "videos"("category", "is_published");

-- CreateIndex
CREATE INDEX "idx_videos_short_published" ON "videos"("is_short", "is_published", "is_locked");

-- CreateIndex
CREATE INDEX "idx_videos_uploader" ON "videos"("uploader_id");

-- CreateIndex
CREATE INDEX "idx_jobs_queue" ON "jobs"("status", "priority", "created_at");

-- CreateIndex
CREATE INDEX "idx_jobs_resource" ON "jobs"("resource_category", "status");

-- CreateIndex
CREATE INDEX "idx_jobs_pipeline" ON "jobs"("pipeline_id");

-- CreateIndex
CREATE INDEX "idx_sessions_user_id" ON "sessions"("user_id");

-- CreateIndex
CREATE INDEX "idx_sessions_country" ON "sessions"("country");

-- CreateIndex
CREATE INDEX "idx_upvotes_unique" ON "upvotes"("media_id", "session_id");

-- CreateIndex
CREATE INDEX "idx_upvotes_media" ON "upvotes"("media_id");

-- CreateIndex
CREATE INDEX "idx_comments_media" ON "comments"("media_id");

-- CreateIndex
CREATE INDEX "idx_comments_session" ON "comments"("session_id");

-- CreateIndex
CREATE INDEX "idx_comments_user" ON "comments"("user_id");

-- CreateIndex
CREATE INDEX "idx_comments_safety_status" ON "comments"("safety_status");

-- CreateIndex
CREATE INDEX "idx_comments_is_hidden" ON "comments"("is_hidden");

-- CreateIndex
CREATE INDEX "idx_views_unique" ON "views"("media_id", "session_id");

-- CreateIndex
CREATE INDEX "idx_views_media" ON "views"("media_id");

-- CreateIndex
CREATE UNIQUE INDEX "auth_tokens_token_key" ON "auth_tokens"("token");

-- CreateIndex
CREATE INDEX "idx_auth_tokens_token" ON "auth_tokens"("token");

-- CreateIndex
CREATE INDEX "idx_auth_tokens_user" ON "auth_tokens"("user_id");

-- CreateIndex
CREATE INDEX "idx_session_bans_session" ON "session_bans"("session_id");

-- CreateIndex
CREATE UNIQUE INDEX "comment_moderation_comment_id_key" ON "comment_moderation"("comment_id");

-- CreateIndex
CREATE INDEX "idx_comment_moderation_comment" ON "comment_moderation"("comment_id");

-- CreateIndex
CREATE INDEX "idx_comment_moderation_status" ON "comment_moderation"("status");

-- CreateIndex
CREATE UNIQUE INDEX "email_verification_tokens_token_key" ON "email_verification_tokens"("token");

-- CreateIndex
CREATE INDEX "idx_email_verification_tokens_token" ON "email_verification_tokens"("token");

-- CreateIndex
CREATE INDEX "idx_email_verification_tokens_user" ON "email_verification_tokens"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "password_reset_tokens_token_key" ON "password_reset_tokens"("token");

-- CreateIndex
CREATE INDEX "idx_password_reset_tokens_token" ON "password_reset_tokens"("token");

-- CreateIndex
CREATE INDEX "idx_password_reset_tokens_user" ON "password_reset_tokens"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "email_change_tokens_token_key" ON "email_change_tokens"("token");

-- CreateIndex
CREATE INDEX "idx_email_change_tokens_token" ON "email_change_tokens"("token");

-- CreateIndex
CREATE INDEX "idx_email_change_tokens_user" ON "email_change_tokens"("user_id");

-- CreateIndex
CREATE INDEX "idx_user_achievements_user" ON "user_achievements"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "idx_user_achievements_unique" ON "user_achievements"("user_id", "achievement_id");

-- CreateIndex
CREATE UNIQUE INDEX "user_stats_user_id_key" ON "user_stats"("user_id");

-- CreateIndex
CREATE INDEX "idx_user_finishes_user" ON "user_finishes"("user_id");

-- CreateIndex
CREATE INDEX "idx_user_finishes_date" ON "user_finishes"("created_at");

-- CreateIndex
CREATE INDEX "idx_video_reactions_user_media_type" ON "video_reactions"("user_id", "media_id", "reaction_type");

-- CreateIndex
CREATE INDEX "idx_video_reactions_media_timestamp" ON "video_reactions"("media_id", "video_timestamp");

-- CreateIndex
CREATE INDEX "idx_video_reactions_media" ON "video_reactions"("media_id");

-- CreateIndex
CREATE INDEX "idx_video_reactions_created" ON "video_reactions"("created_at");

-- CreateIndex
CREATE INDEX "idx_highlight_cooldowns_user" ON "highlight_cooldowns"("user_id");

-- CreateIndex
CREATE INDEX "idx_user_daily_activity_unique" ON "user_daily_activity"("user_id", "activity_date");

-- CreateIndex
CREATE INDEX "idx_chat_thread_read_unique" ON "chat_thread_read_status"("user_id", "media_id");

-- CreateIndex
CREATE INDEX "idx_chat_thread_read_user" ON "chat_thread_read_status"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "rate_limits_key_key" ON "rate_limits"("key");

-- CreateIndex
CREATE INDEX "idx_rate_limits_reset_at" ON "rate_limits"("reset_at");

-- CreateIndex
CREATE UNIQUE INDEX "idx_rate_limits_key" ON "rate_limits"("key");

-- CreateIndex
CREATE INDEX "idx_moderation_word_lists_level" ON "moderation_word_lists"("level");

-- CreateIndex
CREATE INDEX "idx_moderation_word_lists_word" ON "moderation_word_lists"("word");

-- CreateIndex
CREATE INDEX "idx_content_reports_media" ON "content_reports"("media_id");

-- CreateIndex
CREATE INDEX "idx_content_reports_status" ON "content_reports"("status");

-- CreateIndex
CREATE INDEX "idx_content_reports_session" ON "content_reports"("session_id");

-- CreateIndex
CREATE INDEX "idx_content_reports_created" ON "content_reports"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "playlists_share_token_key" ON "playlists"("share_token");

-- CreateIndex
CREATE INDEX "idx_playlists_user" ON "playlists"("user_id");

-- CreateIndex
CREATE INDEX "idx_playlists_public" ON "playlists"("is_public");

-- CreateIndex
CREATE INDEX "idx_playlists_share_token" ON "playlists"("share_token");

-- CreateIndex
CREATE UNIQUE INDEX "idx_playlists_user_name" ON "playlists"("user_id", "name");

-- CreateIndex
CREATE INDEX "idx_playlist_videos_playlist" ON "playlist_videos"("playlist_id");

-- CreateIndex
CREATE INDEX "idx_playlist_videos_media" ON "playlist_videos"("media_id");

-- CreateIndex
CREATE INDEX "idx_playlist_videos_unique" ON "playlist_videos"("playlist_id", "media_id");

-- CreateIndex
CREATE UNIQUE INDEX "featured_playlists_playlist_id_key" ON "featured_playlists"("playlist_id");

-- CreateIndex
CREATE INDEX "idx_featured_playlists_position" ON "featured_playlists"("position");

-- CreateIndex
CREATE INDEX "idx_playlist_views_playlist" ON "playlist_views"("playlist_id");

-- CreateIndex
CREATE INDEX "idx_playlist_views_unique" ON "playlist_views"("playlist_id", "session_id");

-- CreateIndex
CREATE UNIQUE INDEX "ads_product_id_key" ON "ads"("product_id");

-- CreateIndex
CREATE INDEX "idx_ads_type" ON "ads"("type");

-- CreateIndex
CREATE INDEX "idx_ads_is_active" ON "ads"("is_active");

-- CreateIndex
CREATE INDEX "idx_ads_visibility" ON "ads"("visibility");

-- CreateIndex
CREATE INDEX "idx_ad_impressions_ad" ON "ad_impressions"("ad_id");

-- CreateIndex
CREATE INDEX "idx_ad_impressions_session" ON "ad_impressions"("session_id");

-- CreateIndex
CREATE INDEX "idx_ad_impressions_date" ON "ad_impressions"("created_at");

-- CreateIndex
CREATE INDEX "idx_ad_clicks_ad" ON "ad_clicks"("ad_id");

-- CreateIndex
CREATE INDEX "idx_ad_clicks_session" ON "ad_clicks"("session_id");

-- CreateIndex
CREATE INDEX "idx_ad_clicks_date" ON "ad_clicks"("created_at");

-- CreateIndex
CREATE INDEX "idx_friendships_user_friend" ON "friendships"("user_id", "friend_id");

-- CreateIndex
CREATE INDEX "idx_friendships_user" ON "friendships"("user_id");

-- CreateIndex
CREATE INDEX "idx_friendships_friend" ON "friendships"("friend_id");

-- CreateIndex
CREATE INDEX "idx_friendships_status" ON "friendships"("status");

-- CreateIndex
CREATE INDEX "idx_user_blocks_unique" ON "user_blocks"("user_id", "blocked_user_id");

-- CreateIndex
CREATE INDEX "idx_user_blocks_user" ON "user_blocks"("user_id");

-- CreateIndex
CREATE INDEX "idx_pokes_to_user" ON "pokes"("to_user_id");

-- CreateIndex
CREATE INDEX "idx_pokes_from_user" ON "pokes"("from_user_id");

-- CreateIndex
CREATE INDEX "idx_video_recommendations_to_user" ON "video_recommendations"("to_user_id");

-- CreateIndex
CREATE INDEX "idx_video_recommendations_from_user" ON "video_recommendations"("from_user_id");

-- CreateIndex
CREATE INDEX "idx_video_recommendations_media" ON "video_recommendations"("media_id");

-- CreateIndex
CREATE UNIQUE INDEX "user_presence_user_id_key" ON "user_presence"("user_id");

-- CreateIndex
CREATE INDEX "idx_user_presence_online" ON "user_presence"("is_online");

-- CreateIndex
CREATE INDEX "idx_user_presence_user" ON "user_presence"("user_id");

-- CreateIndex
CREATE INDEX "idx_user_gallery_user" ON "user_gallery_images"("user_id");

-- CreateIndex
CREATE INDEX "idx_user_gallery_position" ON "user_gallery_images"("user_id", "position");

-- CreateIndex
CREATE INDEX "idx_user_social_links_user" ON "user_social_links"("user_id");

-- CreateIndex
CREATE INDEX "idx_user_social_links_position" ON "user_social_links"("user_id", "position");

-- CreateIndex
CREATE UNIQUE INDEX "privacy_settings_user_id_key" ON "privacy_settings"("user_id");

-- CreateIndex
CREATE INDEX "idx_privacy_settings_user" ON "privacy_settings"("user_id");

-- CreateIndex
CREATE INDEX "idx_close_friends_user" ON "close_friends"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "idx_close_friends_unique" ON "close_friends"("user_id", "close_friend_id");

-- CreateIndex
CREATE INDEX "idx_user_uploads_user" ON "user_uploads"("user_id");

-- CreateIndex
CREATE INDEX "idx_user_uploads_status" ON "user_uploads"("status");

-- CreateIndex
CREATE INDEX "idx_user_uploads_created" ON "user_uploads"("created_at");

-- CreateIndex
CREATE INDEX "idx_user_uploads_invite" ON "user_uploads"("upload_invite_id");

-- CreateIndex
CREATE UNIQUE INDEX "upload_invites_code_key" ON "upload_invites"("code");

-- CreateIndex
CREATE INDEX "idx_upload_invites_code" ON "upload_invites"("code");

-- CreateIndex
CREATE INDEX "idx_upload_invites_status" ON "upload_invites"("status");

-- CreateIndex
CREATE INDEX "idx_upload_invites_created_by" ON "upload_invites"("created_by");

-- CreateIndex
CREATE UNIQUE INDEX "tag_categories_name_key" ON "tag_categories"("name");

-- CreateIndex
CREATE INDEX "idx_tag_categories_display_order" ON "tag_categories"("display_order");

-- CreateIndex
CREATE INDEX "idx_tags_category" ON "tags"("category_id");

-- CreateIndex
CREATE INDEX "idx_tags_display_order" ON "tags"("display_order");

-- CreateIndex
CREATE INDEX "idx_tags_unique_name" ON "tags"("category_id", "name");

-- CreateIndex
CREATE INDEX "idx_public_media_tags_media" ON "public_media_tags"("media_id");

-- CreateIndex
CREATE INDEX "idx_public_media_tags_tag" ON "public_media_tags"("tag_id");

-- CreateIndex
CREATE INDEX "idx_public_media_tags_unique" ON "public_media_tags"("media_id", "tag_id");

-- CreateIndex
CREATE INDEX "idx_user_upload_suggested_tags_upload" ON "user_upload_suggested_tags"("upload_id");

-- CreateIndex
CREATE INDEX "idx_user_upload_suggested_tags_tag" ON "user_upload_suggested_tags"("tag_id");

-- CreateIndex
CREATE INDEX "idx_user_upload_suggested_tags_unique" ON "user_upload_suggested_tags"("upload_id", "tag_id");

-- CreateIndex
CREATE INDEX "idx_user_tag_preferences_user" ON "user_tag_preferences"("user_id");

-- CreateIndex
CREATE INDEX "idx_user_tag_preferences_tag" ON "user_tag_preferences"("tag_id");

-- CreateIndex
CREATE INDEX "idx_user_tag_preferences_unique" ON "user_tag_preferences"("user_id", "tag_id");

-- CreateIndex
CREATE INDEX "idx_public_media_performers_media" ON "public_media_performers"("media_id");

-- CreateIndex
CREATE INDEX "idx_public_media_performers_performer" ON "public_media_performers"("performer_id");

-- CreateIndex
CREATE UNIQUE INDEX "idx_public_media_performers_unique" ON "public_media_performers"("media_id", "performer_id");

-- CreateIndex
CREATE UNIQUE INDEX "video_digests_job_id_key" ON "video_digests"("job_id");

-- CreateIndex
CREATE INDEX "idx_video_digests_video" ON "video_digests"("video_id");

-- CreateIndex
CREATE INDEX "idx_video_digests_status" ON "video_digests"("status");

-- CreateIndex
CREATE INDEX "idx_video_digests_created" ON "video_digests"("created_at");

-- CreateIndex
CREATE INDEX "idx_digest_video_tags_digest" ON "digest_video_tags"("digest_id");

-- CreateIndex
CREATE INDEX "idx_digest_video_tags_video" ON "digest_video_tags"("video_id");

-- CreateIndex
CREATE INDEX "idx_digest_video_tags_category" ON "digest_video_tags"("category");

-- CreateIndex
CREATE INDEX "idx_digest_video_tags_value" ON "digest_video_tags"("value");

-- CreateIndex
CREATE INDEX "idx_digest_video_tags_cat_val" ON "digest_video_tags"("category", "value");

-- CreateIndex
CREATE INDEX "idx_digest_selected_clips_digest" ON "digest_selected_clips"("digest_id");

-- CreateIndex
CREATE INDEX "idx_digest_selected_clips_video" ON "digest_selected_clips"("video_id");

-- CreateIndex
CREATE INDEX "idx_digest_selected_clips_type" ON "digest_selected_clips"("clip_type");

-- CreateIndex
CREATE INDEX "idx_digest_selected_clips_source" ON "digest_selected_clips"("source");

-- CreateIndex
CREATE INDEX "idx_digest_selected_clips_sequence" ON "digest_selected_clips"("sequence_order");

-- CreateIndex
CREATE INDEX "idx_digest_selected_clips_is_hook" ON "digest_selected_clips"("is_hook");

-- CreateIndex
CREATE INDEX "idx_digest_selected_clips_hook_source" ON "digest_selected_clips"("hook_source_clip_id");

-- CreateIndex
CREATE INDEX "idx_digest_generated_clips_selected" ON "digest_generated_clips"("selected_clip_id");

-- CreateIndex
CREATE INDEX "idx_digest_generated_clips_digest" ON "digest_generated_clips"("digest_id");

-- CreateIndex
CREATE INDEX "idx_digest_generated_clips_status" ON "digest_generated_clips"("status");

-- CreateIndex
CREATE INDEX "idx_digest_generated_clips_folder" ON "digest_generated_clips"("folder_id");

-- CreateIndex
CREATE INDEX "idx_digest_compilations_digest" ON "digest_compilations"("digest_id");

-- CreateIndex
CREATE INDEX "idx_digest_compilations_video" ON "digest_compilations"("video_id");

-- CreateIndex
CREATE INDEX "idx_digest_compilations_status" ON "digest_compilations"("status");

-- CreateIndex
CREATE INDEX "idx_digest_compilations_folder" ON "digest_compilations"("folder_id");

-- CreateIndex
CREATE INDEX "idx_digest_output_folders_digest" ON "digest_output_folders"("digest_id");

-- CreateIndex
CREATE INDEX "idx_digest_output_folders_video" ON "digest_output_folders"("video_id");

-- CreateIndex
CREATE INDEX "idx_digest_output_folders_path" ON "digest_output_folders"("folder_path");

-- CreateIndex
CREATE INDEX "idx_digest_generated_scenes_digest" ON "digest_generated_scenes"("digest_id");

-- CreateIndex
CREATE INDEX "idx_digest_generated_scenes_video" ON "digest_generated_scenes"("video_id");

-- CreateIndex
CREATE INDEX "idx_digest_generated_scenes_folder" ON "digest_generated_scenes"("folder_id");

-- CreateIndex
CREATE INDEX "idx_digest_generated_scenes_path" ON "digest_generated_scenes"("scene_path");

-- CreateIndex
CREATE INDEX "idx_video_scene_cuts_video" ON "video_scene_cuts"("video_id");

-- CreateIndex
CREATE INDEX "idx_video_scene_cuts_detector" ON "video_scene_cuts"("detector");

-- CreateIndex
CREATE INDEX "idx_video_tag_timeline_video" ON "video_tag_timeline"("video_id");

-- CreateIndex
CREATE INDEX "idx_video_tag_timeline_category" ON "video_tag_timeline"("category");

-- CreateIndex
CREATE INDEX "idx_video_tag_timeline_value" ON "video_tag_timeline"("value");

-- CreateIndex
CREATE INDEX "idx_video_tag_timeline_cat_val" ON "video_tag_timeline"("category", "value");

-- CreateIndex
CREATE INDEX "idx_video_tag_timeline_time" ON "video_tag_timeline"("video_id", "start_time");

-- CreateIndex
CREATE INDEX "idx_video_segments_video" ON "video_segments"("video_id");

-- CreateIndex
CREATE INDEX "idx_video_segments_type" ON "video_segments"("segment_type");

-- CreateIndex
CREATE INDEX "idx_video_segments_vocal_category" ON "video_segments"("vocal_category");

-- CreateIndex
CREATE INDEX "idx_video_segments_time" ON "video_segments"("video_id", "start_time");

-- CreateIndex
CREATE INDEX "idx_video_tags_video" ON "video_tags"("video_id");

-- CreateIndex
CREATE INDEX "idx_video_tags_category" ON "video_tags"("category");

-- CreateIndex
CREATE INDEX "idx_video_tags_value" ON "video_tags"("value");

-- CreateIndex
CREATE INDEX "idx_video_tags_cat_val" ON "video_tags"("category", "value");

-- CreateIndex
CREATE INDEX "idx_digest_suggested_tags_digest" ON "digest_suggested_tags"("digest_id");

-- CreateIndex
CREATE INDEX "idx_digest_suggested_tags_status" ON "digest_suggested_tags"("status");

-- CreateIndex
CREATE INDEX "idx_digest_suggested_tags_category" ON "digest_suggested_tags"("category");

-- CreateIndex
CREATE INDEX "idx_digest_suggested_tags_mapped" ON "digest_suggested_tags"("mapped_tag_id");

-- CreateIndex
CREATE INDEX "idx_digest_clip_tags_digest" ON "digest_clip_tags"("digest_id");

-- CreateIndex
CREATE INDEX "idx_digest_clip_tags_clip" ON "digest_clip_tags"("clip_id");

-- CreateIndex
CREATE INDEX "idx_digest_clip_tags_video" ON "digest_clip_tags"("video_id");

-- CreateIndex
CREATE INDEX "idx_digest_clip_tags_category" ON "digest_clip_tags"("category");

-- CreateIndex
CREATE INDEX "idx_digest_clip_tags_value" ON "digest_clip_tags"("value");

-- CreateIndex
CREATE UNIQUE INDEX "watch_party_sessions_invite_code_key" ON "watch_party_sessions"("invite_code");

-- CreateIndex
CREATE INDEX "idx_watch_party_sessions_host" ON "watch_party_sessions"("host_user_id");

-- CreateIndex
CREATE INDEX "idx_watch_party_sessions_media" ON "watch_party_sessions"("media_id");

-- CreateIndex
CREATE INDEX "idx_watch_party_sessions_status" ON "watch_party_sessions"("status");

-- CreateIndex
CREATE INDEX "idx_watch_party_sessions_invite" ON "watch_party_sessions"("invite_code");

-- CreateIndex
CREATE INDEX "idx_watch_party_participants_session" ON "watch_party_participants"("session_id");

-- CreateIndex
CREATE INDEX "idx_watch_party_participants_user" ON "watch_party_participants"("user_id");

-- CreateIndex
CREATE INDEX "idx_watch_party_chat_session" ON "watch_party_chat_messages"("session_id");

-- CreateIndex
CREATE INDEX "idx_watch_party_chat_user" ON "watch_party_chat_messages"("user_id");

-- CreateIndex
CREATE INDEX "idx_watch_party_reactions_session" ON "watch_party_reactions"("session_id");

-- CreateIndex
CREATE INDEX "idx_watch_party_reactions_user" ON "watch_party_reactions"("user_id");

-- CreateIndex
CREATE INDEX "idx_watch_party_invites_session" ON "watch_party_invites"("session_id");

-- CreateIndex
CREATE INDEX "idx_watch_party_invites_user" ON "watch_party_invites"("user_id");

-- CreateIndex
CREATE INDEX "idx_tag_generation_jobs_video" ON "tag_generation_jobs"("video_id");

-- CreateIndex
CREATE INDEX "idx_tag_generation_jobs_status" ON "tag_generation_jobs"("status");

-- CreateIndex
CREATE UNIQUE INDEX "creators_name_key" ON "creators"("name");

-- CreateIndex
CREATE INDEX "idx_creators_name" ON "creators"("name");

-- CreateIndex
CREATE INDEX "idx_creators_category" ON "creators"("performer_category");

-- CreateIndex
CREATE INDEX "idx_performer_faces_performer" ON "performer_faces"("performer_id");

-- CreateIndex
CREATE INDEX "idx_performer_faces_source" ON "performer_faces"("source_path");

-- CreateIndex
CREATE INDEX "idx_performer_faces_reference" ON "performer_faces"("is_reference_image");

-- CreateIndex
CREATE INDEX "idx_performer_discrepancies_assigned" ON "performer_discrepancies"("assigned_performer_id");

-- CreateIndex
CREATE INDEX "idx_performer_discrepancies_detected" ON "performer_discrepancies"("detected_performer_id");

-- CreateIndex
CREATE INDEX "idx_performer_discrepancies_status" ON "performer_discrepancies"("resolution_status");

-- CreateIndex
CREATE INDEX "idx_pipelines_status" ON "pipelines"("status");

-- CreateIndex
CREATE INDEX "idx_pipelines_template" ON "pipelines"("template_id");

-- CreateIndex
CREATE INDEX "idx_pipeline_steps_pipeline" ON "pipeline_steps"("pipeline_id");

-- CreateIndex
CREATE INDEX "idx_pipeline_steps_status" ON "pipeline_steps"("status");

-- CreateIndex
CREATE INDEX "idx_pipeline_steps_sequence" ON "pipeline_steps"("sequence_order");

-- CreateIndex
CREATE INDEX "idx_pipeline_step_events_step" ON "pipeline_step_events"("step_id");

-- CreateIndex
CREATE INDEX "idx_pipeline_step_events_type" ON "pipeline_step_events"("event_type");

-- CreateIndex
CREATE INDEX "idx_resource_snapshots_timestamp" ON "resource_snapshots"("timestamp");

-- CreateIndex
CREATE INDEX "idx_pipeline_templates_category" ON "pipeline_templates"("category");

-- CreateIndex
CREATE INDEX "idx_pipeline_templates_public" ON "pipeline_templates"("is_public");

-- CreateIndex
CREATE UNIQUE INDEX "subscription_plans_slug_key" ON "subscription_plans"("slug");

-- CreateIndex
CREATE UNIQUE INDEX "user_subscriptions_stripe_subscription_id_key" ON "user_subscriptions"("stripe_subscription_id");

-- CreateIndex
CREATE INDEX "idx_user_subscriptions_user" ON "user_subscriptions"("user_id");

-- CreateIndex
CREATE INDEX "idx_user_subscriptions_plan" ON "user_subscriptions"("plan_id");

-- CreateIndex
CREATE INDEX "idx_user_subscriptions_status" ON "user_subscriptions"("status");

-- CreateIndex
CREATE UNIQUE INDEX "invoices_stripe_invoice_id_key" ON "invoices"("stripe_invoice_id");

-- CreateIndex
CREATE INDEX "idx_invoices_user" ON "invoices"("user_id");

-- CreateIndex
CREATE INDEX "idx_invoices_status" ON "invoices"("status");

-- CreateIndex
CREATE INDEX "idx_invoices_issued" ON "invoices"("issued_at");

-- CreateIndex
CREATE UNIQUE INDEX "payments_stripe_payment_intent_id_key" ON "payments"("stripe_payment_intent_id");

-- CreateIndex
CREATE INDEX "idx_payments_invoice" ON "payments"("invoice_id");

-- CreateIndex
CREATE INDEX "idx_payments_user" ON "payments"("user_id");

-- CreateIndex
CREATE INDEX "idx_payments_status" ON "payments"("status");

-- CreateIndex
CREATE INDEX "idx_payment_audit_log_payment" ON "payment_audit_log"("payment_id");

-- CreateIndex
CREATE INDEX "idx_payment_audit_log_action" ON "payment_audit_log"("action");

-- CreateIndex
CREATE INDEX "idx_payment_audit_log_created" ON "payment_audit_log"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "products_slug_key" ON "products"("slug");

-- CreateIndex
CREATE INDEX "idx_products_type" ON "products"("type");

-- CreateIndex
CREATE INDEX "idx_products_active" ON "products"("is_active");

-- CreateIndex
CREATE UNIQUE INDEX "orders_stripe_checkout_session_id_key" ON "orders"("stripe_checkout_session_id");

-- CreateIndex
CREATE INDEX "idx_orders_user" ON "orders"("user_id");

-- CreateIndex
CREATE INDEX "idx_orders_product" ON "orders"("product_id");

-- CreateIndex
CREATE INDEX "idx_orders_status" ON "orders"("status");

-- CreateIndex
CREATE INDEX "idx_orders_type" ON "orders"("order_type");

-- CreateIndex
CREATE INDEX "idx_orders_donation_page" ON "orders"("donation_page_id");

-- CreateIndex
CREATE UNIQUE INDEX "donation_pages_slug_key" ON "donation_pages"("slug");

-- CreateIndex
CREATE INDEX "donation_pages_status_idx" ON "donation_pages"("status");

-- CreateIndex
CREATE INDEX "idx_notifications_user" ON "notifications"("user_id");

-- CreateIndex
CREATE INDEX "idx_notifications_read" ON "notifications"("is_read");

-- CreateIndex
CREATE INDEX "idx_notifications_type" ON "notifications"("type");

-- CreateIndex
CREATE INDEX "idx_notifications_created" ON "notifications"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "notification_preferences_user_id_key" ON "notification_preferences"("user_id");

-- CreateIndex
CREATE INDEX "idx_notification_preferences_user" ON "notification_preferences"("user_id");

-- CreateIndex
CREATE INDEX "idx_geo_blocking_country" ON "geo_blocking_rules"("country_code");

-- CreateIndex
CREATE INDEX "idx_geo_blocking_active" ON "geo_blocking_rules"("is_active");

-- CreateIndex
CREATE INDEX "idx_published_inbox_files_inbox_path" ON "published_inbox_files"("original_inbox_path");

-- CreateIndex
CREATE INDEX "idx_published_inbox_files_public_media" ON "published_inbox_files"("published_to_public_media_id");

-- CreateIndex
CREATE INDEX "idx_video_ocr_results_video" ON "video_ocr_results"("video_id");

-- CreateIndex
CREATE INDEX "idx_video_views_video" ON "video_views"("video_id");

-- CreateIndex
CREATE INDEX "idx_video_views_user" ON "video_views"("user_id");

-- CreateIndex
CREATE INDEX "idx_video_views_created" ON "video_views"("created_at");

-- CreateIndex
CREATE INDEX "idx_video_views_video_created" ON "video_views"("video_id", "created_at");

-- CreateIndex
CREATE INDEX "idx_video_events_video" ON "video_events"("video_id");

-- CreateIndex
CREATE INDEX "idx_video_events_view" ON "video_events"("view_id");

-- CreateIndex
CREATE INDEX "idx_video_events_type" ON "video_events"("event_type");

-- CreateIndex
CREATE INDEX "idx_video_events_created" ON "video_events"("created_at");

-- CreateIndex
CREATE INDEX "idx_video_schedule_history_video" ON "video_schedule_history"("video_id");

-- CreateIndex
CREATE INDEX "idx_video_schedule_history_scheduled" ON "video_schedule_history"("scheduled_for");

-- CreateIndex
CREATE INDEX "idx_video_schedule_history_status" ON "video_schedule_history"("status");

-- CreateIndex
CREATE INDEX "idx_video_schedule_history_user" ON "video_schedule_history"("scheduled_by_user_id");

-- CreateIndex
CREATE INDEX "docs_page_views_createdAt_idx" ON "docs_page_views"("createdAt");

-- CreateIndex
CREATE INDEX "docs_page_views_path_createdAt_idx" ON "docs_page_views"("path", "createdAt");

-- CreateIndex
CREATE UNIQUE INDEX "photos_path_key" ON "photos"("path");

-- CreateIndex
CREATE INDEX "idx_photos_orientation" ON "photos"("orientation");

-- CreateIndex
CREATE INDEX "idx_photos_producer" ON "photos"("producer");

-- CreateIndex
CREATE INDEX "idx_photos_published_locked" ON "photos"("is_published", "is_locked");

-- CreateIndex
CREATE INDEX "idx_photos_category_published" ON "photos"("category", "is_published");

-- CreateIndex
CREATE INDEX "idx_photos_album_position" ON "photos"("album_id", "album_position");

-- CreateIndex
CREATE INDEX "idx_photos_created_at" ON "photos"("created_at");

-- CreateIndex
CREATE INDEX "idx_photos_uploader" ON "photos"("uploader_id");

-- CreateIndex
CREATE UNIQUE INDEX "photo_albums_cover_photo_id_key" ON "photo_albums"("cover_photo_id");

-- CreateIndex
CREATE INDEX "idx_photo_albums_published" ON "photo_albums"("is_published");

-- CreateIndex
CREATE INDEX "idx_photo_albums_creator" ON "photo_albums"("creator_id");

-- CreateIndex
CREATE INDEX "idx_photo_upvotes_photo" ON "photo_upvotes"("photo_id");

-- CreateIndex
CREATE UNIQUE INDEX "idx_photo_upvotes_unique" ON "photo_upvotes"("photo_id", "session_id");

-- CreateIndex
CREATE INDEX "idx_photo_comments_photo_date" ON "photo_comments"("photo_id", "created_at");

-- CreateIndex
CREATE INDEX "idx_photo_comments_session" ON "photo_comments"("session_id");

-- CreateIndex
CREATE INDEX "idx_photo_views_photo_date" ON "photo_views"("photo_id", "viewed_at");

-- CreateIndex
CREATE INDEX "idx_photo_views_session" ON "photo_views"("session_id");

-- CreateIndex
CREATE INDEX "idx_photo_reactions_photo" ON "photo_reactions"("photo_id");

-- CreateIndex
CREATE UNIQUE INDEX "idx_photo_reactions_unique" ON "photo_reactions"("photo_id", "session_id", "reaction_type");

-- CreateIndex
CREATE INDEX "docs_comments_pagePath_status_idx" ON "docs_comments"("pagePath", "status");

-- CreateIndex
CREATE INDEX "docs_comments_status_createdAt_idx" ON "docs_comments"("status", "createdAt");

-- CreateIndex
CREATE INDEX "docs_comments_giteaCommentId_idx" ON "docs_comments"("giteaCommentId");

-- CreateIndex
CREATE INDEX "sms_contact_list_entries_listId_idx" ON "sms_contact_list_entries"("listId");

-- CreateIndex
CREATE INDEX "sms_contact_list_entries_phone_idx" ON "sms_contact_list_entries"("phone");

-- CreateIndex
CREATE UNIQUE INDEX "sms_contact_list_entries_listId_phone_key" ON "sms_contact_list_entries"("listId", "phone");

-- CreateIndex
CREATE INDEX "sms_campaigns_status_idx" ON "sms_campaigns"("status");

-- CreateIndex
CREATE INDEX "sms_campaigns_contactListId_idx" ON "sms_campaigns"("contactListId");

-- CreateIndex
CREATE INDEX "sms_campaigns_advocacyCampaignId_idx" ON "sms_campaigns"("advocacyCampaignId");

-- CreateIndex
CREATE INDEX "sms_campaign_recipients_campaignId_status_idx" ON "sms_campaign_recipients"("campaignId", "status");

-- CreateIndex
CREATE INDEX "sms_campaign_recipients_phone_idx" ON "sms_campaign_recipients"("phone");

-- CreateIndex
CREATE INDEX "sms_messages_phone_idx" ON "sms_messages"("phone");

-- CreateIndex
CREATE INDEX "sms_messages_campaignId_idx" ON "sms_messages"("campaignId");

-- CreateIndex
CREATE INDEX "sms_messages_conversationId_idx" ON "sms_messages"("conversationId");

-- CreateIndex
CREATE INDEX "sms_messages_direction_sentAt_idx" ON "sms_messages"("direction", "sentAt");

-- CreateIndex
CREATE INDEX "sms_conversations_status_idx" ON "sms_conversations"("status");

-- CreateIndex
CREATE INDEX "sms_conversations_lastMessageAt_idx" ON "sms_conversations"("lastMessageAt");

-- CreateIndex
CREATE UNIQUE INDEX "sms_conversations_phone_campaignId_key" ON "sms_conversations"("phone", "campaignId");

-- CreateIndex
CREATE UNIQUE INDEX "contacts_profileToken_key" ON "contacts"("profileToken");

-- CreateIndex
CREATE UNIQUE INDEX "contacts_userId_key" ON "contacts"("userId");

-- CreateIndex
CREATE INDEX "contacts_email_idx" ON "contacts"("email");

-- CreateIndex
CREATE INDEX "contacts_phone_idx" ON "contacts"("phone");

-- CreateIndex
CREATE INDEX "contacts_displayName_idx" ON "contacts"("displayName");

-- CreateIndex
CREATE INDEX "contacts_primarySource_idx" ON "contacts"("primarySource");

-- CreateIndex
CREATE INDEX "contacts_mergedIntoId_idx" ON "contacts"("mergedIntoId");

-- CreateIndex
CREATE UNIQUE INDEX "crm_tags_name_key" ON "crm_tags"("name");

-- CreateIndex
CREATE UNIQUE INDEX "crm_tags_slug_key" ON "crm_tags"("slug");

-- CreateIndex
CREATE INDEX "crm_tags_name_idx" ON "crm_tags"("name");

-- CreateIndex
CREATE INDEX "contact_addresses_addressId_idx" ON "contact_addresses"("addressId");

-- CreateIndex
CREATE UNIQUE INDEX "contact_addresses_contactId_addressId_key" ON "contact_addresses"("contactId", "addressId");

-- CreateIndex
CREATE INDEX "contact_emails_email_idx" ON "contact_emails"("email");

-- CreateIndex
CREATE INDEX "contact_emails_contactId_idx" ON "contact_emails"("contactId");

-- CreateIndex
CREATE UNIQUE INDEX "contact_emails_contactId_email_key" ON "contact_emails"("contactId", "email");

-- CreateIndex
CREATE INDEX "contact_phones_phone_idx" ON "contact_phones"("phone");

-- CreateIndex
CREATE INDEX "contact_phones_contactId_idx" ON "contact_phones"("contactId");

-- CreateIndex
CREATE UNIQUE INDEX "contact_phones_contactId_phone_key" ON "contact_phones"("contactId", "phone");

-- CreateIndex
CREATE INDEX "contact_connections_toContactId_idx" ON "contact_connections"("toContactId");

-- CreateIndex
CREATE UNIQUE INDEX "contact_connections_fromContactId_toContactId_type_key" ON "contact_connections"("fromContactId", "toContactId", "type");

-- CreateIndex
CREATE INDEX "contact_activities_contactId_occurredAt_idx" ON "contact_activities"("contactId", "occurredAt" DESC);

-- CreateIndex
CREATE INDEX "campaign_emails_userPostalCode_idx" ON "campaign_emails"("userPostalCode");

-- CreateIndex
CREATE INDEX "campaign_emails_sentAt_idx" ON "campaign_emails"("sentAt");

-- CreateIndex
CREATE INDEX "campaigns_moderationStatus_idx" ON "campaigns"("moderationStatus");

-- CreateIndex
CREATE INDEX "campaigns_isUserGenerated_idx" ON "campaigns"("isUserGenerated");

-- CreateIndex
CREATE INDEX "canvass_visits_addressId_idx" ON "canvass_visits"("addressId");

-- CreateIndex
CREATE INDEX "canvass_visits_addressId_visitedAt_idx" ON "canvass_visits"("addressId", "visitedAt" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "locations_locGuid_key" ON "locations"("locGuid");

-- CreateIndex
CREATE INDEX "locations_latitude_longitude_idx" ON "locations"("latitude", "longitude");

-- CreateIndex
CREATE INDEX "locations_latitude_idx" ON "locations"("latitude");

-- CreateIndex
CREATE INDEX "locations_longitude_idx" ON "locations"("longitude");

-- CreateIndex
CREATE INDEX "locations_postalCode_idx" ON "locations"("postalCode");

-- CreateIndex
CREATE INDEX "representative_responses_representativeName_idx" ON "representative_responses"("representativeName");

-- CreateIndex
CREATE INDEX "shifts_seriesId_idx" ON "shifts"("seriesId");

-- AddForeignKey
ALTER TABLE "campaigns" ADD CONSTRAINT "campaigns_reviewedByUserId_fkey" FOREIGN KEY ("reviewedByUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "addresses" ADD CONSTRAINT "addresses_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "locations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "addresses" ADD CONSTRAINT "addresses_createdByUserId_fkey" FOREIGN KEY ("createdByUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "addresses" ADD CONSTRAINT "addresses_updatedByUserId_fkey" FOREIGN KEY ("updatedByUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "location_history" ADD CONSTRAINT "location_history_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "locations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "location_history" ADD CONSTRAINT "location_history_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "shifts" ADD CONSTRAINT "shifts_seriesId_fkey" FOREIGN KEY ("seriesId") REFERENCES "shift_series"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "shift_series" ADD CONSTRAINT "shift_series_cutId_fkey" FOREIGN KEY ("cutId") REFERENCES "cuts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "email_templates" ADD CONSTRAINT "email_templates_createdByUserId_fkey" FOREIGN KEY ("createdByUserId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "email_templates" ADD CONSTRAINT "email_templates_updatedByUserId_fkey" FOREIGN KEY ("updatedByUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "email_template_variables" ADD CONSTRAINT "email_template_variables_templateId_fkey" FOREIGN KEY ("templateId") REFERENCES "email_templates"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "email_template_versions" ADD CONSTRAINT "email_template_versions_templateId_fkey" FOREIGN KEY ("templateId") REFERENCES "email_templates"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "email_template_versions" ADD CONSTRAINT "email_template_versions_createdByUserId_fkey" FOREIGN KEY ("createdByUserId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "email_template_test_logs" ADD CONSTRAINT "email_template_test_logs_templateId_fkey" FOREIGN KEY ("templateId") REFERENCES "email_templates"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "email_template_test_logs" ADD CONSTRAINT "email_template_test_logs_sentByUserId_fkey" FOREIGN KEY ("sentByUserId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "canvass_visits" ADD CONSTRAINT "canvass_visits_addressId_fkey" FOREIGN KEY ("addressId") REFERENCES "addresses"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "videos" ADD CONSTRAINT "videos_uploader_id_fkey" FOREIGN KEY ("uploader_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "videos" ADD CONSTRAINT "videos_locked_by_id_fkey" FOREIGN KEY ("locked_by_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "jobs" ADD CONSTRAINT "jobs_pipeline_id_fkey" FOREIGN KEY ("pipeline_id") REFERENCES "pipelines"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "jobs" ADD CONSTRAINT "jobs_pipeline_step_id_fkey" FOREIGN KEY ("pipeline_step_id") REFERENCES "pipeline_steps"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "upvotes" ADD CONSTRAINT "upvotes_media_id_fkey" FOREIGN KEY ("media_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "upvotes" ADD CONSTRAINT "upvotes_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "sessions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "comments" ADD CONSTRAINT "comments_media_id_fkey" FOREIGN KEY ("media_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "comments" ADD CONSTRAINT "comments_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "sessions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "comments" ADD CONSTRAINT "comments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "views" ADD CONSTRAINT "views_media_id_fkey" FOREIGN KEY ("media_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "views" ADD CONSTRAINT "views_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "sessions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "auth_tokens" ADD CONSTRAINT "auth_tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "session_bans" ADD CONSTRAINT "session_bans_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "sessions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "session_bans" ADD CONSTRAINT "session_bans_banned_by_fkey" FOREIGN KEY ("banned_by") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "comment_moderation" ADD CONSTRAINT "comment_moderation_comment_id_fkey" FOREIGN KEY ("comment_id") REFERENCES "comments"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "comment_moderation" ADD CONSTRAINT "comment_moderation_moderated_by_fkey" FOREIGN KEY ("moderated_by") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "email_verification_tokens" ADD CONSTRAINT "email_verification_tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "password_reset_tokens" ADD CONSTRAINT "password_reset_tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "email_change_tokens" ADD CONSTRAINT "email_change_tokens_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_achievements" ADD CONSTRAINT "user_achievements_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_stats" ADD CONSTRAINT "user_stats_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_finishes" ADD CONSTRAINT "user_finishes_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_finishes" ADD CONSTRAINT "user_finishes_media_id_fkey" FOREIGN KEY ("media_id") REFERENCES "videos"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_finishes" ADD CONSTRAINT "user_finishes_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "sessions"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_reactions" ADD CONSTRAINT "video_reactions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_reactions" ADD CONSTRAINT "video_reactions_media_id_fkey" FOREIGN KEY ("media_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "highlight_cooldowns" ADD CONSTRAINT "highlight_cooldowns_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_daily_activity" ADD CONSTRAINT "user_daily_activity_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "chat_thread_read_status" ADD CONSTRAINT "chat_thread_read_status_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "moderation_word_lists" ADD CONSTRAINT "moderation_word_lists_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "content_reports" ADD CONSTRAINT "content_reports_media_id_fkey" FOREIGN KEY ("media_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "content_reports" ADD CONSTRAINT "content_reports_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "sessions"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "content_reports" ADD CONSTRAINT "content_reports_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "content_reports" ADD CONSTRAINT "content_reports_resolved_by_fkey" FOREIGN KEY ("resolved_by") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "playlists" ADD CONSTRAINT "playlists_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "playlist_videos" ADD CONSTRAINT "playlist_videos_playlist_id_fkey" FOREIGN KEY ("playlist_id") REFERENCES "playlists"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "playlist_videos" ADD CONSTRAINT "playlist_videos_media_id_fkey" FOREIGN KEY ("media_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "featured_playlists" ADD CONSTRAINT "featured_playlists_playlist_id_fkey" FOREIGN KEY ("playlist_id") REFERENCES "playlists"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "featured_playlists" ADD CONSTRAINT "featured_playlists_featured_by_fkey" FOREIGN KEY ("featured_by") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "playlist_views" ADD CONSTRAINT "playlist_views_playlist_id_fkey" FOREIGN KEY ("playlist_id") REFERENCES "playlists"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "playlist_views" ADD CONSTRAINT "playlist_views_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "sessions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ads" ADD CONSTRAINT "ads_product_id_fkey" FOREIGN KEY ("product_id") REFERENCES "products"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ad_impressions" ADD CONSTRAINT "ad_impressions_ad_id_fkey" FOREIGN KEY ("ad_id") REFERENCES "ads"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ad_impressions" ADD CONSTRAINT "ad_impressions_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "sessions"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ad_impressions" ADD CONSTRAINT "ad_impressions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ad_clicks" ADD CONSTRAINT "ad_clicks_ad_id_fkey" FOREIGN KEY ("ad_id") REFERENCES "ads"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ad_clicks" ADD CONSTRAINT "ad_clicks_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "sessions"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ad_clicks" ADD CONSTRAINT "ad_clicks_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "friendships" ADD CONSTRAINT "friendships_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "friendships" ADD CONSTRAINT "friendships_friend_id_fkey" FOREIGN KEY ("friend_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_blocks" ADD CONSTRAINT "user_blocks_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_blocks" ADD CONSTRAINT "user_blocks_blocked_user_id_fkey" FOREIGN KEY ("blocked_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pokes" ADD CONSTRAINT "pokes_from_user_id_fkey" FOREIGN KEY ("from_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pokes" ADD CONSTRAINT "pokes_to_user_id_fkey" FOREIGN KEY ("to_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_recommendations" ADD CONSTRAINT "video_recommendations_from_user_id_fkey" FOREIGN KEY ("from_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_recommendations" ADD CONSTRAINT "video_recommendations_to_user_id_fkey" FOREIGN KEY ("to_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_recommendations" ADD CONSTRAINT "video_recommendations_media_id_fkey" FOREIGN KEY ("media_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_presence" ADD CONSTRAINT "user_presence_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_gallery_images" ADD CONSTRAINT "user_gallery_images_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_social_links" ADD CONSTRAINT "user_social_links_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "privacy_settings" ADD CONSTRAINT "privacy_settings_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "close_friends" ADD CONSTRAINT "close_friends_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "close_friends" ADD CONSTRAINT "close_friends_close_friend_id_fkey" FOREIGN KEY ("close_friend_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_uploads" ADD CONSTRAINT "user_uploads_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_uploads" ADD CONSTRAINT "user_uploads_reviewed_by_fkey" FOREIGN KEY ("reviewed_by") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_uploads" ADD CONSTRAINT "user_uploads_upload_invite_id_fkey" FOREIGN KEY ("upload_invite_id") REFERENCES "upload_invites"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "upload_invites" ADD CONSTRAINT "upload_invites_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tags" ADD CONSTRAINT "tags_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "tag_categories"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public_media_tags" ADD CONSTRAINT "public_media_tags_media_id_fkey" FOREIGN KEY ("media_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public_media_tags" ADD CONSTRAINT "public_media_tags_tag_id_fkey" FOREIGN KEY ("tag_id") REFERENCES "tags"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_upload_suggested_tags" ADD CONSTRAINT "user_upload_suggested_tags_upload_id_fkey" FOREIGN KEY ("upload_id") REFERENCES "user_uploads"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_upload_suggested_tags" ADD CONSTRAINT "user_upload_suggested_tags_tag_id_fkey" FOREIGN KEY ("tag_id") REFERENCES "tags"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_tag_preferences" ADD CONSTRAINT "user_tag_preferences_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_tag_preferences" ADD CONSTRAINT "user_tag_preferences_tag_id_fkey" FOREIGN KEY ("tag_id") REFERENCES "tags"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public_media_performers" ADD CONSTRAINT "public_media_performers_media_id_fkey" FOREIGN KEY ("media_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public_media_performers" ADD CONSTRAINT "public_media_performers_performer_id_fkey" FOREIGN KEY ("performer_id") REFERENCES "creators"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_digests" ADD CONSTRAINT "video_digests_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_digests" ADD CONSTRAINT "video_digests_job_id_fkey" FOREIGN KEY ("job_id") REFERENCES "jobs"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_video_tags" ADD CONSTRAINT "digest_video_tags_digest_id_fkey" FOREIGN KEY ("digest_id") REFERENCES "video_digests"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_video_tags" ADD CONSTRAINT "digest_video_tags_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_selected_clips" ADD CONSTRAINT "digest_selected_clips_digest_id_fkey" FOREIGN KEY ("digest_id") REFERENCES "video_digests"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_selected_clips" ADD CONSTRAINT "digest_selected_clips_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_generated_clips" ADD CONSTRAINT "digest_generated_clips_selected_clip_id_fkey" FOREIGN KEY ("selected_clip_id") REFERENCES "digest_selected_clips"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_generated_clips" ADD CONSTRAINT "digest_generated_clips_digest_id_fkey" FOREIGN KEY ("digest_id") REFERENCES "video_digests"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_generated_clips" ADD CONSTRAINT "digest_generated_clips_folder_id_fkey" FOREIGN KEY ("folder_id") REFERENCES "digest_output_folders"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_compilations" ADD CONSTRAINT "digest_compilations_digest_id_fkey" FOREIGN KEY ("digest_id") REFERENCES "video_digests"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_compilations" ADD CONSTRAINT "digest_compilations_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_compilations" ADD CONSTRAINT "digest_compilations_folder_id_fkey" FOREIGN KEY ("folder_id") REFERENCES "digest_output_folders"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_output_folders" ADD CONSTRAINT "digest_output_folders_digest_id_fkey" FOREIGN KEY ("digest_id") REFERENCES "video_digests"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_output_folders" ADD CONSTRAINT "digest_output_folders_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_generated_scenes" ADD CONSTRAINT "digest_generated_scenes_digest_id_fkey" FOREIGN KEY ("digest_id") REFERENCES "video_digests"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_generated_scenes" ADD CONSTRAINT "digest_generated_scenes_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_generated_scenes" ADD CONSTRAINT "digest_generated_scenes_folder_id_fkey" FOREIGN KEY ("folder_id") REFERENCES "digest_output_folders"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_scene_cuts" ADD CONSTRAINT "video_scene_cuts_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_tag_timeline" ADD CONSTRAINT "video_tag_timeline_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_segments" ADD CONSTRAINT "video_segments_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_tags" ADD CONSTRAINT "video_tags_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_suggested_tags" ADD CONSTRAINT "digest_suggested_tags_digest_id_fkey" FOREIGN KEY ("digest_id") REFERENCES "video_digests"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "digest_clip_tags" ADD CONSTRAINT "digest_clip_tags_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watch_party_sessions" ADD CONSTRAINT "watch_party_sessions_host_user_id_fkey" FOREIGN KEY ("host_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watch_party_participants" ADD CONSTRAINT "watch_party_participants_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "watch_party_sessions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watch_party_participants" ADD CONSTRAINT "watch_party_participants_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watch_party_chat_messages" ADD CONSTRAINT "watch_party_chat_messages_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "watch_party_sessions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watch_party_chat_messages" ADD CONSTRAINT "watch_party_chat_messages_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watch_party_chat_messages" ADD CONSTRAINT "watch_party_chat_messages_comment_id_fkey" FOREIGN KEY ("comment_id") REFERENCES "comments"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watch_party_reactions" ADD CONSTRAINT "watch_party_reactions_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "watch_party_sessions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watch_party_reactions" ADD CONSTRAINT "watch_party_reactions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watch_party_invites" ADD CONSTRAINT "watch_party_invites_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "watch_party_sessions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watch_party_invites" ADD CONSTRAINT "watch_party_invites_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "watch_party_invites" ADD CONSTRAINT "watch_party_invites_invited_by_fkey" FOREIGN KEY ("invited_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tag_generation_jobs" ADD CONSTRAINT "tag_generation_jobs_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tag_generation_jobs" ADD CONSTRAINT "tag_generation_jobs_job_id_fkey" FOREIGN KEY ("job_id") REFERENCES "jobs"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "performer_faces" ADD CONSTRAINT "performer_faces_performer_id_fkey" FOREIGN KEY ("performer_id") REFERENCES "creators"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "performer_discrepancies" ADD CONSTRAINT "performer_discrepancies_assigned_performer_id_fkey" FOREIGN KEY ("assigned_performer_id") REFERENCES "creators"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "performer_discrepancies" ADD CONSTRAINT "performer_discrepancies_resolved_by_fkey" FOREIGN KEY ("resolved_by") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pipelines" ADD CONSTRAINT "pipelines_template_id_fkey" FOREIGN KEY ("template_id") REFERENCES "pipeline_templates"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pipeline_steps" ADD CONSTRAINT "pipeline_steps_pipeline_id_fkey" FOREIGN KEY ("pipeline_id") REFERENCES "pipelines"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pipeline_step_events" ADD CONSTRAINT "pipeline_step_events_step_id_fkey" FOREIGN KEY ("step_id") REFERENCES "pipeline_steps"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_subscriptions" ADD CONSTRAINT "user_subscriptions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_subscriptions" ADD CONSTRAINT "user_subscriptions_plan_id_fkey" FOREIGN KEY ("plan_id") REFERENCES "subscription_plans"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "payments" ADD CONSTRAINT "payments_invoice_id_fkey" FOREIGN KEY ("invoice_id") REFERENCES "invoices"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "payments" ADD CONSTRAINT "payments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "payment_audit_log" ADD CONSTRAINT "payment_audit_log_payment_id_fkey" FOREIGN KEY ("payment_id") REFERENCES "payments"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "payment_audit_log" ADD CONSTRAINT "payment_audit_log_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "orders" ADD CONSTRAINT "orders_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "orders" ADD CONSTRAINT "orders_product_id_fkey" FOREIGN KEY ("product_id") REFERENCES "products"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "orders" ADD CONSTRAINT "orders_donation_page_id_fkey" FOREIGN KEY ("donation_page_id") REFERENCES "donation_pages"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "donation_pages" ADD CONSTRAINT "donation_pages_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "notifications" ADD CONSTRAINT "notifications_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "notification_preferences" ADD CONSTRAINT "notification_preferences_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_ocr_results" ADD CONSTRAINT "video_ocr_results_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_views" ADD CONSTRAINT "video_views_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_views" ADD CONSTRAINT "video_views_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_events" ADD CONSTRAINT "video_events_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_schedule_history" ADD CONSTRAINT "video_schedule_history_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "video_schedule_history" ADD CONSTRAINT "video_schedule_history_scheduled_by_user_id_fkey" FOREIGN KEY ("scheduled_by_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "photos" ADD CONSTRAINT "photos_album_id_fkey" FOREIGN KEY ("album_id") REFERENCES "photo_albums"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "photos" ADD CONSTRAINT "photos_uploader_id_fkey" FOREIGN KEY ("uploader_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "photo_albums" ADD CONSTRAINT "photo_albums_cover_photo_id_fkey" FOREIGN KEY ("cover_photo_id") REFERENCES "photos"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "photo_albums" ADD CONSTRAINT "photo_albums_creator_id_fkey" FOREIGN KEY ("creator_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "photo_upvotes" ADD CONSTRAINT "photo_upvotes_photo_id_fkey" FOREIGN KEY ("photo_id") REFERENCES "photos"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "photo_upvotes" ADD CONSTRAINT "photo_upvotes_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "sessions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "photo_comments" ADD CONSTRAINT "photo_comments_photo_id_fkey" FOREIGN KEY ("photo_id") REFERENCES "photos"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "photo_comments" ADD CONSTRAINT "photo_comments_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "sessions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "photo_comments" ADD CONSTRAINT "photo_comments_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "photo_views" ADD CONSTRAINT "photo_views_photo_id_fkey" FOREIGN KEY ("photo_id") REFERENCES "photos"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "photo_reactions" ADD CONSTRAINT "photo_reactions_photo_id_fkey" FOREIGN KEY ("photo_id") REFERENCES "photos"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "photo_reactions" ADD CONSTRAINT "photo_reactions_session_id_fkey" FOREIGN KEY ("session_id") REFERENCES "sessions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sms_contact_lists" ADD CONSTRAINT "sms_contact_lists_createdByUserId_fkey" FOREIGN KEY ("createdByUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sms_contact_list_entries" ADD CONSTRAINT "sms_contact_list_entries_listId_fkey" FOREIGN KEY ("listId") REFERENCES "sms_contact_lists"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sms_campaigns" ADD CONSTRAINT "sms_campaigns_contactListId_fkey" FOREIGN KEY ("contactListId") REFERENCES "sms_contact_lists"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sms_campaigns" ADD CONSTRAINT "sms_campaigns_advocacyCampaignId_fkey" FOREIGN KEY ("advocacyCampaignId") REFERENCES "campaigns"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sms_campaigns" ADD CONSTRAINT "sms_campaigns_createdByUserId_fkey" FOREIGN KEY ("createdByUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sms_campaign_recipients" ADD CONSTRAINT "sms_campaign_recipients_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "sms_campaigns"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sms_messages" ADD CONSTRAINT "sms_messages_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "sms_campaigns"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sms_messages" ADD CONSTRAINT "sms_messages_conversationId_fkey" FOREIGN KEY ("conversationId") REFERENCES "sms_conversations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sms_conversations" ADD CONSTRAINT "sms_conversations_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "sms_campaigns"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sms_message_templates" ADD CONSTRAINT "sms_message_templates_createdByUserId_fkey" FOREIGN KEY ("createdByUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "contacts" ADD CONSTRAINT "contacts_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "contacts" ADD CONSTRAINT "contacts_mergedIntoId_fkey" FOREIGN KEY ("mergedIntoId") REFERENCES "contacts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "contact_addresses" ADD CONSTRAINT "contact_addresses_contactId_fkey" FOREIGN KEY ("contactId") REFERENCES "contacts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "contact_addresses" ADD CONSTRAINT "contact_addresses_addressId_fkey" FOREIGN KEY ("addressId") REFERENCES "addresses"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "contact_emails" ADD CONSTRAINT "contact_emails_contactId_fkey" FOREIGN KEY ("contactId") REFERENCES "contacts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "contact_phones" ADD CONSTRAINT "contact_phones_contactId_fkey" FOREIGN KEY ("contactId") REFERENCES "contacts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "contact_connections" ADD CONSTRAINT "contact_connections_fromContactId_fkey" FOREIGN KEY ("fromContactId") REFERENCES "contacts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "contact_connections" ADD CONSTRAINT "contact_connections_toContactId_fkey" FOREIGN KEY ("toContactId") REFERENCES "contacts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "contact_activities" ADD CONSTRAINT "contact_activities_contactId_fkey" FOREIGN KEY ("contactId") REFERENCES "contacts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

