ALTER TYPE "UserRole" ADD VALUE IF NOT EXISTS 'SALES_ADMIN';

CREATE TABLE "GlobalRoleFeatureSetting" (
    "id" TEXT NOT NULL,
    "role" "UserRole" NOT NULL,
    "feature" TEXT NOT NULL,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "updatedById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

CREATE TABLE "EstateRoleFeatureSetting" (
    "id" TEXT NOT NULL,
    "estateId" TEXT NOT NULL,
    "role" "UserRole" NOT NULL,
    "feature" TEXT NOT NULL,
    "enabled" BOOLEAN NOT NULL DEFAULT true,
    "updatedById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

CREATE UNIQUE INDEX "GlobalRoleFeatureSetting_role_feature_key" ON "GlobalRoleFeatureSetting"("role", "feature");
CREATE INDEX "GlobalRoleFeatureSetting_role_idx" ON "GlobalRoleFeatureSetting"("role");
CREATE INDEX "GlobalRoleFeatureSetting_feature_idx" ON "GlobalRoleFeatureSetting"("feature");

CREATE UNIQUE INDEX "EstateRoleFeatureSetting_estateId_role_feature_key" ON "EstateRoleFeatureSetting"("estateId", "role", "feature");
CREATE INDEX "EstateRoleFeatureSetting_estateId_role_idx" ON "EstateRoleFeatureSetting"("estateId", "role");
CREATE INDEX "EstateRoleFeatureSetting_feature_idx" ON "EstateRoleFeatureSetting"("feature");

ALTER TABLE "EstateRoleFeatureSetting" ADD CONSTRAINT "EstateRoleFeatureSetting_estateId_fkey" FOREIGN KEY ("estateId") REFERENCES "Estate"("id") ON DELETE CASCADE ON UPDATE CASCADE;
