-- CreateTable
CREATE TABLE IF NOT EXISTS "public"."units" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX IF NOT EXISTS "units_name_key" ON "public"."units"("name");

-- Add Columns
ALTER TABLE "public"."users" ADD COLUMN IF NOT EXISTS "unitId" TEXT;
ALTER TABLE "public"."branches" ADD COLUMN IF NOT EXISTS "unitId" TEXT;

-- Indexes
CREATE INDEX IF NOT EXISTS "users_unitId_idx" ON "public"."users"("unitId");
CREATE INDEX IF NOT EXISTS "branches_unitId_idx" ON "public"."branches"("unitId");

-- Foreign Keys
ALTER TABLE "public"."users" ADD CONSTRAINT "users_unitId_fkey" FOREIGN KEY ("unitId") REFERENCES "public"."units"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "public"."branches" ADD CONSTRAINT "branches_unitId_fkey" FOREIGN KEY ("unitId") REFERENCES "public"."units"("id") ON DELETE SET NULL ON UPDATE CASCADE;


