/*
  Warnings:

  - The primary key for the `RolePermission` table will be changed. If it partially fails, the table could be left without primary key constraint.
  - You are about to drop the column `canCreate` on the `RolePermission` table. All the data in the column will be lost.
  - You are about to drop the column `canDelete` on the `RolePermission` table. All the data in the column will be lost.
  - You are about to drop the column `canEdit` on the `RolePermission` table. All the data in the column will be lost.
  - You are about to drop the column `canView` on the `RolePermission` table. All the data in the column will be lost.
  - You are about to drop the column `id` on the `RolePermission` table. All the data in the column will be lost.
  - You are about to drop the column `module` on the `RolePermission` table. All the data in the column will be lost.
  - You are about to drop the column `roleId` on the `Staff` table. All the data in the column will be lost.
  - A unique constraint covering the columns `[name,companyId]` on the table `Role` will be added. If there are existing duplicate values, this will fail.
  - Added the required column `permissionId` to the `RolePermission` table without a default value. This is not possible if the table is not empty.

*/
-- DropForeignKey
ALTER TABLE "obm_core"."Staff" DROP CONSTRAINT "Staff_roleId_fkey";

-- DropIndex
DROP INDEX "obm_core"."Role_name_key";

-- AlterTable
ALTER TABLE "obm_core"."Role" ADD COLUMN     "companyId" TEXT,
ADD COLUMN     "isSystem" BOOLEAN NOT NULL DEFAULT false;

-- AlterTable
ALTER TABLE "obm_core"."RolePermission" DROP CONSTRAINT "RolePermission_pkey",
DROP COLUMN "canCreate",
DROP COLUMN "canDelete",
DROP COLUMN "canEdit",
DROP COLUMN "canView",
DROP COLUMN "id",
DROP COLUMN "module",
ADD COLUMN     "permissionId" TEXT NOT NULL,
ADD CONSTRAINT "RolePermission_pkey" PRIMARY KEY ("roleId", "permissionId");

-- AlterTable
ALTER TABLE "obm_core"."Staff" DROP COLUMN "roleId",
ADD COLUMN     "emergencyContact" TEXT,
ADD COLUMN     "image" TEXT,
ADD COLUMN     "joiningDate" TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN     "salary" DECIMAL(65,30) NOT NULL DEFAULT 0,
ADD COLUMN     "undertakingPerson" TEXT,
ADD COLUMN     "undertakingPersonContact" TEXT;

-- CreateTable
CREATE TABLE "obm_core"."Permission" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "module" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "obm_core"."StaffRole" (
    "staffId" TEXT NOT NULL,
    "roleId" TEXT NOT NULL,

    CONSTRAINT "StaffRole_pkey" PRIMARY KEY ("staffId","roleId")
);

-- CreateTable
CREATE TABLE "obm_core"."StaffArea" (
    "staffId" TEXT NOT NULL,
    "areaId" TEXT NOT NULL,

    CONSTRAINT "StaffArea_pkey" PRIMARY KEY ("staffId","areaId")
);

-- CreateIndex
CREATE UNIQUE INDEX "Permission_name_key" ON "obm_core"."Permission"("name");

-- CreateIndex
CREATE UNIQUE INDEX "Permission_slug_key" ON "obm_core"."Permission"("slug");

-- CreateIndex
CREATE UNIQUE INDEX "Role_name_companyId_key" ON "obm_core"."Role"("name", "companyId");

-- AddForeignKey
ALTER TABLE "obm_core"."Role" ADD CONSTRAINT "Role_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "obm_core"."Company"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."StaffRole" ADD CONSTRAINT "StaffRole_staffId_fkey" FOREIGN KEY ("staffId") REFERENCES "obm_core"."Staff"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."StaffRole" ADD CONSTRAINT "StaffRole_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "obm_core"."Role"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."RolePermission" ADD CONSTRAINT "RolePermission_permissionId_fkey" FOREIGN KEY ("permissionId") REFERENCES "obm_core"."Permission"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."StaffArea" ADD CONSTRAINT "StaffArea_staffId_fkey" FOREIGN KEY ("staffId") REFERENCES "obm_core"."Staff"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "obm_core"."StaffArea" ADD CONSTRAINT "StaffArea_areaId_fkey" FOREIGN KEY ("areaId") REFERENCES "obm_core"."Area"("id") ON DELETE CASCADE ON UPDATE CASCADE;
