-- CreateEnum
CREATE TYPE "EquipmentStatus" AS ENUM ('Active', 'Repair', 'Reserve', 'WriteOff');

-- CreateEnum
CREATE TYPE "laborOperation" AS ENUM ('Manual', 'MachineManual', 'Machine');

-- CreateEnum
CREATE TYPE "EnumPeriodicityTO" AS ENUM ('Ежедневное', 'Еженедельное', 'Ежемесячное', 'Полугодовое', 'Годовое');

-- CreateEnum
CREATE TYPE "Role" AS ENUM ('Исполнитель', 'Подписант', 'Пользователь');

-- CreateEnum
CREATE TYPE "CategoryPart" AS ENUM ('Расходник', 'Запчасть', 'Инструмент', 'Спецодежда');

-- CreateEnum
CREATE TYPE "EquipmentType" AS ENUM ('Производственное', 'Энергетическое', 'Насосное', 'Компрессорное');

-- CreateTable
CREATE TABLE "Equipment" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "serialNumber" TEXT NOT NULL,
    "inventoryNumber" TEXT NOT NULL,
    "equipmentType" "EquipmentType" NOT NULL,
    "dateOfInspection" TIMESTAMP(3),
    "periodicityTO" "EnumPeriodicityTO" NOT NULL,
    "location" TEXT,
    "status" "EquipmentStatus" NOT NULL,
    "commissionedAt" TIMESTAMP(3),
    "totalEngineHours" DECIMAL(65,30),
    "engineHoursSinceLastRepair" DECIMAL(65,30),
    "lastRepairAt" TIMESTAMP(3),
    "notes" TEXT,
    "workAsPartOf" "laborOperation",
    "fuelConsumed" DOUBLE PRECISION,

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

-- CreateTable
CREATE TABLE "Employee" (
    "code" TEXT NOT NULL,
    "fullName" TEXT NOT NULL,
    "role" "Role" NOT NULL,
    "position" TEXT NOT NULL,
    "bossCode" TEXT,
    "price" DOUBLE PRECISION,
    "phoneNumber" DOUBLE PRECISION,

    CONSTRAINT "Employee_pkey" PRIMARY KEY ("code")
);

-- CreateTable
CREATE TABLE "Part" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "categories" "CategoryPart",
    "price" DOUBLE PRECISION,
    "description" TEXT,
    "serialNumber" TEXT,

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

-- CreateTable
CREATE TABLE "CategoryResource" (
    "id" TEXT NOT NULL,
    "partId" TEXT,
    "employeeCode" TEXT,

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

-- CreateIndex
CREATE UNIQUE INDEX "Equipment_inventoryNumber_key" ON "Equipment"("inventoryNumber");

-- AddForeignKey
ALTER TABLE "Employee" ADD CONSTRAINT "Employee_bossCode_fkey" FOREIGN KEY ("bossCode") REFERENCES "Employee"("code") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryResource" ADD CONSTRAINT "CategoryResource_partId_fkey" FOREIGN KEY ("partId") REFERENCES "Part"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CategoryResource" ADD CONSTRAINT "CategoryResource_employeeCode_fkey" FOREIGN KEY ("employeeCode") REFERENCES "Employee"("code") ON DELETE SET NULL ON UPDATE CASCADE;
