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

-- CreateEnum
CREATE TYPE "UserRole" AS ENUM ('SUPER_ADMIN', 'INFLUENCE_ADMIN', 'MAP_ADMIN', 'BROADCAST_ADMIN', 'CONTENT_ADMIN', 'MEDIA_ADMIN', 'PAYMENTS_ADMIN', 'EVENTS_ADMIN', 'SOCIAL_ADMIN', 'POLLS_ADMIN', 'ANALYTICS_ADMIN', 'USER', 'TEMP');

-- CreateEnum
CREATE TYPE "UserStatus" AS ENUM ('ACTIVE', 'INACTIVE', 'SUSPENDED', 'EXPIRED', 'PENDING_VERIFICATION', 'PENDING_APPROVAL');

-- CreateEnum
CREATE TYPE "UserCreatedVia" AS ENUM ('ADMIN', 'PUBLIC_SHIFT_SIGNUP', 'STANDARD', 'SELF_REGISTRATION', 'QUICK_JOIN_INVITE');

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

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

-- CreateEnum
CREATE TYPE "GovernmentLevel" AS ENUM ('FEDERAL', 'PROVINCIAL', 'MUNICIPAL', 'SCHOOL_BOARD');

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

-- CreateEnum
CREATE TYPE "PetitionSignatureStatus" AS ENUM ('PENDING_VERIFICATION', 'VERIFIED', 'UNVERIFIED', 'REJECTED');

-- CreateEnum
CREATE TYPE "EmailMethod" AS ENUM ('SMTP', 'MAILTO');

-- CreateEnum
CREATE TYPE "CampaignEmailStatus" AS ENUM ('QUEUED', 'SENT', 'FAILED', 'CLICKED', 'USER_INFO_CAPTURED');

-- CreateEnum
CREATE TYPE "ResponseType" AS ENUM ('EMAIL', 'LETTER', 'PHONE_CALL', 'MEETING', 'SOCIAL_MEDIA', 'OTHER');

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

-- CreateEnum
CREATE TYPE "SupportLevel" AS ENUM ('1', '2', '3', '4');

-- CreateEnum
CREATE TYPE "GeocodeProvider" AS ENUM ('GOOGLE', 'MAPBOX', 'NOMINATIM', 'PHOTON', 'LOCATIONIQ', 'ARCGIS', 'UNKNOWN');

-- 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 "ShiftStatus" AS ENUM ('OPEN', 'FULL', 'CANCELLED');

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

