-- CreateEnum
CREATE TYPE "roles" AS ENUM ('USER', 'ADMIN', 'CUSTOMER', 'DEALER', 'EMPLOYEE', 'HR', 'SUPERVISER', 'ACCOUNTING');

-- CreateTable
CREATE TABLE "users" (
    "user_id" SERIAL NOT NULL,
    "first_name" TEXT NOT NULL,
    "last_name" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "phone_number" INTEGER NOT NULL,
    "residence" TEXT NOT NULL,
    "role" "roles" NOT NULL DEFAULT 'USER',

    CONSTRAINT "users_pkey" PRIMARY KEY ("user_id")
);

-- CreateTable
CREATE TABLE "employees" (
    "employee_id" SERIAL NOT NULL,
    "user_id" INTEGER NOT NULL,
    "external_payroll_id" INTEGER NOT NULL,
    "company_code" INTEGER NOT NULL,
    "first_work_day" TIMESTAMP(3) NOT NULL,
    "last_work_day" TIMESTAMP(3) NOT NULL,
    "role" "roles" NOT NULL DEFAULT 'EMPLOYEE',

    CONSTRAINT "employees_pkey" PRIMARY KEY ("employee_id")
);

-- CreateTable
CREATE TABLE "customers" (
    "customer_id" SERIAL NOT NULL,
    "user_id" INTEGER NOT NULL,
    "invoice_id" INTEGER NOT NULL,
    "role" "roles" NOT NULL DEFAULT 'CUSTOMER',

    CONSTRAINT "customers_pkey" PRIMARY KEY ("customer_id")
);

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "users_phone_number_key" ON "users"("phone_number");

-- CreateIndex
CREATE UNIQUE INDEX "employees_user_id_key" ON "employees"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "employees_external_payroll_id_key" ON "employees"("external_payroll_id");

-- CreateIndex
CREATE UNIQUE INDEX "customers_user_id_key" ON "customers"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "customers_invoice_id_key" ON "customers"("invoice_id");

-- AddForeignKey
ALTER TABLE "employees" ADD CONSTRAINT "employees_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("user_id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "customers" ADD CONSTRAINT "customers_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("user_id") ON DELETE RESTRICT ON UPDATE CASCADE;
