-- CreateEnum
CREATE TYPE "MenuItemType" AS ENUM (
  'INTERNAL_PAGE',
  'EXTERNAL_LINK',
  'PARENT_MENU',
  'SECTION_LABEL',
  'DIVIDER'
);

-- CreateTable
CREATE TABLE "menu_items" (
  "id" TEXT NOT NULL,
  "systemKey" TEXT,
  "label" TEXT NOT NULL,
  "itemType" "MenuItemType" NOT NULL DEFAULT 'INTERNAL_PAGE',
  "routeOrUrl" TEXT,
  "icon" TEXT,
  "parentId" TEXT,
  "displayOrder" INTEGER NOT NULL DEFAULT 0,
  "openInNewTab" BOOLEAN NOT NULL DEFAULT false,
  "isRequired" BOOLEAN NOT NULL DEFAULT false,
  "isActive" BOOLEAN NOT NULL DEFAULT true,
  "createdBy" TEXT,
  "updatedBy" TEXT,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "subscription_menu_items" (
  "id" TEXT NOT NULL,
  "plan" "Plan" NOT NULL,
  "menuItemId" TEXT NOT NULL,
  "displayOrder" INTEGER NOT NULL DEFAULT 0,
  "isActive" BOOLEAN NOT NULL DEFAULT true,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "company_menu_items" (
  "id" TEXT NOT NULL,
  "companyId" TEXT NOT NULL,
  "menuItemId" TEXT NOT NULL,
  "displayOrder" INTEGER NOT NULL DEFAULT 0,
  "isActive" BOOLEAN NOT NULL DEFAULT true,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "menu_item_role_visibility" (
  "id" TEXT NOT NULL,
  "menuItemId" TEXT NOT NULL,
  "role" "EmployeeRole" NOT NULL,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "menu_items_systemKey_key" ON "menu_items"("systemKey");

-- CreateIndex
CREATE INDEX "menu_items_parentId_idx" ON "menu_items"("parentId");

-- CreateIndex
CREATE INDEX "menu_items_isActive_displayOrder_idx" ON "menu_items"("isActive", "displayOrder");

-- CreateIndex
CREATE UNIQUE INDEX "subscription_menu_items_plan_menuItemId_key" ON "subscription_menu_items"("plan", "menuItemId");

-- CreateIndex
CREATE INDEX "subscription_menu_items_menuItemId_idx" ON "subscription_menu_items"("menuItemId");

-- CreateIndex
CREATE INDEX "subscription_menu_items_plan_displayOrder_idx" ON "subscription_menu_items"("plan", "displayOrder");

-- CreateIndex
CREATE UNIQUE INDEX "company_menu_items_companyId_menuItemId_key" ON "company_menu_items"("companyId", "menuItemId");

-- CreateIndex
CREATE INDEX "company_menu_items_menuItemId_idx" ON "company_menu_items"("menuItemId");

-- CreateIndex
CREATE INDEX "company_menu_items_companyId_displayOrder_idx" ON "company_menu_items"("companyId", "displayOrder");

-- CreateIndex
CREATE UNIQUE INDEX "menu_item_role_visibility_menuItemId_role_key" ON "menu_item_role_visibility"("menuItemId", "role");

-- CreateIndex
CREATE INDEX "menu_item_role_visibility_role_idx" ON "menu_item_role_visibility"("role");

-- AddForeignKey
ALTER TABLE "menu_items"
ADD CONSTRAINT "menu_items_parentId_fkey"
FOREIGN KEY ("parentId") REFERENCES "menu_items"("id")
ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "subscription_menu_items"
ADD CONSTRAINT "subscription_menu_items_menuItemId_fkey"
FOREIGN KEY ("menuItemId") REFERENCES "menu_items"("id")
ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "company_menu_items"
ADD CONSTRAINT "company_menu_items_companyId_fkey"
FOREIGN KEY ("companyId") REFERENCES "companies"("id")
ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "company_menu_items"
ADD CONSTRAINT "company_menu_items_menuItemId_fkey"
FOREIGN KEY ("menuItemId") REFERENCES "menu_items"("id")
ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "menu_item_role_visibility"
ADD CONSTRAINT "menu_item_role_visibility_menuItemId_fkey"
FOREIGN KEY ("menuItemId") REFERENCES "menu_items"("id")
ON DELETE CASCADE ON UPDATE CASCADE;
