/*
  Warnings:

  - The values [PHYSICAL] on the enum `UserType` will be removed. If these variants are still used in the database, this will fail.
  - The primary key for the `company` table will be changed. If it partially fails, the table could be left without primary key constraint.
  - The primary key for the `person` table will be changed. If it partially fails, the table could be left without primary key constraint.
  - The primary key for the `user` table will be changed. If it partially fails, the table could be left without primary key constraint.
  - You are about to drop the column `role` on the `user` table. All the data in the column will be lost.
  - You are about to drop the `nursery` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `tree_nursery` table. If the table is not empty, all the data it contains will be lost.
  - A unique constraint covering the columns `[id]` on the table `company` will be added. If there are existing duplicate values, this will fail.
  - A unique constraint covering the columns `[id]` on the table `person` will be added. If there are existing duplicate values, this will fail.
  - Added the required column `role_id` to the `user` table without a default value. This is not possible if the table is not empty.

*/
-- CreateEnum
CREATE TYPE "InventoryTransactionType" AS ENUM ('INCREASE', 'DECREASE');

-- AlterEnum
BEGIN;
CREATE TYPE "UserType_new" AS ENUM ('LEGAL', 'NATURAL');
ALTER TABLE "user" ALTER COLUMN "user_type" TYPE "UserType_new" USING ("user_type"::text::"UserType_new");
ALTER TYPE "UserType" RENAME TO "UserType_old";
ALTER TYPE "UserType_new" RENAME TO "UserType";
DROP TYPE "UserType_old";
COMMIT;

-- DropForeignKey
ALTER TABLE "balance" DROP CONSTRAINT "balance_user_id_fkey";

-- DropForeignKey
ALTER TABLE "company" DROP CONSTRAINT "company_id_fkey";

-- DropForeignKey
ALTER TABLE "donation" DROP CONSTRAINT "donation_donor_id_fkey";

-- DropForeignKey
ALTER TABLE "nursery" DROP CONSTRAINT "nursery_id_fkey";

-- DropForeignKey
ALTER TABLE "person" DROP CONSTRAINT "person_id_fkey";

-- DropForeignKey
ALTER TABLE "person_address" DROP CONSTRAINT "person_address_person_id_fkey";

-- DropForeignKey
ALTER TABLE "request" DROP CONSTRAINT "request_nurseryId_fkey";

-- DropForeignKey
ALTER TABLE "request" DROP CONSTRAINT "request_requester_id_fkey";

-- DropForeignKey
ALTER TABLE "service" DROP CONSTRAINT "service_nursery_id_fkey";

-- DropForeignKey
ALTER TABLE "service" DROP CONSTRAINT "service_planter_id_fkey";

-- DropForeignKey
ALTER TABLE "tree_nursery" DROP CONSTRAINT "tree_nursery_nursery_id_fkey";

-- DropForeignKey
ALTER TABLE "tree_nursery" DROP CONSTRAINT "tree_nursery_species_id_fkey";

-- AlterTable
ALTER TABLE "balance" ALTER COLUMN "user_id" SET DATA TYPE TEXT;

-- AlterTable
ALTER TABLE "company" DROP CONSTRAINT "company_pkey",
ADD COLUMN     "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ALTER COLUMN "id" SET DATA TYPE TEXT,
ADD CONSTRAINT "company_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "donation" ALTER COLUMN "donor_id" SET DATA TYPE TEXT;

-- AlterTable
ALTER TABLE "person" DROP CONSTRAINT "person_pkey",
ALTER COLUMN "id" SET DATA TYPE TEXT,
ADD CONSTRAINT "person_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "person_address" ALTER COLUMN "person_id" SET DATA TYPE TEXT;

-- AlterTable
ALTER TABLE "request" ALTER COLUMN "requester_id" SET DATA TYPE TEXT,
ALTER COLUMN "nurseryId" SET DATA TYPE TEXT;

-- AlterTable
ALTER TABLE "service" ALTER COLUMN "planter_id" SET DATA TYPE TEXT,
ALTER COLUMN "nursery_id" SET DATA TYPE TEXT;

-- AlterTable
ALTER TABLE "user" DROP CONSTRAINT "user_pkey",
DROP COLUMN "role",
ADD COLUMN     "role_id" INTEGER NOT NULL,
ALTER COLUMN "id" DROP DEFAULT,
ALTER COLUMN "id" SET DATA TYPE TEXT,
ADD CONSTRAINT "user_pkey" PRIMARY KEY ("id");
DROP SEQUENCE "user_id_seq";

-- DropTable
DROP TABLE "nursery";

-- DropTable
DROP TABLE "tree_nursery";

-- DropEnum
DROP TYPE "Roles";

-- CreateTable
CREATE TABLE "Roles" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "permissions" JSONB NOT NULL,

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

-- CreateTable
CREATE TABLE "nursery_plant_inventory" (
    "nursery_stock_id" SERIAL NOT NULL,
    "species_id" INTEGER NOT NULL,
    "quantity" INTEGER NOT NULL,
    "type" "InventoryTransactionType" NOT NULL,
    "nursery_id" TEXT NOT NULL,

    CONSTRAINT "nursery_plant_inventory_pkey" PRIMARY KEY ("nursery_stock_id")
);

-- CreateIndex
CREATE UNIQUE INDEX "Roles_name_key" ON "Roles"("name");

-- CreateIndex
CREATE UNIQUE INDEX "company_id_key" ON "company"("id");

-- CreateIndex
CREATE UNIQUE INDEX "person_id_key" ON "person"("id");

-- AddForeignKey
ALTER TABLE "user" ADD CONSTRAINT "user_role_id_fkey" FOREIGN KEY ("role_id") REFERENCES "Roles"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "person" ADD CONSTRAINT "person_id_fkey" FOREIGN KEY ("id") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "person_address" ADD CONSTRAINT "person_address_person_id_fkey" FOREIGN KEY ("person_id") REFERENCES "person"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "company" ADD CONSTRAINT "company_id_fkey" FOREIGN KEY ("id") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "nursery_plant_inventory" ADD CONSTRAINT "nursery_plant_inventory_nursery_id_fkey" FOREIGN KEY ("nursery_id") REFERENCES "company"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "nursery_plant_inventory" ADD CONSTRAINT "nursery_plant_inventory_species_id_fkey" FOREIGN KEY ("species_id") REFERENCES "species"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "donation" ADD CONSTRAINT "donation_donor_id_fkey" FOREIGN KEY ("donor_id") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "balance" ADD CONSTRAINT "balance_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "request" ADD CONSTRAINT "request_requester_id_fkey" FOREIGN KEY ("requester_id") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "service" ADD CONSTRAINT "service_planter_id_fkey" FOREIGN KEY ("planter_id") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
