/*
  Warnings:

  - You are about to drop the column `district_sector_assignment_id` on the `purchase_orders` table. All the data in the column will be lost.
  - You are about to drop the `district_sector_assignments` table. If the table is not empty, all the data it contains will be lost.

*/
-- DropForeignKey
ALTER TABLE "district_sector_assignments" DROP CONSTRAINT "district_sector_assignments_district_sector_id_fkey";

-- DropForeignKey
ALTER TABLE "district_sector_assignments" DROP CONSTRAINT "district_sector_assignments_userId_fkey";

-- DropForeignKey
ALTER TABLE "purchase_orders" DROP CONSTRAINT "purchase_orders_district_sector_assignment_id_fkey";

-- DropForeignKey
ALTER TABLE "sector_kyc_requests" DROP CONSTRAINT "sector_kyc_requests_assignment_id_fkey";

-- DropIndex
DROP INDEX "purchase_orders_district_sector_assignment_id_idx";

-- AlterTable
ALTER TABLE "purchase_orders" DROP COLUMN "district_sector_assignment_id",
ADD COLUMN     "district_sector_owner_id" TEXT;

-- DropTable
DROP TABLE "district_sector_assignments";

-- CreateTable
CREATE TABLE "district_sector_owner" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "district_sector_id" TEXT NOT NULL,
    "isActive" BOOLEAN NOT NULL DEFAULT false,
    "assignedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "assignedBy" TEXT,
    "revokedAt" TIMESTAMP(3),
    "revokedBy" TEXT,
    "remarks" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "district_sector_owner_userId_idx" ON "district_sector_owner"("userId");

-- CreateIndex
CREATE INDEX "district_sector_owner_district_sector_id_idx" ON "district_sector_owner"("district_sector_id");

-- CreateIndex
CREATE UNIQUE INDEX "district_sector_owner_userId_district_sector_id_key" ON "district_sector_owner"("userId", "district_sector_id");

-- CreateIndex
CREATE INDEX "purchase_orders_district_sector_owner_id_idx" ON "purchase_orders"("district_sector_owner_id");

-- AddForeignKey
ALTER TABLE "sector_kyc_requests" ADD CONSTRAINT "sector_kyc_requests_assignment_id_fkey" FOREIGN KEY ("assignment_id") REFERENCES "district_sector_owner"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "purchase_orders" ADD CONSTRAINT "purchase_orders_district_sector_owner_id_fkey" FOREIGN KEY ("district_sector_owner_id") REFERENCES "district_sector_owner"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "district_sector_owner" ADD CONSTRAINT "district_sector_owner_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "district_sector_owner" ADD CONSTRAINT "district_sector_owner_district_sector_id_fkey" FOREIGN KEY ("district_sector_id") REFERENCES "district_sectors"("id") ON DELETE CASCADE ON UPDATE CASCADE;
