/*
  Warnings:

  - You are about to drop the `ExpenseCodes` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `Expenses` table. If the table is not empty, all the data it contains will be lost.

*/
-- DropForeignKey
ALTER TABLE "Expenses" DROP CONSTRAINT "Expenses_expense_code_id_fkey";

-- DropForeignKey
ALTER TABLE "Expenses" DROP CONSTRAINT "Expenses_timesheet_id_fkey";

-- AlterTable
ALTER TABLE "employees" ADD COLUMN     "supervisor_id" INTEGER,
ALTER COLUMN "first_work_day" SET DATA TYPE DATE,
ALTER COLUMN "last_work_day" SET DATA TYPE DATE;

-- AlterTable
ALTER TABLE "leave_requests" ALTER COLUMN "start_date_time" SET DATA TYPE DATE,
ALTER COLUMN "end_date_time" SET DATA TYPE DATE;

-- AlterTable
ALTER TABLE "shifts" ALTER COLUMN "date" SET DATA TYPE DATE,
ALTER COLUMN "start_time" SET DATA TYPE TIME(0),
ALTER COLUMN "end_time" SET DATA TYPE TIME(0);

-- DropTable
DROP TABLE "ExpenseCodes";

-- DropTable
DROP TABLE "Expenses";

-- CreateTable
CREATE TABLE "employees_archive" (
    "id" SERIAL NOT NULL,
    "employee_id" INTEGER NOT NULL,
    "archived_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "user_id" UUID NOT NULL,
    "first_name" TEXT NOT NULL,
    "last_name" TEXT NOT NULL,
    "external_payroll_id" INTEGER NOT NULL,
    "company_code" INTEGER NOT NULL,
    "first_Work_Day" DATE NOT NULL,
    "last_work_day" DATE NOT NULL,
    "supervisor_id" INTEGER,

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

-- CreateTable
CREATE TABLE "customers_archive" (
    "id" SERIAL NOT NULL,
    "customer_id" INTEGER NOT NULL,
    "archived_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "user_id" UUID NOT NULL,
    "invoice_id" INTEGER,

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

-- CreateTable
CREATE TABLE "leave_requests_archive" (
    "id" SERIAL NOT NULL,
    "leave_request_id" INTEGER NOT NULL,
    "archived_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "employee_id" INTEGER NOT NULL,
    "leave_type" "leave_types" NOT NULL,
    "start_date_time" DATE NOT NULL,
    "end_date_time" DATE,
    "comment" TEXT NOT NULL,
    "approval_status" "leave_approval_status" NOT NULL,

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

-- CreateTable
CREATE TABLE "timesheets_archive" (
    "id" SERIAL NOT NULL,
    "timesheet_id" INTEGER NOT NULL,
    "archive_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "employee_id" INTEGER NOT NULL,
    "is_approved" BOOLEAN NOT NULL,

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

-- CreateTable
CREATE TABLE "shifts_archive" (
    "id" SERIAL NOT NULL,
    "shift_id" INTEGER NOT NULL,
    "archive_at" TIMESTAMP(3) NOT NULL,
    "timesheet_id" INTEGER NOT NULL,
    "shift_code_id" INTEGER NOT NULL,
    "description" TEXT,
    "date" DATE NOT NULL,
    "start_time" TIME(0) NOT NULL,
    "end_time" TIME(0) NOT NULL,

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

-- CreateTable
CREATE TABLE "expenses" (
    "id" SERIAL NOT NULL,
    "timesheet_id" INTEGER NOT NULL,
    "expense_code_id" INTEGER NOT NULL,
    "date" DATE NOT NULL,
    "amount" MONEY NOT NULL,
    "attachement" TEXT,
    "description" TEXT,
    "is_approved" BOOLEAN NOT NULL DEFAULT false,
    "supervisor_comment" TEXT,

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

-- CreateTable
CREATE TABLE "expenses_archive" (
    "id" SERIAL NOT NULL,
    "expense_id" INTEGER NOT NULL,
    "timesheet_id" INTEGER NOT NULL,
    "archived_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expense_code_id" INTEGER NOT NULL,
    "date" DATE NOT NULL,
    "amount" MONEY NOT NULL,
    "attachement" TEXT,
    "description" TEXT,
    "is_approved" BOOLEAN NOT NULL,
    "supervisor_comment" TEXT,

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

-- CreateTable
CREATE TABLE "expense_codes" (
    "id" SERIAL NOT NULL,
    "expense_type" TEXT NOT NULL,
    "bank_code" TEXT NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "customers_archive_invoice_id_key" ON "customers_archive"("invoice_id");

-- AddForeignKey
ALTER TABLE "employees" ADD CONSTRAINT "employees_supervisor_id_fkey" FOREIGN KEY ("supervisor_id") REFERENCES "employees"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "employees_archive" ADD CONSTRAINT "employees_archive_employee_id_fkey" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "employees_archive" ADD CONSTRAINT "employees_archive_supervisor_id_fkey" FOREIGN KEY ("supervisor_id") REFERENCES "employees"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "customers_archive" ADD CONSTRAINT "customers_archive_customer_id_fkey" FOREIGN KEY ("customer_id") REFERENCES "customers"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "leave_requests_archive" ADD CONSTRAINT "leave_requests_archive_leave_request_id_fkey" FOREIGN KEY ("leave_request_id") REFERENCES "leave_requests"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "timesheets_archive" ADD CONSTRAINT "timesheets_archive_timesheet_id_fkey" FOREIGN KEY ("timesheet_id") REFERENCES "timesheets"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "shifts_archive" ADD CONSTRAINT "shifts_archive_shift_id_fkey" FOREIGN KEY ("shift_id") REFERENCES "shifts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "expenses" ADD CONSTRAINT "expenses_timesheet_id_fkey" FOREIGN KEY ("timesheet_id") REFERENCES "timesheets"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "expenses" ADD CONSTRAINT "expenses_expense_code_id_fkey" FOREIGN KEY ("expense_code_id") REFERENCES "expense_codes"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "expenses_archive" ADD CONSTRAINT "expenses_archive_expense_id_fkey" FOREIGN KEY ("expense_id") REFERENCES "expenses"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