-- CreateEnum
CREATE TYPE "SignupStatus" AS ENUM ('CONFIRMED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "SignupSource" AS ENUM ('AUTHENTICATED', 'PUBLIC', 'ADMIN', 'POLL_CONVERSION');

-- CreateEnum
CREATE TYPE "ShiftKind" AS ENUM ('CANVASS', 'TRAINING', 'EVENT_STAFFING', 'PHONE_BANK', 'OTHER');

-- CreateEnum
CREATE TYPE "CutCategory" AS ENUM ('CUSTOM', 'WARD', 'NEIGHBORHOOD', 'DISTRICT');

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

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

-- CreateEnum
CREATE TYPE "EditorMode" AS ENUM ('VISUAL', 'CODE');

-- CreateEnum
CREATE TYPE "MkdocsExportMode" AS ENUM ('THEMED', 'STANDALONE');

-- CreateEnum
CREATE TYPE "VisitOutcome" AS ENUM ('NOT_HOME', 'REFUSED', 'MOVED', 'ALREADY_VOTED', 'SPOKE_WITH', 'LEFT_LITERATURE', 'COME_BACK_LATER');

-- CreateEnum
CREATE TYPE "CanvassSessionStatus" AS ENUM ('ACTIVE', 'COMPLETED', 'ABANDONED');

-- CreateEnum
CREATE TYPE "TrackPointEvent" AS ENUM ('LOCATION_ADDED', 'VISIT_RECORDED', 'SESSION_STARTED', 'SESSION_ENDED');

-- 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', 'DISPUTED');

-- CreateEnum
CREATE TYPE "NotificationType" AS ENUM ('friend_request', 'friend_accepted', 'poke', 'comment', 'upload_approved', 'upload_rejected', 'achievement', 'system', 'group_call', 'impact_story', 'referral_completed', 'challenge_update', 'shared_view_invite', 'shared_view_accepted', 'calendar_event_invite', 'shift_signup_confirmed', 'shift_reminder', 'shift_cancelled', 'canvass_session_summary', 'reengagement', 'poll_closed', 'poll_results_available', 'poll_challenge');

-- CreateEnum
CREATE TYPE "SocialGroupType" AS ENUM ('SHIFT_TEAM', 'CAMPAIGN_TEAM', 'CUSTOM');

-- 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', 'POLL_VOTE', 'PETITION_SIGNER', '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', 'POLL_VOTED', 'PETITION_SIGNED');

-- CreateEnum
CREATE TYPE "SchedulingPollStatus" AS ENUM ('OPEN', 'CLOSED', 'FINALIZED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "PollVoteValue" AS ENUM ('YES', 'IF_NEED_BE', 'NO');

-- CreateEnum
CREATE TYPE "ImpactStoryType" AS ENUM ('MILESTONE', 'VICTORY', 'RESPONSE', 'CUSTOM');

-- CreateEnum
CREATE TYPE "ImpactStoryStatus" AS ENUM ('DRAFT', 'PUBLISHED', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "SpotlightStatus" AS ENUM ('NOMINATED', 'APPROVED', 'FEATURED', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "ChallengeStatus" AS ENUM ('DRAFT', 'UPCOMING', 'ACTIVE', 'COMPLETED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "ChallengeMetric" AS ENUM ('DOORS_KNOCKED', 'EMAILS_SENT', 'SHIFTS_ATTENDED', 'RESPONSES_SUBMITTED', 'REFERRALS_MADE');

-- CreateEnum
CREATE TYPE "TicketedEventStatus" AS ENUM ('DRAFT', 'PENDING_APPROVAL', 'PUBLISHED', 'CANCELLED', 'COMPLETED');

-- CreateEnum
CREATE TYPE "TicketedEventVisibility" AS ENUM ('PUBLIC', 'UNLISTED', 'PRIVATE');

-- CreateEnum
CREATE TYPE "TicketTierType" AS ENUM ('PAID', 'FREE', 'DONATION');

-- CreateEnum
CREATE TYPE "TicketStatus" AS ENUM ('VALID', 'CHECKED_IN', 'CANCELLED', 'REFUNDED');

-- CreateEnum
CREATE TYPE "EventFormat" AS ENUM ('IN_PERSON', 'ONLINE', 'HYBRID');

-- CreateEnum
CREATE TYPE "CalendarLayerType" AS ENUM ('SYSTEM', 'USER', 'EXTERNAL');

-- CreateEnum
CREATE TYPE "CalendarSystemType" AS ENUM ('SHIFTS', 'TICKETS', 'POLLS', 'PUBLIC_EVENTS');

-- CreateEnum
CREATE TYPE "CalendarVisibility" AS ENUM ('PRIVATE', 'FRIENDS', 'PUBLIC');

-- CreateEnum
CREATE TYPE "CalendarItemType" AS ENUM ('EVENT', 'TIME_BLOCK', 'REMINDER');

-- CreateEnum
CREATE TYPE "CalendarBusyStatus" AS ENUM ('BUSY', 'TENTATIVE', 'FREE');

-- CreateEnum
CREATE TYPE "CalendarShowDetailsTo" AS ENUM ('NOBODY', 'FRIENDS', 'EVERYONE');

-- CreateEnum
CREATE TYPE "CalendarItemSource" AS ENUM ('MANUAL', 'ICS_FEED', 'POLL', 'SHIFT', 'MEETING', 'TICKETED_EVENT');

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

-- CreateEnum
CREATE TYPE "CalendarFeedStatus" AS ENUM ('OK', 'ERROR', 'PENDING');

-- CreateEnum
CREATE TYPE "CalendarFeedInterval" AS ENUM ('FIFTEEN_MIN', 'HOURLY', 'SIX_HOUR', 'DAILY');

-- CreateEnum
CREATE TYPE "SharedViewType" AS ENUM ('MANUAL', 'ROLE_BASED');

-- CreateEnum
CREATE TYPE "SharedViewScope" AS ENUM ('MEMBERS', 'PUBLIC');

-- CreateEnum
CREATE TYPE "SharedViewMemberStatus" AS ENUM ('INVITED', 'ACCEPTED', 'DECLINED');

-- CreateEnum
CREATE TYPE "DocShareLinkStatus" AS ENUM ('ACTIVE', 'REVOKED', 'EXPIRED');

-- CreateEnum
CREATE TYPE "StrawPollType" AS ENUM ('SINGLE_CHOICE', 'YES_NO_ABSTAIN');

-- CreateEnum
CREATE TYPE "StrawPollStatus" AS ENUM ('DRAFT', 'ACTIVE', 'CLOSED', 'ARCHIVED');

-- CreateEnum
CREATE TYPE "StrawPollIdentityMode" AS ENUM ('ANONYMOUS', 'TOKEN_GATED', 'AUTHENTICATED', 'MIXED');

-- CreateEnum
CREATE TYPE "StrawPollResultVisibility" AS ENUM ('LIVE', 'AFTER_VOTE', 'AFTER_CLOSE', 'CREATOR_ONLY', 'PUBLIC_ALWAYS');

-- CreateEnum
CREATE TYPE "ActionStepKind" AS ENUM ('WATCH_VIDEO', 'SUBMIT_INFLUENCE', 'SIGN_PETITION', 'RSVP_EVENT', 'SIGNUP_SHIFT', 'JOIN_CHALLENGE', 'VISIT_LINK', 'CUSTOM');

-- CreateEnum
CREATE TYPE "ActionStepCompletionSource" AS ENUM ('AUTO', 'SELF_REPORTED');

-- CreateTable
CREATE TABLE "users" (
    "id" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "name" TEXT,
    "phone" TEXT,
    "pronouns" TEXT,
    "role" "UserRole" NOT NULL DEFAULT 'USER',
    "roles" JSONB NOT NULL DEFAULT '[]',
    "status" "UserStatus" NOT NULL DEFAULT 'ACTIVE',
    "permissions" JSONB,
    "createdVia" "UserCreatedVia" NOT NULL DEFAULT 'STANDARD',
    "expiresAt" TIMESTAMP(3),
    "expireDays" INTEGER,
    "lastLoginAt" TIMESTAMP(3),
    "emailVerified" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "refresh_tokens" (
    "id" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "expiresAt" TIMESTAMP(3) NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "campaigns" (
    "id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "emailSubject" TEXT NOT NULL,
    "emailBody" TEXT NOT NULL,
    "callToAction" TEXT,
    "coverPhoto" TEXT,
    "coverVideoId" INTEGER,
    "status" "CampaignStatus" NOT NULL DEFAULT 'DRAFT',
    "allowSmtpEmail" BOOLEAN NOT NULL DEFAULT true,
    "allowMailtoLink" BOOLEAN NOT NULL DEFAULT true,
    "collectUserInfo" BOOLEAN NOT NULL DEFAULT true,
    "showEmailCount" BOOLEAN NOT NULL DEFAULT true,
    "showCallCount" BOOLEAN NOT NULL DEFAULT true,
    "allowEmailEditing" BOOLEAN NOT NULL DEFAULT false,
    "allowCustomRecipients" BOOLEAN NOT NULL DEFAULT false,
    "showResponseWall" BOOLEAN NOT NULL DEFAULT false,
    "highlightCampaign" BOOLEAN NOT NULL DEFAULT false,
    "targetGovernmentLevels" "GovernmentLevel"[],
    "createdByUserId" TEXT,
    "createdByUserEmail" TEXT,
    "createdByUserName" TEXT,
    "isUserGenerated" BOOLEAN NOT NULL DEFAULT false,
    "moderationStatus" "CampaignModerationStatus",
    "reviewedByUserId" TEXT,
    "reviewedAt" TIMESTAMP(3),
    "rejectionReason" TEXT,
    "moderationNotes" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "petitions" (
    "id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "signatureGoal" INTEGER,
    "showProgress" BOOLEAN NOT NULL DEFAULT true,
    "showSignatureCount" BOOLEAN NOT NULL DEFAULT true,
    "showSignerNames" BOOLEAN NOT NULL DEFAULT true,
    "signatureCountOffset" INTEGER NOT NULL DEFAULT 0,
    "requireName" BOOLEAN NOT NULL DEFAULT true,
    "requireEmail" BOOLEAN NOT NULL DEFAULT true,
    "requirePostalCode" BOOLEAN NOT NULL DEFAULT false,
    "requirePhone" BOOLEAN NOT NULL DEFAULT false,
    "allowComment" BOOLEAN NOT NULL DEFAULT true,
    "commentLabel" TEXT,
    "requireEmailConfirmation" BOOLEAN NOT NULL DEFAULT false,
    "confirmationEmailSubject" TEXT,
    "confirmationEmailBody" TEXT,
    "coverPhoto" TEXT,
    "coverVideoId" INTEGER,
    "callToAction" TEXT,
    "thankYouMessage" TEXT,
    "highlightPetition" BOOLEAN NOT NULL DEFAULT false,
    "linkedCampaignId" TEXT,
    "status" "PetitionStatus" NOT NULL DEFAULT 'DRAFT',
    "isUserGenerated" BOOLEAN NOT NULL DEFAULT false,
    "moderationStatus" "CampaignModerationStatus",
    "rejectionReason" TEXT,
    "moderationNotes" TEXT,
    "createdByUserId" TEXT,
    "createdByUserEmail" TEXT,
    "createdByUserName" TEXT,
    "reviewedByUserId" TEXT,
    "reviewedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "petition_signatures" (
    "id" TEXT NOT NULL,
    "petitionId" TEXT NOT NULL,
    "signerName" TEXT,
    "signerEmail" TEXT,
    "signerPostalCode" TEXT,
    "signerPhone" TEXT,
    "signerComment" TEXT,
    "isAnonymous" BOOLEAN NOT NULL DEFAULT false,
    "displayName" TEXT,
    "status" "PetitionSignatureStatus" NOT NULL DEFAULT 'UNVERIFIED',
    "verificationToken" TEXT,
    "verificationSentAt" TIMESTAMP(3),
    "verifiedAt" TIMESTAMP(3),
    "contactId" TEXT,
    "signerIp" TEXT,
    "geoCountry" TEXT,
    "geoRegion" TEXT,
    "geoCity" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "representatives" (
    "id" TEXT NOT NULL,
    "postalCode" TEXT NOT NULL,
    "name" TEXT,
    "email" TEXT,
    "districtName" TEXT,
    "electedOffice" TEXT,
    "partyName" TEXT,
    "representativeSetName" TEXT,
    "url" TEXT,
    "photoUrl" TEXT,
    "offices" JSONB,
    "cachedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "campaign_emails" (
    "id" TEXT NOT NULL,
    "campaignId" TEXT NOT NULL,
    "campaignSlug" TEXT NOT NULL,
    "userId" TEXT,
    "userEmail" TEXT,
    "userName" TEXT,
    "userPostalCode" TEXT,
    "recipientEmail" TEXT NOT NULL,
    "recipientName" TEXT,
    "recipientTitle" TEXT,
    "recipientLevel" "GovernmentLevel",
    "emailMethod" "EmailMethod" NOT NULL,
    "subject" TEXT NOT NULL,
    "message" TEXT NOT NULL,
    "status" "CampaignEmailStatus" NOT NULL DEFAULT 'SENT',
    "senderIp" TEXT,
    "sentAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "representative_responses" (
    "id" TEXT NOT NULL,
    "campaignId" TEXT NOT NULL,
    "campaignSlug" TEXT NOT NULL,
    "representativeName" TEXT NOT NULL,
    "representativeTitle" TEXT,
    "representativeLevel" "GovernmentLevel" NOT NULL,
    "representativeEmail" TEXT,
    "responseType" "ResponseType" NOT NULL,
    "responseText" TEXT NOT NULL,
    "userComment" TEXT,
    "screenshotUrl" TEXT,
    "submittedByUserId" TEXT,
    "submittedByName" TEXT,
    "submittedByEmail" TEXT,
    "isAnonymous" BOOLEAN NOT NULL DEFAULT false,
    "status" "ResponseStatus" NOT NULL DEFAULT 'PENDING',
    "isVerified" BOOLEAN NOT NULL DEFAULT false,
    "verificationToken" TEXT,
    "verificationSentAt" TIMESTAMP(3),
    "verifiedAt" TIMESTAMP(3),
    "verifiedBy" TEXT,
    "upvoteCount" INTEGER NOT NULL DEFAULT 0,
    "submittedIp" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "response_upvotes" (
    "id" TEXT NOT NULL,
    "responseId" TEXT NOT NULL,
    "userId" TEXT,
    "userEmail" TEXT,
    "upvotedIp" TEXT,

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

-- CreateTable
CREATE TABLE "custom_recipients" (
    "id" TEXT NOT NULL,
    "campaignId" TEXT NOT NULL,
    "campaignSlug" TEXT NOT NULL,
    "recipientName" TEXT NOT NULL,
    "recipientEmail" TEXT NOT NULL,
    "recipientTitle" TEXT,
    "recipientOrganization" TEXT,
    "notes" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "postal_code_cache" (
    "id" TEXT NOT NULL,
    "postalCode" TEXT NOT NULL,
    "city" TEXT,
    "province" TEXT,
    "centroidLat" DECIMAL(10,8),
    "centroidLng" DECIMAL(11,8),
    "lastUpdated" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "email_logs" (
    "id" TEXT NOT NULL,
    "recipientEmail" TEXT NOT NULL,
    "senderName" TEXT NOT NULL,
    "senderEmail" TEXT NOT NULL,
    "subject" TEXT,
    "message" TEXT,
    "postalCode" TEXT,
    "status" TEXT NOT NULL DEFAULT 'sent',
    "senderIp" TEXT,
    "sentAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "email_verifications" (
    "id" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "tempCampaignData" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expiresAt" TIMESTAMP(3) NOT NULL,
    "used" BOOLEAN NOT NULL DEFAULT false,

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

-- CreateTable
CREATE TABLE "calls" (
    "id" TEXT NOT NULL,
    "representativeName" TEXT NOT NULL,
    "representativeTitle" TEXT,
    "phoneNumber" TEXT NOT NULL,
    "officeType" TEXT,
    "callerName" TEXT,
    "callerEmail" TEXT,
    "postalCode" TEXT,
    "campaignId" TEXT,
    "campaignSlug" TEXT,
    "callerIp" TEXT,
    "calledAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "locations" (
    "id" TEXT NOT NULL,
    "latitude" DECIMAL(10,8) NOT NULL,
    "longitude" DECIMAL(11,8) NOT NULL,
    "address" TEXT NOT NULL,
    "postalCode" TEXT,
    "province" TEXT,
    "federalDistrict" TEXT,
    "buildingUse" INTEGER,
    "locGuid" TEXT,
    "buildingType" "BuildingType" NOT NULL DEFAULT 'SINGLE_FAMILY',
    "totalUnits" INTEGER NOT NULL DEFAULT 1,
    "buildingNotes" TEXT,
    "geocodeConfidence" INTEGER,
    "geocodeProvider" "GeocodeProvider",
    "createdByUserId" TEXT,
    "updatedByUserId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- 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 "shifts" (
    "id" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "date" DATE NOT NULL,
    "startTime" TEXT NOT NULL,
    "endTime" TEXT NOT NULL,
    "location" TEXT,
    "maxVolunteers" INTEGER NOT NULL,
    "currentVolunteers" INTEGER NOT NULL DEFAULT 0,
    "status" "ShiftStatus" NOT NULL DEFAULT 'OPEN',
    "kind" "ShiftKind" NOT NULL DEFAULT 'CANVASS',
    "isPublic" BOOLEAN NOT NULL DEFAULT false,
    "cutId" TEXT,
    "ticketed_event_id" TEXT,
    "seriesId" TEXT,
    "isException" BOOLEAN NOT NULL DEFAULT false,
    "gancioEventId" INTEGER,
    "meetingId" TEXT,
    "createdBy" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "shift_signups" (
    "id" TEXT NOT NULL,
    "shiftId" TEXT NOT NULL,
    "shiftTitle" TEXT,
    "userId" TEXT,
    "userEmail" TEXT NOT NULL,
    "userName" TEXT,
    "userPhone" TEXT,
    "signupDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "status" "SignupStatus" NOT NULL DEFAULT 'CONFIRMED',
    "signupSource" "SignupSource" NOT NULL DEFAULT 'AUTHENTICATED',

    CONSTRAINT "shift_signups_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 "cuts" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "color" TEXT NOT NULL DEFAULT '#3388ff',
    "opacity" DECIMAL(3,2) NOT NULL DEFAULT 0.3,
    "category" "CutCategory",
    "isPublic" BOOLEAN NOT NULL DEFAULT false,
    "isOfficial" BOOLEAN NOT NULL DEFAULT false,
    "geojson" TEXT NOT NULL,
    "bounds" TEXT,
    "showLocations" BOOLEAN NOT NULL DEFAULT true,
    "exportEnabled" BOOLEAN NOT NULL DEFAULT true,
    "assignedTo" TEXT,
    "filterSettings" JSONB,
    "lastCanvassed" TIMESTAMP(3),
    "completionPercentage" INTEGER NOT NULL DEFAULT 0,
    "createdByUserId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "map_settings" (
    "id" TEXT NOT NULL,
    "latitude" DECIMAL(10,8),
    "longitude" DECIMAL(11,8),
    "zoom" INTEGER,
    "walkSheetTitle" TEXT,
    "walkSheetSubtitle" TEXT,
    "walkSheetFooter" TEXT,
    "qrCode1Url" TEXT,
    "qrCode1Label" TEXT,
    "qrCode2Url" TEXT,
    "qrCode2Label" TEXT,
    "qrCode3Url" TEXT,
    "qrCode3Label" TEXT,
    "publicMapEnabled" BOOLEAN NOT NULL DEFAULT false,
    "publicShowLocations" BOOLEAN NOT NULL DEFAULT true,
    "publicShowSupportLevels" BOOLEAN NOT NULL DEFAULT true,
    "publicShowCuts" BOOLEAN NOT NULL DEFAULT true,
    "publicShowEvents" BOOLEAN NOT NULL DEFAULT true,
    "publicShowAddresses" BOOLEAN NOT NULL DEFAULT true,
    "publicShowSignInfo" BOOLEAN NOT NULL DEFAULT true,
    "createdBy" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "site_settings" (
    "id" TEXT NOT NULL,
    "organizationName" TEXT NOT NULL DEFAULT 'Changemaker Lite',
    "organizationShortName" TEXT NOT NULL DEFAULT 'CML',
    "organizationLogoUrl" TEXT,
    "organizationFaviconUrl" TEXT,
    "adminColorPrimary" TEXT NOT NULL DEFAULT '#9d4edd',
    "adminColorBgBase" TEXT NOT NULL DEFAULT '#1a1025',
    "publicColorPrimary" TEXT NOT NULL DEFAULT '#3498db',
    "publicColorBgBase" TEXT NOT NULL DEFAULT '#0d1b2a',
    "publicColorBgContainer" TEXT NOT NULL DEFAULT '#1b2838',
    "publicHeaderGradient" TEXT NOT NULL DEFAULT 'linear-gradient(135deg, #005a9c 0%, #007acc 100%)',
    "footerText" TEXT NOT NULL DEFAULT 'Powered by Changemaker Lite',
    "loginSubtitle" TEXT NOT NULL DEFAULT 'Admin',
    "homepage_tagline" TEXT,
    "emailFromName" TEXT NOT NULL DEFAULT 'Changemaker Lite',
    "smtpHost" TEXT NOT NULL DEFAULT '',
    "smtpPort" INTEGER NOT NULL DEFAULT 0,
    "smtpUser" TEXT NOT NULL DEFAULT '',
    "smtpPass" TEXT NOT NULL DEFAULT '',
    "smtpFromAddress" TEXT NOT NULL DEFAULT '',
    "smtpActiveProvider" TEXT NOT NULL DEFAULT 'mailhog',
    "emailTestMode" BOOLEAN NOT NULL DEFAULT true,
    "testEmailRecipient" TEXT NOT NULL DEFAULT '',
    "enablePublicRegistration" BOOLEAN NOT NULL DEFAULT true,
    "enableEmailVerification" BOOLEAN NOT NULL DEFAULT true,
    "autoApproveVerifiedUsers" BOOLEAN NOT NULL DEFAULT true,
    "enableInfluence" BOOLEAN NOT NULL DEFAULT true,
    "enableMap" BOOLEAN NOT NULL DEFAULT false,
    "enableNewsletter" BOOLEAN NOT NULL DEFAULT true,
    "enableLandingPages" BOOLEAN NOT NULL DEFAULT true,
    "enable_media_features" BOOLEAN NOT NULL DEFAULT true,
    "enablePayments" BOOLEAN NOT NULL DEFAULT false,
    "enable_gallery_ads" BOOLEAN NOT NULL DEFAULT false,
    "enable_chat" BOOLEAN NOT NULL DEFAULT false,
    "enable_events" BOOLEAN NOT NULL DEFAULT false,
    "enable_docs_comments" BOOLEAN NOT NULL DEFAULT false,
    "enable_sms" BOOLEAN NOT NULL DEFAULT false,
    "enable_people" BOOLEAN NOT NULL DEFAULT false,
    "enable_social" BOOLEAN NOT NULL DEFAULT false,
    "enable_meet" BOOLEAN NOT NULL DEFAULT false,
    "enable_meeting_planner" BOOLEAN NOT NULL DEFAULT false,
    "enable_ticketed_events" BOOLEAN NOT NULL DEFAULT false,
    "enable_social_calendar" BOOLEAN NOT NULL DEFAULT false,
    "enable_polls" BOOLEAN NOT NULL DEFAULT false,
    "enable_analytics" BOOLEAN NOT NULL DEFAULT false,
    "analytics_retention_days" INTEGER NOT NULL DEFAULT 90,
    "analytics_geo_enabled" BOOLEAN NOT NULL DEFAULT true,
    "track_authenticated_users" BOOLEAN NOT NULL DEFAULT true,
    "enable_petitions" BOOLEAN NOT NULL DEFAULT false,
    "enable_docs_collaboration" BOOLEAN NOT NULL DEFAULT false,
    "require_event_approval" BOOLEAN NOT NULL DEFAULT true,
    "auto_sync_people_to_map" BOOLEAN NOT NULL DEFAULT false,
    "sms_termux_api_url" TEXT NOT NULL DEFAULT '',
    "sms_termux_api_key" TEXT NOT NULL DEFAULT '',
    "sms_tailscale_api_key" TEXT NOT NULL DEFAULT '',
    "sms_tailscale_tailnet" TEXT NOT NULL DEFAULT '',
    "sms_tailscale_device_id" TEXT NOT NULL DEFAULT '',
    "sms_tailscale_device_name" TEXT NOT NULL DEFAULT '',
    "giteaApiToken" TEXT NOT NULL DEFAULT '',
    "giteaCommentsRepoOwner" TEXT NOT NULL DEFAULT '',
    "giteaCommentsRepoName" TEXT NOT NULL DEFAULT 'docs-comments',
    "giteaOauthClientId" TEXT NOT NULL DEFAULT '',
    "giteaOauthClientSecret" TEXT NOT NULL DEFAULT '',
    "gitea_setup_complete" BOOLEAN NOT NULL DEFAULT false,
    "notifyAdminShiftSignup" BOOLEAN NOT NULL DEFAULT true,
    "notifyAdminResponseSubmitted" BOOLEAN NOT NULL DEFAULT true,
    "notifyAdminSignRequested" BOOLEAN NOT NULL DEFAULT true,
    "notify_admin_petition_milestone" BOOLEAN NOT NULL DEFAULT false,
    "notifyAdminShiftCancellation" BOOLEAN NOT NULL DEFAULT true,
    "notifyVolunteerSessionSummary" BOOLEAN NOT NULL DEFAULT true,
    "notifyVolunteerCancellation" BOOLEAN NOT NULL DEFAULT true,
    "notifyVolunteerShiftReminder" BOOLEAN NOT NULL DEFAULT true,
    "notifyVolunteerShiftThankYou" BOOLEAN NOT NULL DEFAULT true,
    "sms_shift_reminders" BOOLEAN NOT NULL DEFAULT false,
    "sms_shift_reminder_hours" INTEGER NOT NULL DEFAULT 24,
    "sms_shift_signup_confirm" BOOLEAN NOT NULL DEFAULT false,
    "sms_volunteer_welcome" BOOLEAN NOT NULL DEFAULT false,
    "notify_volunteer_reengagement" BOOLEAN NOT NULL DEFAULT false,
    "reengagement_inactive_days" INTEGER NOT NULL DEFAULT 30,
    "reengagement_cooldown_days" INTEGER NOT NULL DEFAULT 30,
    "enable_auto_upgrade" BOOLEAN NOT NULL DEFAULT false,
    "auto_upgrade_schedule" TEXT NOT NULL DEFAULT 'daily-3am',
    "auto_upgrade_pull_services" BOOLEAN NOT NULL DEFAULT false,
    "notify_admin_auto_upgrade" BOOLEAN NOT NULL DEFAULT true,
    "use_registry_for_upgrade" BOOLEAN NOT NULL DEFAULT false,
    "gitea_registry_url" TEXT NOT NULL DEFAULT 'gitea.bnkops.com/admin',
    "nav_config" JSONB,
    "enable_user_provisioning" BOOLEAN NOT NULL DEFAULT false,
    "provision_gitea" BOOLEAN NOT NULL DEFAULT false,
    "provision_gitea_timing" TEXT NOT NULL DEFAULT 'lazy',
    "provision_vaultwarden" BOOLEAN NOT NULL DEFAULT false,
    "provision_vaultwarden_timing" TEXT NOT NULL DEFAULT 'lazy',
    "provision_listmonk" BOOLEAN NOT NULL DEFAULT true,
    "provision_listmonk_timing" TEXT NOT NULL DEFAULT 'eager',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "site_settings_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 "landing_pages" (
    "id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "blocks" JSONB NOT NULL,
    "htmlOutput" TEXT,
    "cssOutput" TEXT,
    "editorMode" "EditorMode" NOT NULL DEFAULT 'VISUAL',
    "mkdocsPath" TEXT,
    "mkdocsStubPath" TEXT,
    "mkdocsExportMode" "MkdocsExportMode" NOT NULL DEFAULT 'THEMED',
    "mkdocsHideNav" BOOLEAN NOT NULL DEFAULT true,
    "mkdocsHideToc" BOOLEAN NOT NULL DEFAULT true,
    "mkdocsSkipExport" BOOLEAN NOT NULL DEFAULT false,
    "published" BOOLEAN NOT NULL DEFAULT false,
    "listed" BOOLEAN NOT NULL DEFAULT false,
    "seoTitle" TEXT,
    "seoDescription" TEXT,
    "seoImage" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "page_blocks" (
    "id" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "label" TEXT NOT NULL,
    "schema" JSONB NOT NULL,
    "defaults" JSONB NOT NULL,
    "thumbnail" TEXT,
    "category" TEXT,
    "sortOrder" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "canvass_sessions" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "cutId" TEXT NOT NULL,
    "shiftId" TEXT,
    "status" "CanvassSessionStatus" NOT NULL DEFAULT 'ACTIVE',
    "startedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "endedAt" TIMESTAMP(3),
    "startLatitude" DECIMAL(10,8),
    "startLongitude" DECIMAL(11,8),

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

-- CreateTable
CREATE TABLE "canvass_visits" (
    "id" TEXT NOT NULL,
    "addressId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "shiftId" TEXT,
    "sessionId" TEXT,
    "outcome" "VisitOutcome" NOT NULL,
    "supportLevel" "SupportLevel",
    "signRequested" BOOLEAN NOT NULL DEFAULT false,
    "signSize" TEXT,
    "notes" TEXT,
    "durationSeconds" INTEGER,
    "visitedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "tracking_sessions" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "canvassSessionId" TEXT,
    "startedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "endedAt" TIMESTAMP(3),
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "totalPoints" INTEGER NOT NULL DEFAULT 0,
    "totalDistanceM" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "lastLatitude" DECIMAL(10,8),
    "lastLongitude" DECIMAL(11,8),
    "lastRecordedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "track_points" (
    "id" TEXT NOT NULL,
    "trackingSessionId" TEXT NOT NULL,
    "latitude" DECIMAL(10,8) NOT NULL,
    "longitude" DECIMAL(11,8) NOT NULL,
    "accuracy" DOUBLE PRECISION,
    "recordedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "eventType" "TrackPointEvent",

    CONSTRAINT "track_points_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),
    "placements" JSONB DEFAULT '[]',
    "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,
    "show_on_leaderboard" BOOLEAN DEFAULT true,
    "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 "social_groups" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type" "SocialGroupType" NOT NULL,
    "reference_id" TEXT,
    "meeting_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "social_group_members" (
    "id" TEXT NOT NULL,
    "group_id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "joined_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "social_group_members_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,
    "order_id" TEXT,
    "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,
    "influence_campaign_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,
    "digest_frequency" TEXT NOT NULL DEFAULT 'none',
    "last_digest_sent_at" TIMESTAMP(3),
    "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,
    "country" VARCHAR(2),
    "region" VARCHAR(100),
    "city" VARCHAR(100),
    "latitude" DOUBLE PRECISION,
    "longitude" DOUBLE PRECISION,
    "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,
    "ip_address_hash" VARCHAR(64),
    "country" VARCHAR(2),
    "region" VARCHAR(100),
    "city" VARCHAR(100),
    "latitude" DOUBLE PRECISION,
    "longitude" DOUBLE PRECISION,
    "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,
    "country" VARCHAR(2),
    "region" VARCHAR(100),
    "city" VARCHAR(100),
    "latitude" DOUBLE PRECISION,
    "longitude" DOUBLE PRECISION,
    "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,
    "contactId" 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,
    "pronouns" 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")
);

-- CreateTable
CREATE TABLE "meetings" (
    "id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "jitsi_room" TEXT NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_by_user_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "start_time" TIMESTAMP(3),
    "end_time" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "scheduling_polls" (
    "id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "location" TEXT,
    "status" "SchedulingPollStatus" NOT NULL DEFAULT 'OPEN',
    "timezone" TEXT NOT NULL DEFAULT 'America/Edmonton',
    "finalized_option_id" TEXT,
    "converted_shift_id" TEXT,
    "converted_gancio_event_id" INTEGER,
    "converted_calendar_item_id" TEXT,
    "voting_deadline" TIMESTAMP(3),
    "auto_finalize" BOOLEAN NOT NULL DEFAULT false,
    "auto_finalize_threshold" INTEGER,
    "auto_convert_to_calendar" BOOLEAN NOT NULL DEFAULT false,
    "auto_convert_to_gancio" BOOLEAN NOT NULL DEFAULT false,
    "auto_convert_to_shift" BOOLEAN NOT NULL DEFAULT false,
    "tie_breaker" TEXT NOT NULL DEFAULT 'earliest',
    "auto_enroll_voters" BOOLEAN NOT NULL DEFAULT true,
    "auto_finalize_job_id" TEXT,
    "allow_anonymous" BOOLEAN NOT NULL DEFAULT true,
    "is_private" BOOLEAN NOT NULL DEFAULT false,
    "notify_on_vote" BOOLEAN NOT NULL DEFAULT true,
    "created_by_user_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "scheduling_poll_options" (
    "id" TEXT NOT NULL,
    "poll_id" TEXT NOT NULL,
    "date" DATE NOT NULL,
    "start_time" TEXT NOT NULL,
    "end_time" TEXT NOT NULL,
    "sort_order" INTEGER NOT NULL DEFAULT 0,

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

-- CreateTable
CREATE TABLE "scheduling_poll_votes" (
    "id" TEXT NOT NULL,
    "poll_id" TEXT NOT NULL,
    "option_id" TEXT NOT NULL,
    "user_id" TEXT,
    "voter_name" TEXT NOT NULL,
    "voter_email" TEXT,
    "voter_token" TEXT,
    "contact_id" TEXT,
    "value" "PollVoteValue" NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "scheduling_poll_comments" (
    "id" TEXT NOT NULL,
    "poll_id" TEXT NOT NULL,
    "user_id" TEXT,
    "author_name" TEXT NOT NULL,
    "content" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "invite_codes" (
    "id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "created_by_user_id" TEXT NOT NULL,
    "max_uses" INTEGER NOT NULL DEFAULT 0,
    "used_count" INTEGER NOT NULL DEFAULT 0,
    "expires_at" TIMESTAMP(3),
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "note" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "referrals" (
    "id" SERIAL NOT NULL,
    "referrer_id" TEXT NOT NULL,
    "referred_user_id" TEXT NOT NULL,
    "invite_code_id" TEXT,
    "referral_source" TEXT,
    "completed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "impact_stories" (
    "id" TEXT NOT NULL,
    "campaign_id" TEXT NOT NULL,
    "type" "ImpactStoryType" NOT NULL,
    "status" "ImpactStoryStatus" NOT NULL DEFAULT 'DRAFT',
    "title" TEXT NOT NULL,
    "body" TEXT NOT NULL,
    "cover_image_url" TEXT,
    "milestone_value" INTEGER,
    "milestone_metric" TEXT,
    "created_by_user_id" TEXT,
    "published_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "campaign_milestones" (
    "id" SERIAL NOT NULL,
    "campaign_id" TEXT NOT NULL,
    "metric" TEXT NOT NULL,
    "threshold" INTEGER NOT NULL,
    "reached_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "story_generated" BOOLEAN NOT NULL DEFAULT false,

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

-- CreateTable
CREATE TABLE "volunteer_spotlights" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "status" "SpotlightStatus" NOT NULL DEFAULT 'NOMINATED',
    "headline" TEXT,
    "story" TEXT,
    "featured_month" TEXT,
    "nominated_by_user_id" TEXT,
    "approved_by_user_id" TEXT,
    "approved_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "challenges" (
    "id" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "metric" "ChallengeMetric" NOT NULL,
    "status" "ChallengeStatus" NOT NULL DEFAULT 'DRAFT',
    "starts_at" TIMESTAMP(3) NOT NULL,
    "ends_at" TIMESTAMP(3) NOT NULL,
    "min_team_size" INTEGER NOT NULL DEFAULT 2,
    "max_team_size" INTEGER NOT NULL DEFAULT 10,
    "max_teams" INTEGER,
    "created_by_user_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "challenge_teams" (
    "id" TEXT NOT NULL,
    "challenge_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "captain_user_id" TEXT NOT NULL,
    "score" INTEGER NOT NULL DEFAULT 0,
    "last_scored_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "challenge_team_members" (
    "id" SERIAL NOT NULL,
    "team_id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "score" INTEGER NOT NULL DEFAULT 0,
    "joined_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "ticketed_events" (
    "id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "rich_description" TEXT,
    "date" DATE NOT NULL,
    "start_time" TEXT NOT NULL,
    "end_time" TEXT NOT NULL,
    "doors_open_time" TEXT,
    "venue_name" TEXT,
    "venue_address" TEXT,
    "latitude" DECIMAL(10,7),
    "longitude" DECIMAL(10,7),
    "status" "TicketedEventStatus" NOT NULL DEFAULT 'DRAFT',
    "visibility" "TicketedEventVisibility" NOT NULL DEFAULT 'PUBLIC',
    "invite_code" TEXT,
    "featured" BOOLEAN NOT NULL DEFAULT false,
    "cover_image_url" TEXT,
    "max_attendees" INTEGER,
    "current_attendees" INTEGER NOT NULL DEFAULT 0,
    "gancio_event_id" INTEGER,
    "event_format" "EventFormat" NOT NULL DEFAULT 'IN_PERSON',
    "meeting_id" TEXT,
    "created_by_user_id" TEXT NOT NULL,
    "organizer_name" TEXT,
    "organizer_email" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ticket_tiers" (
    "id" TEXT NOT NULL,
    "event_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "tier_type" "TicketTierType" NOT NULL,
    "price_cad" INTEGER NOT NULL DEFAULT 0,
    "min_donation_cad" INTEGER,
    "max_quantity" INTEGER,
    "sold_count" INTEGER NOT NULL DEFAULT 0,
    "reserved_count" INTEGER NOT NULL DEFAULT 0,
    "max_per_order" INTEGER NOT NULL DEFAULT 10,
    "sales_start_at" TIMESTAMP(3),
    "sales_end_at" TIMESTAMP(3),
    "sort_order" INTEGER NOT NULL DEFAULT 0,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "tickets" (
    "id" TEXT NOT NULL,
    "ticket_code" TEXT NOT NULL,
    "token_hash" TEXT NOT NULL,
    "event_id" TEXT NOT NULL,
    "tier_id" TEXT NOT NULL,
    "order_id" TEXT,
    "holder_email" TEXT NOT NULL,
    "holder_name" TEXT,
    "user_id" TEXT,
    "status" "TicketStatus" NOT NULL DEFAULT 'VALID',
    "checked_in_at" TIMESTAMP(3),
    "checked_in_by_user_id" TEXT,
    "issued_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "check_ins" (
    "id" TEXT NOT NULL,
    "ticket_id" TEXT NOT NULL,
    "event_id" TEXT NOT NULL,
    "checked_in_by_user_id" TEXT,
    "method" TEXT NOT NULL,
    "checked_in_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "notes" TEXT,

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

-- CreateTable
CREATE TABLE "calendar_layers" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "layer_type" "CalendarLayerType" NOT NULL,
    "system_type" "CalendarSystemType",
    "color" TEXT NOT NULL DEFAULT '#1890ff',
    "visibility" "CalendarVisibility" NOT NULL DEFAULT 'PRIVATE',
    "is_enabled" BOOLEAN NOT NULL DEFAULT true,
    "sort_order" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "calendar_items" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "layer_id" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "date" DATE NOT NULL,
    "start_time" TEXT NOT NULL,
    "end_time" TEXT NOT NULL,
    "is_all_day" BOOLEAN NOT NULL DEFAULT false,
    "item_type" "CalendarItemType" NOT NULL DEFAULT 'EVENT',
    "location" TEXT,
    "color" TEXT,
    "visibility" "CalendarVisibility",
    "busy_status" "CalendarBusyStatus" NOT NULL DEFAULT 'BUSY',
    "show_details_to" "CalendarShowDetailsTo" NOT NULL DEFAULT 'FRIENDS',
    "recurrence_rule" JSONB,
    "recurrence_end" TIMESTAMP(3),
    "series_id" TEXT,
    "is_exception" BOOLEAN NOT NULL DEFAULT false,
    "source_type" "CalendarItemSource" NOT NULL DEFAULT 'MANUAL',
    "source_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "calendar_feeds" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "url" TEXT NOT NULL,
    "layer_id" TEXT NOT NULL,
    "refresh_interval" "CalendarFeedInterval" NOT NULL DEFAULT 'HOURLY',
    "last_fetched_at" TIMESTAMP(3),
    "last_status" "CalendarFeedStatus" NOT NULL DEFAULT 'PENDING',
    "last_error" TEXT,
    "item_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "shared_calendar_views" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "owner_id" TEXT NOT NULL,
    "view_type" "SharedViewType" NOT NULL DEFAULT 'MANUAL',
    "auto_include_roles" JSONB,
    "included_layer_types" JSONB NOT NULL DEFAULT '[]',
    "share_scope" "SharedViewScope" NOT NULL DEFAULT 'MEMBERS',
    "share_token" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "shared_calendar_members" (
    "id" TEXT NOT NULL,
    "view_id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "status" "SharedViewMemberStatus" NOT NULL DEFAULT 'INVITED',
    "color" TEXT NOT NULL DEFAULT '#1890ff',
    "joined_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "shared_view_comments" (
    "id" TEXT NOT NULL,
    "view_id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "item_date" TEXT NOT NULL,
    "item_id" TEXT,
    "content" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "shared_view_reactions" (
    "id" TEXT NOT NULL,
    "view_id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "item_id" TEXT NOT NULL,
    "emoji" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "calendar_export_tokens" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "include_personal" BOOLEAN NOT NULL DEFAULT false,
    "include_layers" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "doc_collab_state" (
    "id" TEXT NOT NULL,
    "document_id" TEXT NOT NULL,
    "state" BYTEA NOT NULL,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "doc_access_policies" (
    "id" TEXT NOT NULL,
    "document_path" TEXT NOT NULL,
    "is_directory" BOOLEAN NOT NULL DEFAULT false,
    "allowed_editors" JSONB NOT NULL DEFAULT '[]',
    "created_by_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "doc_share_links" (
    "id" TEXT NOT NULL,
    "document_path" TEXT NOT NULL,
    "share_token" TEXT NOT NULL,
    "status" "DocShareLinkStatus" NOT NULL DEFAULT 'ACTIVE',
    "can_edit" BOOLEAN NOT NULL DEFAULT true,
    "expires_at" TIMESTAMP(3),
    "max_uses" INTEGER,
    "use_count" INTEGER NOT NULL DEFAULT 0,
    "guest_name" TEXT,
    "created_by_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "doc_watches" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "file_path" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "participant_needs" (
    "id" TEXT NOT NULL,
    "user_id" TEXT,
    "contact_id" TEXT,
    "needs_wheelchair" BOOLEAN NOT NULL DEFAULT false,
    "needs_ground_floor" BOOLEAN NOT NULL DEFAULT false,
    "needs_hearing_loop" BOOLEAN NOT NULL DEFAULT false,
    "needs_sign_language" BOOLEAN NOT NULL DEFAULT false,
    "other_accessibility" TEXT,
    "is_vegan" BOOLEAN NOT NULL DEFAULT false,
    "is_vegetarian" BOOLEAN NOT NULL DEFAULT false,
    "is_gluten_free" BOOLEAN NOT NULL DEFAULT false,
    "is_halal" BOOLEAN NOT NULL DEFAULT false,
    "is_kosher" BOOLEAN NOT NULL DEFAULT false,
    "has_nut_allergy" BOOLEAN NOT NULL DEFAULT false,
    "other_dietary" TEXT,
    "needs_childcare" BOOLEAN NOT NULL DEFAULT false,
    "childcare_details" TEXT,
    "needs_transportation" BOOLEAN NOT NULL DEFAULT false,
    "transportation_notes" TEXT,
    "preferred_language" TEXT DEFAULT 'en',
    "needs_translation" BOOLEAN NOT NULL DEFAULT false,
    "translation_language" TEXT,
    "visibility_consent" TEXT NOT NULL DEFAULT 'organizer_only',
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "meeting_agendas" (
    "id" TEXT NOT NULL,
    "shift_id" TEXT,
    "poll_id" TEXT,
    "title" TEXT NOT NULL,
    "items" JSONB NOT NULL DEFAULT '[]',
    "status" TEXT NOT NULL DEFAULT 'draft',
    "created_by_user_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "meeting_minutes" (
    "id" TEXT NOT NULL,
    "agenda_id" TEXT NOT NULL,
    "notes" TEXT NOT NULL,
    "decisions" JSONB NOT NULL DEFAULT '[]',
    "attendees" JSONB NOT NULL DEFAULT '[]',
    "approved_at" TIMESTAMP(3),
    "approved_by_user_id" TEXT,
    "created_by_user_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "action_items" (
    "id" TEXT NOT NULL,
    "agenda_id" TEXT,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "assignee_user_id" TEXT,
    "due_date" TIMESTAMP(3),
    "status" TEXT NOT NULL DEFAULT 'open',
    "priority" TEXT NOT NULL DEFAULT 'normal',
    "completed_at" TIMESTAMP(3),
    "created_by_user_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "straw_polls" (
    "id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "title" VARCHAR(200) NOT NULL,
    "description" TEXT,
    "type" "StrawPollType" NOT NULL,
    "status" "StrawPollStatus" NOT NULL DEFAULT 'DRAFT',
    "identity_mode" "StrawPollIdentityMode" NOT NULL DEFAULT 'ANONYMOUS',
    "result_visibility" "StrawPollResultVisibility" NOT NULL DEFAULT 'LIVE',
    "allow_comments" BOOLEAN NOT NULL DEFAULT true,
    "closes_at" TIMESTAMP(3),
    "close_threshold" INTEGER,
    "auto_close_job_id" TEXT,
    "is_private" BOOLEAN NOT NULL DEFAULT false,
    "created_by_user_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "straw_poll_options" (
    "id" TEXT NOT NULL,
    "poll_id" TEXT NOT NULL,
    "label" VARCHAR(500) NOT NULL,
    "sort_order" INTEGER NOT NULL DEFAULT 0,

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

-- CreateTable
CREATE TABLE "straw_poll_votes" (
    "id" TEXT NOT NULL,
    "poll_id" TEXT NOT NULL,
    "option_id" TEXT NOT NULL,
    "user_id" TEXT,
    "voter_name" VARCHAR(100),
    "voter_token" TEXT,
    "voter_ip" TEXT,
    "contact_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "straw_poll_comments" (
    "id" TEXT NOT NULL,
    "poll_id" TEXT NOT NULL,
    "user_id" TEXT,
    "author_name" VARCHAR(100) NOT NULL,
    "content" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "straw_poll_challenges" (
    "id" TEXT NOT NULL,
    "poll_id" TEXT NOT NULL,
    "challenger_user_id" TEXT NOT NULL,
    "challenged_user_id" TEXT NOT NULL,
    "completed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "action_campaigns" (
    "id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "reward_text" TEXT,
    "is_active" BOOLEAN NOT NULL DEFAULT false,
    "starts_at" TIMESTAMP(3),
    "ends_at" TIMESTAMP(3),
    "min_steps_for_reward" INTEGER,
    "created_by_user_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "action_steps" (
    "id" TEXT NOT NULL,
    "campaign_id" TEXT NOT NULL,
    "order" INTEGER NOT NULL,
    "kind" "ActionStepKind" NOT NULL,
    "label" TEXT NOT NULL,
    "description" TEXT,
    "target_id" TEXT,
    "target_url" TEXT,
    "auto_complete" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "action_step_completions" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "step_id" TEXT NOT NULL,
    "source" "ActionStepCompletionSource" NOT NULL DEFAULT 'AUTO',
    "completed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "documents" (
    "id" TEXT NOT NULL,
    "path" TEXT NOT NULL,
    "filename" TEXT NOT NULL,
    "original_filename" TEXT,
    "title" TEXT,
    "description" TEXT,
    "mime_type" TEXT NOT NULL,
    "file_size" BIGINT,
    "page_count" INTEGER,
    "thumbnail_path" TEXT,
    "category" TEXT,
    "tags" JSONB,
    "is_published" BOOLEAN NOT NULL DEFAULT true,
    "position" INTEGER DEFAULT 0,
    "uploader_id" TEXT,
    "download_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "refresh_tokens_token_key" ON "refresh_tokens"("token");

-- CreateIndex
CREATE INDEX "refresh_tokens_userId_idx" ON "refresh_tokens"("userId");

-- CreateIndex
CREATE UNIQUE INDEX "campaigns_slug_key" ON "campaigns"("slug");

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

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

-- CreateIndex
CREATE UNIQUE INDEX "petitions_slug_key" ON "petitions"("slug");

-- CreateIndex
CREATE INDEX "petitions_status_idx" ON "petitions"("status");

-- CreateIndex
CREATE INDEX "petitions_isUserGenerated_idx" ON "petitions"("isUserGenerated");

-- CreateIndex
CREATE INDEX "petitions_highlightPetition_idx" ON "petitions"("highlightPetition");

-- CreateIndex
CREATE INDEX "petitions_linkedCampaignId_idx" ON "petitions"("linkedCampaignId");

-- CreateIndex
CREATE UNIQUE INDEX "petition_signatures_verificationToken_key" ON "petition_signatures"("verificationToken");

-- CreateIndex
CREATE INDEX "petition_signatures_petitionId_idx" ON "petition_signatures"("petitionId");

-- CreateIndex
CREATE INDEX "petition_signatures_signerEmail_idx" ON "petition_signatures"("signerEmail");

-- CreateIndex
CREATE INDEX "petition_signatures_petitionId_status_idx" ON "petition_signatures"("petitionId", "status");

-- CreateIndex
CREATE INDEX "petition_signatures_contactId_idx" ON "petition_signatures"("contactId");

-- CreateIndex
CREATE UNIQUE INDEX "petition_signatures_petitionId_signerEmail_key" ON "petition_signatures"("petitionId", "signerEmail");

-- CreateIndex
CREATE INDEX "representatives_postalCode_idx" ON "representatives"("postalCode");

-- CreateIndex
CREATE INDEX "campaign_emails_campaignId_idx" ON "campaign_emails"("campaignId");

-- CreateIndex
CREATE INDEX "campaign_emails_campaignSlug_idx" ON "campaign_emails"("campaignSlug");

-- 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 "representative_responses_campaignId_idx" ON "representative_responses"("campaignId");

-- CreateIndex
CREATE INDEX "representative_responses_campaignSlug_idx" ON "representative_responses"("campaignSlug");

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

-- CreateIndex
CREATE UNIQUE INDEX "response_upvotes_responseId_userId_key" ON "response_upvotes"("responseId", "userId");

-- CreateIndex
CREATE UNIQUE INDEX "response_upvotes_responseId_upvotedIp_key" ON "response_upvotes"("responseId", "upvotedIp");

-- CreateIndex
CREATE INDEX "custom_recipients_campaignId_idx" ON "custom_recipients"("campaignId");

-- CreateIndex
CREATE UNIQUE INDEX "postal_code_cache_postalCode_key" ON "postal_code_cache"("postalCode");

-- CreateIndex
CREATE UNIQUE INDEX "email_verifications_token_key" ON "email_verifications"("token");

-- CreateIndex
CREATE INDEX "calls_campaignId_idx" ON "calls"("campaignId");

-- 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 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 UNIQUE INDEX "shifts_meetingId_key" ON "shifts"("meetingId");

-- CreateIndex
CREATE INDEX "shifts_cutId_idx" ON "shifts"("cutId");

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

-- CreateIndex
CREATE INDEX "shift_signups_shiftId_idx" ON "shift_signups"("shiftId");

-- CreateIndex
CREATE UNIQUE INDEX "shift_signups_shiftId_userEmail_key" ON "shift_signups"("shiftId", "userEmail");

-- 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 "landing_pages_slug_key" ON "landing_pages"("slug");

-- CreateIndex
CREATE INDEX "canvass_sessions_userId_idx" ON "canvass_sessions"("userId");

-- CreateIndex
CREATE INDEX "canvass_sessions_cutId_idx" ON "canvass_sessions"("cutId");

-- CreateIndex
CREATE INDEX "canvass_sessions_shiftId_idx" ON "canvass_sessions"("shiftId");

-- 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 INDEX "canvass_visits_userId_idx" ON "canvass_visits"("userId");

-- CreateIndex
CREATE INDEX "canvass_visits_shiftId_idx" ON "canvass_visits"("shiftId");

-- CreateIndex
CREATE INDEX "canvass_visits_sessionId_idx" ON "canvass_visits"("sessionId");

-- CreateIndex
CREATE INDEX "canvass_visits_visitedAt_idx" ON "canvass_visits"("visitedAt");

-- CreateIndex
CREATE UNIQUE INDEX "tracking_sessions_canvassSessionId_key" ON "tracking_sessions"("canvassSessionId");

-- CreateIndex
CREATE INDEX "tracking_sessions_userId_idx" ON "tracking_sessions"("userId");

-- CreateIndex
CREATE INDEX "tracking_sessions_isActive_idx" ON "tracking_sessions"("isActive");

-- CreateIndex
CREATE INDEX "tracking_sessions_isActive_lastRecordedAt_idx" ON "tracking_sessions"("isActive", "lastRecordedAt");

-- CreateIndex
CREATE INDEX "track_points_trackingSessionId_recordedAt_idx" ON "track_points"("trackingSessionId", "recordedAt");

-- CreateIndex
CREATE INDEX "track_points_recordedAt_idx" ON "track_points"("recordedAt");

-- 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 UNIQUE INDEX "social_groups_meeting_id_key" ON "social_groups"("meeting_id");

-- CreateIndex
CREATE UNIQUE INDEX "idx_social_groups_type_ref" ON "social_groups"("type", "reference_id");

-- CreateIndex
CREATE INDEX "idx_social_group_members_user" ON "social_group_members"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "idx_social_group_members_unique" ON "social_group_members"("group_id", "user_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_order" ON "payment_audit_log"("order_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 INDEX "idx_orders_influence_campaign" ON "orders"("influence_campaign_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_views_country_created" ON "video_views"("country", "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 INDEX "docs_page_views_country_createdAt_idx" ON "docs_page_views"("country", "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_views_country_date" ON "photo_views"("country", "viewed_at");

-- 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 INDEX "sms_conversations_contactId_idx" ON "sms_conversations"("contactId");

-- 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 UNIQUE INDEX "meetings_slug_key" ON "meetings"("slug");

-- CreateIndex
CREATE UNIQUE INDEX "meetings_jitsi_room_key" ON "meetings"("jitsi_room");

-- CreateIndex
CREATE UNIQUE INDEX "scheduling_polls_slug_key" ON "scheduling_polls"("slug");

-- CreateIndex
CREATE UNIQUE INDEX "scheduling_polls_finalized_option_id_key" ON "scheduling_polls"("finalized_option_id");

-- CreateIndex
CREATE UNIQUE INDEX "scheduling_polls_converted_shift_id_key" ON "scheduling_polls"("converted_shift_id");

-- CreateIndex
CREATE INDEX "scheduling_polls_created_by_user_id_idx" ON "scheduling_polls"("created_by_user_id");

-- CreateIndex
CREATE INDEX "scheduling_polls_status_idx" ON "scheduling_polls"("status");

-- CreateIndex
CREATE INDEX "scheduling_poll_options_poll_id_idx" ON "scheduling_poll_options"("poll_id");

-- CreateIndex
CREATE INDEX "scheduling_poll_votes_poll_id_idx" ON "scheduling_poll_votes"("poll_id");

-- CreateIndex
CREATE INDEX "scheduling_poll_votes_contact_id_idx" ON "scheduling_poll_votes"("contact_id");

-- CreateIndex
CREATE UNIQUE INDEX "scheduling_poll_votes_option_id_user_id_key" ON "scheduling_poll_votes"("option_id", "user_id");

-- CreateIndex
CREATE UNIQUE INDEX "scheduling_poll_votes_option_id_voter_token_key" ON "scheduling_poll_votes"("option_id", "voter_token");

-- CreateIndex
CREATE INDEX "scheduling_poll_comments_poll_id_idx" ON "scheduling_poll_comments"("poll_id");

-- CreateIndex
CREATE UNIQUE INDEX "invite_codes_code_key" ON "invite_codes"("code");

-- CreateIndex
CREATE INDEX "idx_invite_codes_code" ON "invite_codes"("code");

-- CreateIndex
CREATE INDEX "idx_invite_codes_created_by" ON "invite_codes"("created_by_user_id");

-- CreateIndex
CREATE UNIQUE INDEX "referrals_referred_user_id_key" ON "referrals"("referred_user_id");

-- CreateIndex
CREATE INDEX "idx_referrals_referrer" ON "referrals"("referrer_id");

-- CreateIndex
CREATE INDEX "idx_impact_stories_campaign" ON "impact_stories"("campaign_id");

-- CreateIndex
CREATE INDEX "idx_impact_stories_status" ON "impact_stories"("status");

-- CreateIndex
CREATE INDEX "idx_impact_stories_type" ON "impact_stories"("type");

-- CreateIndex
CREATE UNIQUE INDEX "campaign_milestones_campaign_id_metric_threshold_key" ON "campaign_milestones"("campaign_id", "metric", "threshold");

-- CreateIndex
CREATE INDEX "idx_volunteer_spotlights_user" ON "volunteer_spotlights"("user_id");

-- CreateIndex
CREATE INDEX "idx_volunteer_spotlights_status" ON "volunteer_spotlights"("status");

-- CreateIndex
CREATE INDEX "idx_volunteer_spotlights_month" ON "volunteer_spotlights"("featured_month");

-- CreateIndex
CREATE INDEX "idx_challenges_status" ON "challenges"("status");

-- CreateIndex
CREATE INDEX "idx_challenges_starts_at" ON "challenges"("starts_at");

-- CreateIndex
CREATE INDEX "idx_challenge_teams_challenge" ON "challenge_teams"("challenge_id");

-- CreateIndex
CREATE INDEX "idx_challenge_teams_score" ON "challenge_teams"("score");

-- CreateIndex
CREATE UNIQUE INDEX "challenge_teams_challenge_id_name_key" ON "challenge_teams"("challenge_id", "name");

-- CreateIndex
CREATE INDEX "idx_challenge_team_members_user" ON "challenge_team_members"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "challenge_team_members_team_id_user_id_key" ON "challenge_team_members"("team_id", "user_id");

-- CreateIndex
CREATE UNIQUE INDEX "ticketed_events_slug_key" ON "ticketed_events"("slug");

-- CreateIndex
CREATE UNIQUE INDEX "ticketed_events_invite_code_key" ON "ticketed_events"("invite_code");

-- CreateIndex
CREATE UNIQUE INDEX "ticketed_events_meeting_id_key" ON "ticketed_events"("meeting_id");

-- CreateIndex
CREATE INDEX "idx_ticketed_events_status" ON "ticketed_events"("status");

-- CreateIndex
CREATE INDEX "idx_ticketed_events_date" ON "ticketed_events"("date");

-- CreateIndex
CREATE INDEX "idx_ticketed_events_visibility" ON "ticketed_events"("visibility");

-- CreateIndex
CREATE INDEX "idx_ticketed_events_creator" ON "ticketed_events"("created_by_user_id");

-- CreateIndex
CREATE INDEX "idx_ticket_tiers_event" ON "ticket_tiers"("event_id");

-- CreateIndex
CREATE UNIQUE INDEX "tickets_ticket_code_key" ON "tickets"("ticket_code");

-- CreateIndex
CREATE UNIQUE INDEX "tickets_token_hash_key" ON "tickets"("token_hash");

-- CreateIndex
CREATE INDEX "idx_tickets_event" ON "tickets"("event_id");

-- CreateIndex
CREATE INDEX "idx_tickets_tier" ON "tickets"("tier_id");

-- CreateIndex
CREATE INDEX "idx_tickets_order" ON "tickets"("order_id");

-- CreateIndex
CREATE INDEX "idx_tickets_holder_email" ON "tickets"("holder_email");

-- CreateIndex
CREATE INDEX "idx_tickets_status" ON "tickets"("status");

-- CreateIndex
CREATE INDEX "idx_checkins_event" ON "check_ins"("event_id");

-- CreateIndex
CREATE INDEX "idx_checkins_ticket" ON "check_ins"("ticket_id");

-- CreateIndex
CREATE INDEX "idx_calendar_layers_user" ON "calendar_layers"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "idx_calendar_layers_user_system" ON "calendar_layers"("user_id", "system_type");

-- CreateIndex
CREATE INDEX "idx_calendar_items_user_date" ON "calendar_items"("user_id", "date");

-- CreateIndex
CREATE INDEX "idx_calendar_items_layer_date" ON "calendar_items"("layer_id", "date");

-- CreateIndex
CREATE INDEX "idx_calendar_items_series" ON "calendar_items"("series_id");

-- CreateIndex
CREATE INDEX "idx_calendar_items_source" ON "calendar_items"("source_type", "source_id");

-- CreateIndex
CREATE UNIQUE INDEX "calendar_feeds_layer_id_key" ON "calendar_feeds"("layer_id");

-- CreateIndex
CREATE INDEX "idx_calendar_feeds_user" ON "calendar_feeds"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "shared_calendar_views_share_token_key" ON "shared_calendar_views"("share_token");

-- CreateIndex
CREATE INDEX "idx_shared_views_owner" ON "shared_calendar_views"("owner_id");

-- CreateIndex
CREATE INDEX "idx_shared_members_user" ON "shared_calendar_members"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "idx_shared_members_view_user" ON "shared_calendar_members"("view_id", "user_id");

-- CreateIndex
CREATE INDEX "idx_shared_comments_view_date" ON "shared_view_comments"("view_id", "item_date");

-- CreateIndex
CREATE UNIQUE INDEX "idx_shared_reactions_unique" ON "shared_view_reactions"("view_id", "user_id", "item_id", "emoji");

-- CreateIndex
CREATE UNIQUE INDEX "calendar_export_tokens_token_key" ON "calendar_export_tokens"("token");

-- CreateIndex
CREATE INDEX "idx_calendar_export_tokens_user" ON "calendar_export_tokens"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "doc_collab_state_document_id_key" ON "doc_collab_state"("document_id");

-- CreateIndex
CREATE UNIQUE INDEX "doc_access_policies_document_path_key" ON "doc_access_policies"("document_path");

-- CreateIndex
CREATE INDEX "doc_access_policies_created_by_id_idx" ON "doc_access_policies"("created_by_id");

-- CreateIndex
CREATE UNIQUE INDEX "doc_share_links_share_token_key" ON "doc_share_links"("share_token");

-- CreateIndex
CREATE INDEX "doc_share_links_document_path_idx" ON "doc_share_links"("document_path");

-- CreateIndex
CREATE INDEX "doc_share_links_created_by_id_idx" ON "doc_share_links"("created_by_id");

-- CreateIndex
CREATE INDEX "doc_watches_file_path_idx" ON "doc_watches"("file_path");

-- CreateIndex
CREATE UNIQUE INDEX "doc_watches_user_id_file_path_key" ON "doc_watches"("user_id", "file_path");

-- CreateIndex
CREATE UNIQUE INDEX "participant_needs_user_id_key" ON "participant_needs"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "participant_needs_contact_id_key" ON "participant_needs"("contact_id");

-- CreateIndex
CREATE UNIQUE INDEX "meeting_agendas_shift_id_key" ON "meeting_agendas"("shift_id");

-- CreateIndex
CREATE UNIQUE INDEX "meeting_agendas_poll_id_key" ON "meeting_agendas"("poll_id");

-- CreateIndex
CREATE UNIQUE INDEX "meeting_minutes_agenda_id_key" ON "meeting_minutes"("agenda_id");

-- CreateIndex
CREATE INDEX "action_items_assignee_user_id_status_idx" ON "action_items"("assignee_user_id", "status");

-- CreateIndex
CREATE INDEX "action_items_due_date_idx" ON "action_items"("due_date");

-- CreateIndex
CREATE UNIQUE INDEX "straw_polls_slug_key" ON "straw_polls"("slug");

-- CreateIndex
CREATE INDEX "straw_polls_created_by_user_id_idx" ON "straw_polls"("created_by_user_id");

-- CreateIndex
CREATE INDEX "straw_polls_status_idx" ON "straw_polls"("status");

-- CreateIndex
CREATE INDEX "straw_poll_options_poll_id_idx" ON "straw_poll_options"("poll_id");

-- CreateIndex
CREATE INDEX "straw_poll_votes_poll_id_idx" ON "straw_poll_votes"("poll_id");

-- CreateIndex
CREATE INDEX "straw_poll_votes_option_id_idx" ON "straw_poll_votes"("option_id");

-- CreateIndex
CREATE UNIQUE INDEX "straw_poll_votes_poll_id_user_id_key" ON "straw_poll_votes"("poll_id", "user_id");

-- CreateIndex
CREATE UNIQUE INDEX "straw_poll_votes_poll_id_voter_token_key" ON "straw_poll_votes"("poll_id", "voter_token");

-- CreateIndex
CREATE UNIQUE INDEX "straw_poll_votes_poll_id_voter_ip_key" ON "straw_poll_votes"("poll_id", "voter_ip");

-- CreateIndex
CREATE INDEX "straw_poll_comments_poll_id_idx" ON "straw_poll_comments"("poll_id");

-- CreateIndex
CREATE UNIQUE INDEX "straw_poll_challenges_poll_id_challenger_user_id_challenged_key" ON "straw_poll_challenges"("poll_id", "challenger_user_id", "challenged_user_id");

-- CreateIndex
CREATE UNIQUE INDEX "action_campaigns_slug_key" ON "action_campaigns"("slug");

-- CreateIndex
CREATE INDEX "idx_action_campaigns_active" ON "action_campaigns"("is_active");

-- CreateIndex
CREATE INDEX "idx_action_steps_campaign" ON "action_steps"("campaign_id");

-- CreateIndex
CREATE UNIQUE INDEX "action_steps_campaign_id_order_key" ON "action_steps"("campaign_id", "order");

-- CreateIndex
CREATE INDEX "idx_action_step_completions_user" ON "action_step_completions"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "action_step_completions_user_id_step_id_key" ON "action_step_completions"("user_id", "step_id");

-- CreateIndex
CREATE UNIQUE INDEX "documents_path_key" ON "documents"("path");

-- CreateIndex
CREATE INDEX "idx_documents_published" ON "documents"("is_published");

-- CreateIndex
CREATE INDEX "idx_documents_category" ON "documents"("category");

-- CreateIndex
CREATE INDEX "idx_documents_created_at" ON "documents"("created_at");

-- AddForeignKey
ALTER TABLE "refresh_tokens" ADD CONSTRAINT "refresh_tokens_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- 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 "petitions" ADD CONSTRAINT "petitions_linkedCampaignId_fkey" FOREIGN KEY ("linkedCampaignId") REFERENCES "campaigns"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

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

-- AddForeignKey
ALTER TABLE "petition_signatures" ADD CONSTRAINT "petition_signatures_petitionId_fkey" FOREIGN KEY ("petitionId") REFERENCES "petitions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "petition_signatures" ADD CONSTRAINT "petition_signatures_contactId_fkey" FOREIGN KEY ("contactId") REFERENCES "contacts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "campaign_emails" ADD CONSTRAINT "campaign_emails_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "campaigns"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "representative_responses" ADD CONSTRAINT "representative_responses_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "campaigns"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "representative_responses" ADD CONSTRAINT "representative_responses_submittedByUserId_fkey" FOREIGN KEY ("submittedByUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "response_upvotes" ADD CONSTRAINT "response_upvotes_responseId_fkey" FOREIGN KEY ("responseId") REFERENCES "representative_responses"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "custom_recipients" ADD CONSTRAINT "custom_recipients_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "campaigns"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "calls" ADD CONSTRAINT "calls_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "campaigns"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "locations" ADD CONSTRAINT "locations_updatedByUserId_fkey" FOREIGN KEY ("updatedByUserId") 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_cutId_fkey" FOREIGN KEY ("cutId") REFERENCES "cuts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "shifts" ADD CONSTRAINT "shifts_ticketed_event_id_fkey" FOREIGN KEY ("ticketed_event_id") REFERENCES "ticketed_events"("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 "shifts" ADD CONSTRAINT "shifts_meetingId_fkey" FOREIGN KEY ("meetingId") REFERENCES "meetings"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "shift_signups" ADD CONSTRAINT "shift_signups_shiftId_fkey" FOREIGN KEY ("shiftId") REFERENCES "shifts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "shift_signups" ADD CONSTRAINT "shift_signups_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("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 "cuts" ADD CONSTRAINT "cuts_createdByUserId_fkey" FOREIGN KEY ("createdByUserId") REFERENCES "users"("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_sessions" ADD CONSTRAINT "canvass_sessions_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "canvass_sessions" ADD CONSTRAINT "canvass_sessions_cutId_fkey" FOREIGN KEY ("cutId") REFERENCES "cuts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "canvass_sessions" ADD CONSTRAINT "canvass_sessions_shiftId_fkey" FOREIGN KEY ("shiftId") REFERENCES "shifts"("id") ON DELETE SET NULL 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 "canvass_visits" ADD CONSTRAINT "canvass_visits_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "canvass_visits" ADD CONSTRAINT "canvass_visits_shiftId_fkey" FOREIGN KEY ("shiftId") REFERENCES "shifts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "canvass_visits" ADD CONSTRAINT "canvass_visits_sessionId_fkey" FOREIGN KEY ("sessionId") REFERENCES "canvass_sessions"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tracking_sessions" ADD CONSTRAINT "tracking_sessions_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tracking_sessions" ADD CONSTRAINT "tracking_sessions_canvassSessionId_fkey" FOREIGN KEY ("canvassSessionId") REFERENCES "canvass_sessions"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "track_points" ADD CONSTRAINT "track_points_trackingSessionId_fkey" FOREIGN KEY ("trackingSessionId") REFERENCES "tracking_sessions"("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 "social_groups" ADD CONSTRAINT "social_groups_meeting_id_fkey" FOREIGN KEY ("meeting_id") REFERENCES "meetings"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "social_group_members" ADD CONSTRAINT "social_group_members_group_id_fkey" FOREIGN KEY ("group_id") REFERENCES "social_groups"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "social_group_members" ADD CONSTRAINT "social_group_members_user_id_fkey" FOREIGN KEY ("user_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 SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "payment_audit_log" ADD CONSTRAINT "payment_audit_log_order_id_fkey" FOREIGN KEY ("order_id") REFERENCES "orders"("id") ON DELETE SET NULL 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 "orders" ADD CONSTRAINT "orders_influence_campaign_id_fkey" FOREIGN KEY ("influence_campaign_id") REFERENCES "campaigns"("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_conversations" ADD CONSTRAINT "sms_conversations_contactId_fkey" FOREIGN KEY ("contactId") REFERENCES "contacts"("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;

-- AddForeignKey
ALTER TABLE "meetings" ADD CONSTRAINT "meetings_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "scheduling_polls" ADD CONSTRAINT "scheduling_polls_finalized_option_id_fkey" FOREIGN KEY ("finalized_option_id") REFERENCES "scheduling_poll_options"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "scheduling_polls" ADD CONSTRAINT "scheduling_polls_converted_shift_id_fkey" FOREIGN KEY ("converted_shift_id") REFERENCES "shifts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "scheduling_polls" ADD CONSTRAINT "scheduling_polls_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "scheduling_poll_options" ADD CONSTRAINT "scheduling_poll_options_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "scheduling_polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "scheduling_poll_votes" ADD CONSTRAINT "scheduling_poll_votes_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "scheduling_polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "scheduling_poll_votes" ADD CONSTRAINT "scheduling_poll_votes_option_id_fkey" FOREIGN KEY ("option_id") REFERENCES "scheduling_poll_options"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "scheduling_poll_votes" ADD CONSTRAINT "scheduling_poll_votes_contact_id_fkey" FOREIGN KEY ("contact_id") REFERENCES "contacts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "scheduling_poll_comments" ADD CONSTRAINT "scheduling_poll_comments_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "scheduling_polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "invite_codes" ADD CONSTRAINT "invite_codes_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "referrals" ADD CONSTRAINT "referrals_referrer_id_fkey" FOREIGN KEY ("referrer_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "referrals" ADD CONSTRAINT "referrals_referred_user_id_fkey" FOREIGN KEY ("referred_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "referrals" ADD CONSTRAINT "referrals_invite_code_id_fkey" FOREIGN KEY ("invite_code_id") REFERENCES "invite_codes"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "impact_stories" ADD CONSTRAINT "impact_stories_campaign_id_fkey" FOREIGN KEY ("campaign_id") REFERENCES "campaigns"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "impact_stories" ADD CONSTRAINT "impact_stories_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "campaign_milestones" ADD CONSTRAINT "campaign_milestones_campaign_id_fkey" FOREIGN KEY ("campaign_id") REFERENCES "campaigns"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "volunteer_spotlights" ADD CONSTRAINT "volunteer_spotlights_nominated_by_user_id_fkey" FOREIGN KEY ("nominated_by_user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "volunteer_spotlights" ADD CONSTRAINT "volunteer_spotlights_approved_by_user_id_fkey" FOREIGN KEY ("approved_by_user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "challenges" ADD CONSTRAINT "challenges_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "challenge_teams" ADD CONSTRAINT "challenge_teams_challenge_id_fkey" FOREIGN KEY ("challenge_id") REFERENCES "challenges"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "challenge_teams" ADD CONSTRAINT "challenge_teams_captain_user_id_fkey" FOREIGN KEY ("captain_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "challenge_team_members" ADD CONSTRAINT "challenge_team_members_team_id_fkey" FOREIGN KEY ("team_id") REFERENCES "challenge_teams"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "ticketed_events" ADD CONSTRAINT "ticketed_events_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ticketed_events" ADD CONSTRAINT "ticketed_events_meeting_id_fkey" FOREIGN KEY ("meeting_id") REFERENCES "meetings"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ticket_tiers" ADD CONSTRAINT "ticket_tiers_event_id_fkey" FOREIGN KEY ("event_id") REFERENCES "ticketed_events"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tickets" ADD CONSTRAINT "tickets_event_id_fkey" FOREIGN KEY ("event_id") REFERENCES "ticketed_events"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tickets" ADD CONSTRAINT "tickets_tier_id_fkey" FOREIGN KEY ("tier_id") REFERENCES "ticket_tiers"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tickets" ADD CONSTRAINT "tickets_order_id_fkey" FOREIGN KEY ("order_id") REFERENCES "orders"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "check_ins" ADD CONSTRAINT "check_ins_ticket_id_fkey" FOREIGN KEY ("ticket_id") REFERENCES "tickets"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "check_ins" ADD CONSTRAINT "check_ins_event_id_fkey" FOREIGN KEY ("event_id") REFERENCES "ticketed_events"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "check_ins" ADD CONSTRAINT "check_ins_checked_in_by_user_id_fkey" FOREIGN KEY ("checked_in_by_user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

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

-- AddForeignKey
ALTER TABLE "calendar_items" ADD CONSTRAINT "calendar_items_layer_id_fkey" FOREIGN KEY ("layer_id") REFERENCES "calendar_layers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "calendar_feeds" ADD CONSTRAINT "calendar_feeds_layer_id_fkey" FOREIGN KEY ("layer_id") REFERENCES "calendar_layers"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "shared_calendar_views" ADD CONSTRAINT "shared_calendar_views_owner_id_fkey" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "shared_calendar_members" ADD CONSTRAINT "shared_calendar_members_view_id_fkey" FOREIGN KEY ("view_id") REFERENCES "shared_calendar_views"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "shared_view_comments" ADD CONSTRAINT "shared_view_comments_view_id_fkey" FOREIGN KEY ("view_id") REFERENCES "shared_calendar_views"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "shared_view_reactions" ADD CONSTRAINT "shared_view_reactions_view_id_fkey" FOREIGN KEY ("view_id") REFERENCES "shared_calendar_views"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

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

-- AddForeignKey
ALTER TABLE "doc_access_policies" ADD CONSTRAINT "doc_access_policies_created_by_id_fkey" FOREIGN KEY ("created_by_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "doc_share_links" ADD CONSTRAINT "doc_share_links_created_by_id_fkey" FOREIGN KEY ("created_by_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

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

-- AddForeignKey
ALTER TABLE "participant_needs" ADD CONSTRAINT "participant_needs_contact_id_fkey" FOREIGN KEY ("contact_id") REFERENCES "contacts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "meeting_agendas" ADD CONSTRAINT "meeting_agendas_shift_id_fkey" FOREIGN KEY ("shift_id") REFERENCES "shifts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "meeting_agendas" ADD CONSTRAINT "meeting_agendas_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "scheduling_polls"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "meeting_agendas" ADD CONSTRAINT "meeting_agendas_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "meeting_minutes" ADD CONSTRAINT "meeting_minutes_agenda_id_fkey" FOREIGN KEY ("agenda_id") REFERENCES "meeting_agendas"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "meeting_minutes" ADD CONSTRAINT "meeting_minutes_approved_by_user_id_fkey" FOREIGN KEY ("approved_by_user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "meeting_minutes" ADD CONSTRAINT "meeting_minutes_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "action_items" ADD CONSTRAINT "action_items_agenda_id_fkey" FOREIGN KEY ("agenda_id") REFERENCES "meeting_agendas"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "action_items" ADD CONSTRAINT "action_items_assignee_user_id_fkey" FOREIGN KEY ("assignee_user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "action_items" ADD CONSTRAINT "action_items_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "straw_polls" ADD CONSTRAINT "straw_polls_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "straw_poll_options" ADD CONSTRAINT "straw_poll_options_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "straw_polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "straw_poll_votes" ADD CONSTRAINT "straw_poll_votes_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "straw_polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "straw_poll_votes" ADD CONSTRAINT "straw_poll_votes_option_id_fkey" FOREIGN KEY ("option_id") REFERENCES "straw_poll_options"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "straw_poll_votes" ADD CONSTRAINT "straw_poll_votes_contact_id_fkey" FOREIGN KEY ("contact_id") REFERENCES "contacts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "straw_poll_comments" ADD CONSTRAINT "straw_poll_comments_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "straw_polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "straw_poll_challenges" ADD CONSTRAINT "straw_poll_challenges_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "straw_polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "straw_poll_challenges" ADD CONSTRAINT "straw_poll_challenges_challenger_user_id_fkey" FOREIGN KEY ("challenger_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "straw_poll_challenges" ADD CONSTRAINT "straw_poll_challenges_challenged_user_id_fkey" FOREIGN KEY ("challenged_user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "action_campaigns" ADD CONSTRAINT "action_campaigns_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "action_steps" ADD CONSTRAINT "action_steps_campaign_id_fkey" FOREIGN KEY ("campaign_id") REFERENCES "action_campaigns"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "action_step_completions" ADD CONSTRAINT "action_step_completions_step_id_fkey" FOREIGN KEY ("step_id") REFERENCES "action_steps"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

