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

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

-- AlterTable
ALTER TABLE "site_settings" ADD COLUMN     "enable_social_calendar" BOOLEAN NOT NULL DEFAULT false;

-- 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")
);

-- 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");

-- 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;
