-- CreateEnum
CREATE TYPE "TrackingSince" AS ENUM ('MANUFACTURED', 'INSTALLED', 'OVERHAUL');

-- CreateEnum
CREATE TYPE "InstalledStatus" AS ENUM ('INSTALLED', 'REMOVED', 'STOCK');

-- CreateEnum
CREATE TYPE "DefectStatus" AS ENUM ('OPEN', 'IN_PROGRESS', 'CLOSED');

-- CreateEnum
CREATE TYPE "DefectSeverity" AS ENUM ('MINOR', 'MAJOR', 'AOG');

-- CreateEnum
CREATE TYPE "DiscoveryPhase" AS ENUM ('FLIGHT', 'INSPECTION', 'MAINTENANCE');

-- AlterTable
ALTER TABLE "WorkOrder" ADD COLUMN     "catalogId" TEXT;

-- CreateTable
CREATE TABLE "InspectionCatalog" (
    "id" TEXT NOT NULL,
    "aircraftType" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "flightHours" DOUBLE PRECISION,
    "calendarDays" INTEGER,
    "estimatedCost" DOUBLE PRECISION NOT NULL,
    "ataChapter" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "MaintenanceTaskTemplate" (
    "id" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "estimatedHours" DOUBLE PRECISION NOT NULL,
    "skillRequired" TEXT NOT NULL,
    "catalogId" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "PartsKitItem" (
    "id" TEXT NOT NULL,
    "partNumber" TEXT NOT NULL,
    "quantity" INTEGER NOT NULL,
    "catalogId" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ResourceRequirement" (
    "id" TEXT NOT NULL,
    "resourceType" TEXT NOT NULL,
    "quantity" INTEGER NOT NULL,
    "catalogId" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ComponentTracking" (
    "id" TEXT NOT NULL,
    "componentId" TEXT NOT NULL,
    "hoursTotal" DOUBLE PRECISION NOT NULL,
    "hoursSinceOverhaul" DOUBLE PRECISION NOT NULL,
    "cycles" INTEGER NOT NULL,
    "maxUtilizationHours" DOUBLE PRECISION NOT NULL,
    "overhaulHours" DOUBLE PRECISION NOT NULL,
    "expirationDate" TIMESTAMP(3) NOT NULL,
    "trackingSince" "TrackingSince" NOT NULL,
    "installedStatus" "InstalledStatus" NOT NULL,
    "lastUpdated" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "ComponentHistory" (
    "id" TEXT NOT NULL,
    "trackingId" TEXT NOT NULL,
    "eventType" TEXT NOT NULL,
    "eventDate" TIMESTAMP(3) NOT NULL,
    "hoursAtEvent" DOUBLE PRECISION NOT NULL,
    "cyclesAtEvent" INTEGER NOT NULL,
    "description" TEXT NOT NULL,
    "performedBy" TEXT NOT NULL,
    "workOrderId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "DefectRecord" (
    "id" TEXT NOT NULL,
    "aircraftId" TEXT NOT NULL,
    "occurrenceDate" TIMESTAMP(3) NOT NULL,
    "flightHours" DOUBLE PRECISION NOT NULL,
    "landings" INTEGER NOT NULL,
    "status" "DefectStatus" NOT NULL,
    "severity" "DefectSeverity" NOT NULL,
    "ataChapter" TEXT NOT NULL,
    "discoveryPhase" "DiscoveryPhase" NOT NULL,
    "description" TEXT NOT NULL,
    "manualRefs" TEXT[],
    "correction" TEXT,
    "manhours" DOUBLE PRECISION,
    "workOrderId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "DefectPart" (
    "id" TEXT NOT NULL,
    "partNumber" TEXT NOT NULL,
    "quantity" INTEGER NOT NULL,
    "defectId" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "InspectionCatalog_aircraftType_name_key" ON "InspectionCatalog"("aircraftType", "name");

-- CreateIndex
CREATE UNIQUE INDEX "ComponentTracking_componentId_key" ON "ComponentTracking"("componentId");

-- CreateIndex
CREATE INDEX "AircraftDocument_aircraftId_idx" ON "AircraftDocument"("aircraftId");

-- CreateIndex
CREATE INDEX "AircraftDocument_type_idx" ON "AircraftDocument"("type");

-- CreateIndex
CREATE INDEX "AircraftDocument_status_idx" ON "AircraftDocument"("status");

-- CreateIndex
CREATE INDEX "AircraftSpecification_aircraftId_idx" ON "AircraftSpecification"("aircraftId");

-- CreateIndex
CREATE INDEX "MaintenanceHistory_aircraftId_idx" ON "MaintenanceHistory"("aircraftId");

-- CreateIndex
CREATE INDEX "MaintenanceStatus_aircraftId_idx" ON "MaintenanceStatus"("aircraftId");

-- CreateIndex
CREATE INDEX "WorkOrder_number_idx" ON "WorkOrder"("number");

-- CreateIndex
CREATE INDEX "WorkOrder_status_idx" ON "WorkOrder"("status");

-- CreateIndex
CREATE INDEX "WorkOrder_assetId_idx" ON "WorkOrder"("assetId");

-- CreateIndex
CREATE INDEX "WorkOrder_assetType_idx" ON "WorkOrder"("assetType");

-- AddForeignKey
ALTER TABLE "WorkOrder" ADD CONSTRAINT "WorkOrder_catalogId_fkey" FOREIGN KEY ("catalogId") REFERENCES "InspectionCatalog"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "MaintenanceTaskTemplate" ADD CONSTRAINT "MaintenanceTaskTemplate_catalogId_fkey" FOREIGN KEY ("catalogId") REFERENCES "InspectionCatalog"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PartsKitItem" ADD CONSTRAINT "PartsKitItem_catalogId_fkey" FOREIGN KEY ("catalogId") REFERENCES "InspectionCatalog"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ResourceRequirement" ADD CONSTRAINT "ResourceRequirement_catalogId_fkey" FOREIGN KEY ("catalogId") REFERENCES "InspectionCatalog"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ComponentTracking" ADD CONSTRAINT "ComponentTracking_componentId_fkey" FOREIGN KEY ("componentId") REFERENCES "Component"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ComponentHistory" ADD CONSTRAINT "ComponentHistory_trackingId_fkey" FOREIGN KEY ("trackingId") REFERENCES "ComponentTracking"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "DefectRecord" ADD CONSTRAINT "DefectRecord_aircraftId_fkey" FOREIGN KEY ("aircraftId") REFERENCES "Aircraft"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "DefectRecord" ADD CONSTRAINT "DefectRecord_workOrderId_fkey" FOREIGN KEY ("workOrderId") REFERENCES "WorkOrder"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "DefectPart" ADD CONSTRAINT "DefectPart_defectId_fkey" FOREIGN KEY ("defectId") REFERENCES "DefectRecord"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
