-- Add DEVELOPER + PARK_OPERATOR roles and ownership FKs for ProjectDevelopment
-- and IndustrialPark.
--
-- Rationale: B2B accounts provisioned by admin. A DEVELOPER account owns 0..N
-- ProjectDevelopment records; a PARK_OPERATOR account owns 0..N IndustrialPark
-- records. Existing rows remain unassigned (ownerId NULL) — admin continues to
-- manage those until they're explicitly linked to a CĐT / operator.

-- 1. Extend UserRole enum. New values placed before ADMIN to keep ADMIN last.
ALTER TYPE "UserRole" ADD VALUE IF NOT EXISTS 'DEVELOPER' BEFORE 'ADMIN';
ALTER TYPE "UserRole" ADD VALUE IF NOT EXISTS 'PARK_OPERATOR' BEFORE 'ADMIN';

-- 2. ProjectDevelopment.ownerId FK to User.id (nullable, SET NULL on user delete).
ALTER TABLE "ProjectDevelopment" ADD COLUMN "ownerId" TEXT;
ALTER TABLE "ProjectDevelopment"
  ADD CONSTRAINT "ProjectDevelopment_ownerId_fkey"
  FOREIGN KEY ("ownerId") REFERENCES "User"("id")
  ON DELETE SET NULL ON UPDATE CASCADE;
CREATE INDEX "ProjectDevelopment_ownerId_idx" ON "ProjectDevelopment"("ownerId");

-- 3. IndustrialPark.ownerId FK to User.id (nullable, SET NULL on user delete).
ALTER TABLE "IndustrialPark" ADD COLUMN "ownerId" TEXT;
ALTER TABLE "IndustrialPark"
  ADD CONSTRAINT "IndustrialPark_ownerId_fkey"
  FOREIGN KEY ("ownerId") REFERENCES "User"("id")
  ON DELETE SET NULL ON UPDATE CASCADE;
CREATE INDEX "IndustrialPark_ownerId_idx" ON "IndustrialPark"("ownerId");
