-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."OutlookMailboxType" AS ENUM ('SHARED', 'PERSONAL');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."OutlookSubscriptionStatus" AS ENUM ('ACTIVE', 'EXPIRED', 'ERROR', 'DISABLED');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."OutlookManageStatus" AS ENUM ('PENDING_SELECTION', 'MANAGED', 'SYNC_ERROR', 'DISABLED');

-- CreateEnum
CREATE TYPE "platform_meeting_attendance"."OutlookCancellationSource" AS ENUM ('CANCELLED_BY_ORGANIZER', 'DELETED');

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_sync_mailboxes" (
    "id" VARCHAR(32) NOT NULL,
    "mailbox_email" VARCHAR(255) NOT NULL,
    "mailbox_type" "platform_meeting_attendance"."OutlookMailboxType" NOT NULL,
    "is_enabled" BOOLEAN NOT NULL DEFAULT true,
    "is_primary_default" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_subscriptions" (
    "id" VARCHAR(32) NOT NULL,
    "mailbox_id" VARCHAR(32) NOT NULL,
    "graph_subscription_id" VARCHAR(128) NOT NULL,
    "resource" VARCHAR(255) NOT NULL,
    "status" "platform_meeting_attendance"."OutlookSubscriptionStatus" NOT NULL DEFAULT 'ACTIVE',
    "expiration_at" TIMESTAMPTZ(3) NOT NULL,
    "last_error" TEXT,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_sync_bindings" (
    "id" VARCHAR(32) NOT NULL,
    "meeting_id" VARCHAR(32),
    "meeting_series_id" VARCHAR(32),
    "graph_event_id" VARCHAR(255) NOT NULL,
    "ical_uid" VARCHAR(255) NOT NULL,
    "graph_series_master_id" VARCHAR(255),
    "graph_event_type" VARCHAR(32) NOT NULL,
    "manage_status" "platform_meeting_attendance"."OutlookManageStatus" NOT NULL DEFAULT 'PENDING_SELECTION',
    "primary_mailbox_id" VARCHAR(32) NOT NULL,
    "override_primary_mailbox_id" VARCHAR(32),
    "cancellation_source" "platform_meeting_attendance"."OutlookCancellationSource",
    "last_synced_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_sync_cursors" (
    "id" VARCHAR(32) NOT NULL,
    "mailbox_id" VARCHAR(32) NOT NULL,
    "delta_token" TEXT NOT NULL,
    "last_synced_at" TIMESTAMPTZ(3),
    "last_reconciled_at" TIMESTAMPTZ(3),
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "platform_meeting_attendance"."outlook_sync_settings" (
    "id" VARCHAR(32) NOT NULL DEFAULT 'default',
    "reconcile_cron" VARCHAR(100) NOT NULL DEFAULT '0 */2 * * *',
    "delta_batch_size" INTEGER NOT NULL DEFAULT 200,
    "lookahead_days" INTEGER NOT NULL DEFAULT 90,
    "lookback_days" INTEGER NOT NULL DEFAULT 30,
    "max_retry" INTEGER NOT NULL DEFAULT 5,
    "retry_backoff_ms" INTEGER NOT NULL DEFAULT 1000,
    "renew_before_minutes" INTEGER NOT NULL DEFAULT 1440,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "outlook_sync_mailboxes_mailbox_email_key" ON "platform_meeting_attendance"."outlook_sync_mailboxes"("mailbox_email");

-- CreateIndex
CREATE UNIQUE INDEX "outlook_subscriptions_graph_subscription_id_key" ON "platform_meeting_attendance"."outlook_subscriptions"("graph_subscription_id");

-- CreateIndex
CREATE INDEX "outlook_subscriptions_mailbox_id_idx" ON "platform_meeting_attendance"."outlook_subscriptions"("mailbox_id");

-- CreateIndex
CREATE INDEX "outlook_subscriptions_status_idx" ON "platform_meeting_attendance"."outlook_subscriptions"("status");

-- CreateIndex
CREATE INDEX "outlook_subscriptions_expiration_at_idx" ON "platform_meeting_attendance"."outlook_subscriptions"("expiration_at");

-- CreateIndex
CREATE INDEX "outlook_sync_bindings_ical_uid_idx" ON "platform_meeting_attendance"."outlook_sync_bindings"("ical_uid");

-- CreateIndex
CREATE INDEX "outlook_sync_bindings_manage_status_idx" ON "platform_meeting_attendance"."outlook_sync_bindings"("manage_status");

-- CreateIndex
CREATE INDEX "outlook_sync_bindings_meeting_id_idx" ON "platform_meeting_attendance"."outlook_sync_bindings"("meeting_id");

-- CreateIndex
CREATE UNIQUE INDEX "outlook_sync_bindings_primary_mailbox_id_graph_event_id_key" ON "platform_meeting_attendance"."outlook_sync_bindings"("primary_mailbox_id", "graph_event_id");

-- CreateIndex
CREATE UNIQUE INDEX "outlook_sync_cursors_mailbox_id_key" ON "platform_meeting_attendance"."outlook_sync_cursors"("mailbox_id");

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_subscriptions" ADD CONSTRAINT "outlook_subscriptions_mailbox_id_fkey" FOREIGN KEY ("mailbox_id") REFERENCES "platform_meeting_attendance"."outlook_sync_mailboxes"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_sync_bindings" ADD CONSTRAINT "outlook_sync_bindings_meeting_id_fkey" FOREIGN KEY ("meeting_id") REFERENCES "platform_meeting_attendance"."meetings"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_sync_bindings" ADD CONSTRAINT "outlook_sync_bindings_meeting_series_id_fkey" FOREIGN KEY ("meeting_series_id") REFERENCES "platform_meeting_attendance"."meeting_series"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_sync_bindings" ADD CONSTRAINT "outlook_sync_bindings_primary_mailbox_id_fkey" FOREIGN KEY ("primary_mailbox_id") REFERENCES "platform_meeting_attendance"."outlook_sync_mailboxes"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_sync_bindings" ADD CONSTRAINT "outlook_sync_bindings_override_primary_mailbox_id_fkey" FOREIGN KEY ("override_primary_mailbox_id") REFERENCES "platform_meeting_attendance"."outlook_sync_mailboxes"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "platform_meeting_attendance"."outlook_sync_cursors" ADD CONSTRAINT "outlook_sync_cursors_mailbox_id_fkey" FOREIGN KEY ("mailbox_id") REFERENCES "platform_meeting_attendance"."outlook_sync_mailboxes"("id") ON DELETE CASCADE ON UPDATE CASCADE;
