/*
  Warnings:

  - You are about to drop the column `weatherConditions` on the `DailyReport` table. All the data in the column will be lost.
  - You are about to drop the column `activityId` on the `DailyReportMaterial` table. All the data in the column will be lost.
  - You are about to drop the column `supplierName` on the `Material` table. All the data in the column will be lost.

*/
-- CreateTable
CREATE TABLE "HumanResource" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "resourceNumber" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "resourceType" TEXT NOT NULL,
    "phone" TEXT,
    "email" TEXT,
    "address" TEXT,
    "panCardUrl" TEXT,
    "nationalityCardUrl" TEXT,
    "idCardUrl" TEXT,
    "licenseUrl" TEXT,
    "paymentType" TEXT NOT NULL DEFAULT 'Monthly',
    "monthlyRate" REAL,
    "hourlyRate" REAL,
    "overtimeRate" REAL,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "joinDate" DATETIME,
    "notes" TEXT,
    "totalWorkHours" REAL NOT NULL DEFAULT 0,
    "totalWorkCost" REAL NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "HumanResourcePayment" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "humanResourceId" TEXT NOT NULL,
    "amount" REAL NOT NULL,
    "paymentDate" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "paymentMethod" TEXT NOT NULL DEFAULT 'Cash',
    "referenceNumber" TEXT,
    "notes" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "HumanResourcePayment_humanResourceId_fkey" FOREIGN KEY ("humanResourceId") REFERENCES "HumanResource" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "ActivityColumn" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "fieldKey" TEXT NOT NULL,
    "label" TEXT NOT NULL,
    "fieldType" TEXT NOT NULL DEFAULT 'text',
    "options" TEXT,
    "width" INTEGER NOT NULL DEFAULT 120,
    "isVisible" BOOLEAN NOT NULL DEFAULT true,
    "isRequired" BOOLEAN NOT NULL DEFAULT false,
    "isEditable" BOOLEAN NOT NULL DEFAULT true,
    "sortOrder" INTEGER NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CreateTable
CREATE TABLE "ActivityDependency" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "predecessorId" TEXT NOT NULL,
    "dependentId" TEXT NOT NULL,
    "dependencyType" TEXT NOT NULL DEFAULT 'FS',
    "lagDays" INTEGER NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "ActivityDependency_predecessorId_fkey" FOREIGN KEY ("predecessorId") REFERENCES "Activity" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "ActivityDependency_dependentId_fkey" FOREIGN KEY ("dependentId") REFERENCES "Activity" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "ProjectSettings" (
    "id" TEXT NOT NULL PRIMARY KEY DEFAULT 'default',
    "companyName" TEXT NOT NULL DEFAULT 'Construction View',
    "companyLogo" TEXT,
    "projectName" TEXT NOT NULL,
    "contractId" TEXT,
    "clientName" TEXT,
    "location" TEXT,
    "preparedBy" TEXT,
    "approvedBy" TEXT,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "DailyReportTodo" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "dailyReportId" TEXT NOT NULL,
    "taskName" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'Pending',
    "priority" TEXT NOT NULL DEFAULT 'Medium',
    "category" TEXT,
    "dueDate" DATETIME NOT NULL,
    "assignedTo" TEXT,
    "assignedEmail" TEXT,
    "notes" TEXT,
    "reminderSent" BOOLEAN NOT NULL DEFAULT false,
    "reminderDate" DATETIME,
    "completedAt" DATETIME,
    "completedBy" TEXT,
    "isRecurring" BOOLEAN NOT NULL DEFAULT false,
    "recurringType" TEXT,
    "isOverdue" BOOLEAN NOT NULL DEFAULT false,
    "createdFrom" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "DailyReportTodo_dailyReportId_fkey" FOREIGN KEY ("dailyReportId") REFERENCES "DailyReport" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "EmailNotification" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "to" TEXT NOT NULL,
    "subject" TEXT NOT NULL,
    "body" TEXT NOT NULL,
    "sentAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "status" TEXT NOT NULL DEFAULT 'Sent',
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CreateTable
CREATE TABLE "QMSProject" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectCode" TEXT NOT NULL,
    "projectName" TEXT NOT NULL,
    "clientName" TEXT,
    "clientContact" TEXT,
    "startDate" DATETIME,
    "targetEndDate" DATETIME,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "QMSSubProject" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectId" TEXT NOT NULL,
    "subProjectCode" TEXT NOT NULL,
    "subProjectName" TEXT NOT NULL,
    "location" TEXT,
    "subcontractorName" TEXT,
    "subcontractorContact" TEXT,
    "consultantName" TEXT,
    "consultantContact" TEXT,
    "startDate" DATETIME,
    "targetEndDate" DATETIME,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "QMSSubProject_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "QMSProject" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "InspectionTestPlan" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "itpNumber" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "projectId" TEXT NOT NULL,
    "subProjectId" TEXT,
    "discipline" TEXT NOT NULL,
    "workActivity" TEXT NOT NULL,
    "version" TEXT NOT NULL DEFAULT '1.0',
    "status" TEXT NOT NULL DEFAULT 'Draft',
    "priority" TEXT NOT NULL DEFAULT 'Normal',
    "requiresSubcontractorSignoff" BOOLEAN NOT NULL DEFAULT true,
    "requiresMainContractorSignoff" BOOLEAN NOT NULL DEFAULT true,
    "requiresConsultantSignoff" BOOLEAN NOT NULL DEFAULT true,
    "requiresClientSignoff" BOOLEAN NOT NULL DEFAULT false,
    "subcontractorApprovedBy" TEXT,
    "subcontractorApprovalDate" DATETIME,
    "mainContractorApprovedBy" TEXT,
    "mainContractorApprovalDate" DATETIME,
    "consultantApprovedBy" TEXT,
    "consultantApprovalDate" DATETIME,
    "clientApprovedBy" TEXT,
    "clientApprovalDate" DATETIME,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "InspectionTestPlan_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "QMSProject" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "InspectionTestPlan_subProjectId_fkey" FOREIGN KEY ("subProjectId") REFERENCES "QMSSubProject" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "ITPItem" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "inspectionTestPlanId" TEXT NOT NULL,
    "itemNumber" INTEGER NOT NULL,
    "description" TEXT NOT NULL,
    "specReference" TEXT,
    "holdPoint" BOOLEAN NOT NULL DEFAULT false,
    "witnessPoint" BOOLEAN NOT NULL DEFAULT false,
    "surveillancePoint" BOOLEAN NOT NULL DEFAULT false,
    "reviewPoint" BOOLEAN NOT NULL DEFAULT false,
    "optional" BOOLEAN NOT NULL DEFAULT false,
    "acceptanceCriteria" TEXT,
    "toleranceMin" REAL,
    "toleranceMax" REAL,
    "unit" TEXT,
    "requirePhoto" BOOLEAN NOT NULL DEFAULT false,
    "requireGps" BOOLEAN NOT NULL DEFAULT false,
    "requireMeasurement" BOOLEAN NOT NULL DEFAULT false,
    "notes" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "ITPItem_inspectionTestPlanId_fkey" FOREIGN KEY ("inspectionTestPlanId") REFERENCES "InspectionTestPlan" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "QMSChecklist" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "checklistNumber" TEXT NOT NULL,
    "inspectionTestPlanId" TEXT NOT NULL,
    "inspectionId" TEXT,
    "title" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'Pending',
    "location" TEXT,
    "gpsLatitude" REAL,
    "gpsLongitude" REAL,
    "locationAddress" TEXT,
    "scheduledDate" DATETIME,
    "actualStartTime" DATETIME,
    "actualEndTime" DATETIME,
    "createdBy" TEXT NOT NULL,
    "assignedTo" TEXT,
    "photos" TEXT,
    "isOffline" BOOLEAN NOT NULL DEFAULT false,
    "lastSyncedAt" DATETIME,
    "offlineData" TEXT,
    "subcontractorStatus" TEXT,
    "subcontractorSignedBy" TEXT,
    "subcontractorSignedAt" DATETIME,
    "subcontractorComments" TEXT,
    "mainContractorStatus" TEXT,
    "mainContractorSignedBy" TEXT,
    "mainContractorSignedAt" DATETIME,
    "mainContractorComments" TEXT,
    "consultantStatus" TEXT,
    "consultantSignedBy" TEXT,
    "consultantSignedAt" DATETIME,
    "consultantComments" TEXT,
    "clientStatus" TEXT,
    "clientSignedBy" TEXT,
    "clientSignedAt" DATETIME,
    "clientComments" TEXT,
    "overallResult" TEXT,
    "completionNotes" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "QMSChecklist_inspectionTestPlanId_fkey" FOREIGN KEY ("inspectionTestPlanId") REFERENCES "InspectionTestPlan" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "QMSChecklist_inspectionId_fkey" FOREIGN KEY ("inspectionId") REFERENCES "QMSInspection" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "QMSChecklistItem" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "checklistId" TEXT NOT NULL,
    "itpItemId" TEXT,
    "itemNumber" INTEGER NOT NULL,
    "description" TEXT NOT NULL,
    "itemType" TEXT NOT NULL DEFAULT 'checkbox',
    "required" BOOLEAN NOT NULL DEFAULT true,
    "result" TEXT,
    "value" TEXT,
    "measuredValue" TEXT,
    "unit" TEXT,
    "photoRequired" BOOLEAN NOT NULL DEFAULT false,
    "photos" TEXT,
    "photoCapturedAt" DATETIME,
    "photoGpsLatitude" REAL,
    "photoGpsLongitude" REAL,
    "comments" TEXT,
    "verifiedBy" TEXT,
    "verifiedAt" DATETIME,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "QMSChecklistItem_checklistId_fkey" FOREIGN KEY ("checklistId") REFERENCES "QMSChecklist" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "QMSInspection" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "inspectionNumber" TEXT NOT NULL,
    "projectId" TEXT NOT NULL,
    "subProjectId" TEXT,
    "inspectionTestPlanId" TEXT,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "workActivity" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Scheduled',
    "overallResult" TEXT,
    "scheduledDate" DATETIME NOT NULL,
    "actualStartDate" DATETIME,
    "actualEndDate" DATETIME,
    "location" TEXT,
    "gpsLatitude" REAL,
    "gpsLongitude" REAL,
    "siteEngineer" TEXT,
    "inspectedBy" TEXT,
    "reviewedBy" TEXT,
    "weatherCondition" TEXT,
    "temperature" TEXT,
    "humidity" TEXT,
    "scope" TEXT,
    "lotNumber" TEXT,
    "quantity" REAL,
    "unit" TEXT,
    "documents" TEXT,
    "isReInspection" BOOLEAN NOT NULL DEFAULT false,
    "parentInspectionId" TEXT,
    "subcontractorApproved" BOOLEAN NOT NULL DEFAULT false,
    "subcontractorApprover" TEXT,
    "subcontractorApprovalDate" DATETIME,
    "mainContractorApproved" BOOLEAN NOT NULL DEFAULT false,
    "mainContractorApprover" TEXT,
    "mainContractorApprovalDate" DATETIME,
    "consultantApproved" BOOLEAN NOT NULL DEFAULT false,
    "consultantApprover" TEXT,
    "consultantApprovalDate" DATETIME,
    "clientApproved" BOOLEAN NOT NULL DEFAULT false,
    "clientApprover" TEXT,
    "clientApprovalDate" DATETIME,
    "notes" TEXT,
    "recommendations" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "QMSInspection_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "QMSProject" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "QMSInspection_subProjectId_fkey" FOREIGN KEY ("subProjectId") REFERENCES "QMSSubProject" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "QMSInspection_inspectionTestPlanId_fkey" FOREIGN KEY ("inspectionTestPlanId") REFERENCES "InspectionTestPlan" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "QMSNCR" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "ncrNumber" TEXT NOT NULL,
    "projectId" TEXT NOT NULL,
    "subProjectId" TEXT,
    "inspectionId" TEXT,
    "checklistId" TEXT,
    "checklistItemId" TEXT,
    "title" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "severity" TEXT NOT NULL,
    "category" TEXT NOT NULL,
    "location" TEXT,
    "gpsLatitude" REAL,
    "gpsLongitude" REAL,
    "dateIdentified" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "identifiedBy" TEXT NOT NULL,
    "photos" TEXT,
    "documents" TEXT,
    "detectionMethod" TEXT,
    "specReference" TEXT,
    "nonConformanceType" TEXT,
    "consultantInvestigation" TEXT,
    "consultantRootCause" TEXT,
    "consultantRecommendations" TEXT,
    "consultantName" TEXT,
    "consultantDate" DATETIME,
    "contractorRootCauseAnalysis" TEXT,
    "rootCauseCategory" TEXT,
    "fiveWhyAnalysis" TEXT,
    "fishboneDiagram" TEXT,
    "immediateAction" TEXT,
    "correctiveAction" TEXT,
    "preventiveAction" TEXT,
    "methodOfStatement" TEXT,
    "targetCompletionDate" DATETIME,
    "verificationStatus" TEXT,
    "verificationComments" TEXT,
    "verifiedBy" TEXT,
    "verificationDate" DATETIME,
    "closeOutNotes" TEXT,
    "lessonsLearned" TEXT,
    "closedBy" TEXT,
    "closedDate" DATETIME,
    "currentStep" INTEGER NOT NULL DEFAULT 1,
    "status" TEXT NOT NULL DEFAULT 'OPEN',
    "subcontractorSignoff" TEXT,
    "subcontractorSignoffDate" DATETIME,
    "mainContractorSignoff" TEXT,
    "mainContractorSignoffDate" DATETIME,
    "consultantSignoff" TEXT,
    "consultantSignoffDate" DATETIME,
    "clientSignoff" TEXT,
    "clientSignoffDate" DATETIME,
    "requiresReInspection" BOOLEAN NOT NULL DEFAULT false,
    "reInspectionStatus" TEXT,
    "reInspectionId" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    "targetCloseDate" DATETIME,
    "actualCloseDate" DATETIME,
    CONSTRAINT "QMSNCR_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "QMSProject" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "QMSNCR_subProjectId_fkey" FOREIGN KEY ("subProjectId") REFERENCES "QMSSubProject" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "QMSNCR_inspectionId_fkey" FOREIGN KEY ("inspectionId") REFERENCES "QMSInspection" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "QMSNCR_checklistId_fkey" FOREIGN KEY ("checklistId") REFERENCES "QMSChecklist" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "QMSUser" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT,
    "projectId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "email" TEXT,
    "role" TEXT NOT NULL,
    "company" TEXT,
    "phone" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "QMSUser_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "QMSProject" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "QMSNotification" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "message" TEXT NOT NULL,
    "relatedType" TEXT,
    "relatedId" TEXT,
    "priority" TEXT NOT NULL DEFAULT 'Normal',
    "isRead" BOOLEAN NOT NULL DEFAULT false,
    "readAt" DATETIME,
    "emailSent" BOOLEAN NOT NULL DEFAULT false,
    "pushSent" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "QMSNotification_userId_fkey" FOREIGN KEY ("userId") REFERENCES "QMSUser" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "QMSOfflineQueue" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "entityType" TEXT NOT NULL,
    "entityId" TEXT NOT NULL,
    "action" TEXT NOT NULL,
    "data" TEXT NOT NULL,
    "syncStatus" TEXT NOT NULL DEFAULT 'Pending',
    "retryCount" INTEGER NOT NULL DEFAULT 0,
    "errorMessage" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "syncedAt" DATETIME
);

-- CreateTable
CREATE TABLE "QAPlan" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "planNumber" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "projectName" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Draft',
    "totalMaterials" INTEGER NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "QAPlanMaterial" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "qaPlanId" TEXT NOT NULL,
    "materialId" TEXT NOT NULL,
    "testRequirements" TEXT NOT NULL,
    "generatedPlan" TEXT NOT NULL,
    CONSTRAINT "QAPlanMaterial_qaPlanId_fkey" FOREIGN KEY ("qaPlanId") REFERENCES "QAPlan" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "QAPlanMaterial_materialId_fkey" FOREIGN KEY ("materialId") REFERENCES "Material" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "QualityInspection" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "inspectionNumber" TEXT NOT NULL,
    "qaPlanId" TEXT NOT NULL,
    "materialArrivalId" TEXT,
    "materialId" TEXT NOT NULL,
    "batchNumber" TEXT,
    "quantity" REAL,
    "status" TEXT NOT NULL DEFAULT 'PENDING',
    "inspectionDate" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "inspectedBy" TEXT,
    "location" TEXT,
    "notes" TEXT,
    "overallResult" TEXT,
    "approvalDate" DATETIME,
    "approvedBy" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "QualityInspection_qaPlanId_fkey" FOREIGN KEY ("qaPlanId") REFERENCES "QAPlan" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "QualityInspection_materialId_fkey" FOREIGN KEY ("materialId") REFERENCES "Material" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "TestRecord" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "qualityInspectionId" TEXT NOT NULL,
    "testName" TEXT NOT NULL,
    "testMethod" TEXT,
    "standard" TEXT,
    "criticality" TEXT,
    "testDate" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "testedBy" TEXT,
    "observedValue" TEXT,
    "unit" TEXT,
    "acceptanceCriteria" TEXT,
    "result" TEXT,
    "sampleId" TEXT,
    "equipmentUsed" TEXT,
    "temperature" TEXT,
    "humidity" TEXT,
    "notes" TEXT,
    "attachments" TEXT,
    "evaluatedBy" TEXT,
    "evaluatedAt" DATETIME,
    CONSTRAINT "TestRecord_qualityInspectionId_fkey" FOREIGN KEY ("qualityInspectionId") REFERENCES "QualityInspection" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "NonConformanceReport" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "ncrNumber" TEXT NOT NULL,
    "qualityInspectionId" TEXT,
    "testRecordId" TEXT,
    "materialId" TEXT NOT NULL,
    "projectNameAndLocation" TEXT,
    "dateOfNcr" DATETIME,
    "consultantName" TEXT,
    "consultantPosition" TEXT,
    "consultantPhone" TEXT,
    "contractorName" TEXT,
    "contractorPosition" TEXT,
    "contractorPhone" TEXT,
    "clientName" TEXT,
    "clientPosition" TEXT,
    "clientPhone" TEXT,
    "documents" TEXT,
    "disciplineTrade" TEXT,
    "causesOfNonConformance" TEXT,
    "defects" TEXT,
    "ncrDetails" TEXT,
    "documentReference" TEXT,
    "ncrRaisedBy" TEXT,
    "reviewedBy" TEXT,
    "consultantDate" DATETIME,
    "contractorStand" TEXT,
    "rootCauses" TEXT,
    "proposedCorrectiveAction" TEXT,
    "methodOfStatement" TEXT,
    "contractorSignName" TEXT,
    "contractorSignPosition" TEXT,
    "contractorProjectManager" TEXT,
    "contractorSignDate" DATETIME,
    "consultantAcceptanceStatus" TEXT,
    "consultantAcceptanceComments" TEXT,
    "consultantAcceptName" TEXT,
    "consultantAcceptPosition" TEXT,
    "consultantAcceptManager" TEXT,
    "consultantAcceptDate" DATETIME,
    "contractorRemedialComments" TEXT,
    "contractorRemedialName" TEXT,
    "contractorRemedialPosition" TEXT,
    "contractorRemedialManager" TEXT,
    "contractorRemedialDate" DATETIME,
    "consultantVerificationStatus" TEXT,
    "consultantVerificationComments" TEXT,
    "consultantVerifyName" TEXT,
    "consultantVerifyPosition" TEXT,
    "consultantVerifyManager" TEXT,
    "consultantVerifyDate" DATETIME,
    "clientApprovalStatus" TEXT,
    "clientApprovalComments" TEXT,
    "clientSignName" TEXT,
    "clientSignPosition" TEXT,
    "clientSignDate" DATETIME,
    "title" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "severity" TEXT NOT NULL,
    "category" TEXT,
    "status" TEXT NOT NULL DEFAULT 'OPEN',
    "rootCause" TEXT,
    "correctiveActionDate" DATETIME,
    "correctiveActionBy" TEXT,
    "verificationDate" DATETIME,
    "verifiedBy" TEXT,
    "verificationNotes" TEXT,
    "closureDate" DATETIME,
    "closedBy" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "NonConformanceReport_qualityInspectionId_fkey" FOREIGN KEY ("qualityInspectionId") REFERENCES "QualityInspection" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "NonConformanceReport_testRecordId_fkey" FOREIGN KEY ("testRecordId") REFERENCES "TestRecord" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "NonConformanceReport_materialId_fkey" FOREIGN KEY ("materialId") REFERENCES "Material" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "IncidentReport" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "incidentNumber" TEXT NOT NULL,
    "dateOfReport" DATETIME,
    "injuredParty" TEXT,
    "employer" TEXT,
    "siteLocation" TEXT,
    "reportPreparedBy" TEXT,
    "title" TEXT,
    "signature" TEXT,
    "incidentCategory" TEXT,
    "otherIncidentCategory" TEXT,
    "dateOfIncident" DATETIME,
    "timeOfIncident" TEXT,
    "narrativeReport" TEXT,
    "witnesses" TEXT,
    "injuredPersonName" TEXT,
    "injuredPersonAddress" TEXT,
    "injuredPersonAge" INTEGER,
    "lengthOfService" TEXT,
    "timeOnPresentJob" TEXT,
    "timeClassification" TEXT,
    "severity" TEXT,
    "estimatedDaysAway" INTEGER,
    "natureOfInjury" TEXT,
    "injuryClassification" TEXT,
    "partOfBodyAffected" TEXT,
    "degreeOfDisability" TEXT,
    "dateMedicalCareReceived" DATETIME,
    "whereMedicalCareReceived" TEXT,
    "medicalCareAddress" TEXT,
    "propertyDamageDescription" TEXT,
    "costOfDamage" REAL,
    "causativeAgent" TEXT,
    "weatherFactor" TEXT,
    "unsafeConditions" TEXT,
    "personalFactors" TEXT,
    "ppeLevelRequired" TEXT,
    "modifications" TEXT,
    "injuredUsingRequiredEquipment" TEXT,
    "equipmentDifferFromPlan" TEXT,
    "specificAction" TEXT,
    "whenWillItBeDone" DATETIME,
    "responsibleParty" TEXT,
    "reviewers" TEXT,
    "investigators" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Draft',
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "Subcontractor" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "subcontractorCode" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "contactPerson" TEXT,
    "phone" TEXT,
    "email" TEXT,
    "specialty" TEXT,
    "panVatNo" TEXT,
    "notes" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "SubcontractorAssignment" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "subcontractorId" TEXT NOT NULL,
    "activityId" TEXT NOT NULL,
    "contractNumber" TEXT,
    "unitOfMeasurement" TEXT NOT NULL,
    "agreedQuantity" REAL NOT NULL DEFAULT 0,
    "unitRate" REAL NOT NULL DEFAULT 0,
    "totalValue" REAL NOT NULL DEFAULT 0,
    "startDate" DATETIME,
    "plannedEndDate" DATETIME,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "notes" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "SubcontractorAssignment_subcontractorId_fkey" FOREIGN KEY ("subcontractorId") REFERENCES "Subcontractor" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "SubcontractorAssignment_activityId_fkey" FOREIGN KEY ("activityId") REFERENCES "Activity" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "SubcontractorLabor" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "subcontractorId" TEXT NOT NULL,
    "humanResourceId" TEXT NOT NULL,
    "numberOfWorkers" INTEGER NOT NULL DEFAULT 1,
    "hourlyRate" REAL NOT NULL DEFAULT 0,
    "totalCost" REAL NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "SubcontractorLabor_subcontractorId_fkey" FOREIGN KEY ("subcontractorId") REFERENCES "Subcontractor" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "SubcontractorLabor_humanResourceId_fkey" FOREIGN KEY ("humanResourceId") REFERENCES "HumanResource" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "SubcontractorWorkLog" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "assignmentId" TEXT NOT NULL,
    "dailyReportId" TEXT,
    "workDate" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "quantityCompleted" REAL NOT NULL DEFAULT 0,
    "approvedBy" TEXT,
    "notes" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "SubcontractorWorkLog_assignmentId_fkey" FOREIGN KEY ("assignmentId") REFERENCES "SubcontractorAssignment" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "SubcontractorWorkLog_dailyReportId_fkey" FOREIGN KEY ("dailyReportId") REFERENCES "DailyReport" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "GanttTask" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "name" TEXT NOT NULL,
    "startDate" DATETIME NOT NULL,
    "endDate" DATETIME NOT NULL,
    "progress" INTEGER NOT NULL DEFAULT 0,
    "parentId" TEXT,
    "isCollapsed" BOOLEAN NOT NULL DEFAULT false,
    "category" TEXT NOT NULL DEFAULT 'Engineering',
    "color" TEXT,
    "sortOrder" INTEGER NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "GanttTask_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "GanttTask" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "GanttTaskDependency" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "predecessorId" TEXT NOT NULL,
    "dependentId" TEXT NOT NULL,
    "dependencyType" TEXT NOT NULL DEFAULT 'FS',
    "lagDays" INTEGER NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "GanttTaskDependency_predecessorId_fkey" FOREIGN KEY ("predecessorId") REFERENCES "GanttTask" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "GanttTaskDependency_dependentId_fkey" FOREIGN KEY ("dependentId") REFERENCES "GanttTask" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "TrafficManagementPlan" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "planNumber" TEXT NOT NULL,
    "projectName" TEXT,
    "projectAddress" TEXT,
    "contractNumber" TEXT,
    "contractorName" TEXT,
    "preparedBy" TEXT,
    "date" DATETIME,
    "tmpVersion" TEXT,
    "contactPhone" TEXT,
    "reviewDate" DATETIME,
    "documentsChecked" TEXT,
    "documentsCheckedOther" TEXT,
    "constructionType" TEXT,
    "projectDuration" TEXT,
    "workingDays" TEXT,
    "estimatedStartDate" DATETIME,
    "estimatedCompletionDate" DATETIME,
    "morningStartTime" TEXT,
    "morningFinishTime" TEXT,
    "afternoonStartTime" TEXT,
    "afternoonFinishTime" TEXT,
    "outsideStandardHoursRequired" BOOLEAN NOT NULL DEFAULT false,
    "outsideStandardHoursDescription" TEXT,
    "tgsRequired" BOOLEAN NOT NULL DEFAULT false,
    "tgsReferenceNumber" TEXT,
    "tgsPlanReference" TEXT,
    "tgsPlanDocumentReference" TEXT,
    "tgsPreparedBy" TEXT,
    "tgsDate" DATETIME,
    "tgsVersion" TEXT,
    "tgsReviewDate" DATETIME,
    "currentSpeedLimit" TEXT,
    "proposedSpeedLimit" TEXT,
    "speedLimitRequired" BOOLEAN NOT NULL DEFAULT false,
    "trafficControl" TEXT,
    "trafficControlOther" TEXT,
    "vehicleMovement" TEXT,
    "vehicleMovementOther" TEXT,
    "pedestrianDetourRequired" BOOLEAN NOT NULL DEFAULT false,
    "detourLocation" TEXT,
    "sharedPathUsers" BOOLEAN NOT NULL DEFAULT false,
    "cyclistDetourRequired" BOOLEAN NOT NULL DEFAULT false,
    "cyclistDetourLocation" TEXT,
    "businessAccessRequired" BOOLEAN NOT NULL DEFAULT false,
    "businessAccessDescription" TEXT,
    "busStopRelocated" BOOLEAN NOT NULL DEFAULT false,
    "busStopRelocationDescription" TEXT,
    "busRouteAffected" BOOLEAN NOT NULL DEFAULT false,
    "busRouteDescription" TEXT,
    "busStopRelocatedPublic" BOOLEAN NOT NULL DEFAULT false,
    "busStopLocationDescription" TEXT,
    "tramRouteAffected" BOOLEAN NOT NULL DEFAULT false,
    "trainRouteAffected" BOOLEAN NOT NULL DEFAULT false,
    "onStreetParkingRemoved" BOOLEAN NOT NULL DEFAULT false,
    "onStreetParkingDescription" TEXT,
    "residentParkingAffected" BOOLEAN NOT NULL DEFAULT false,
    "residentParkingDescription" TEXT,
    "workStages" TEXT,
    "advanceNoticeRequired" BOOLEAN NOT NULL DEFAULT false,
    "advanceNoticeMethod" TEXT,
    "stakeholderConsultation" BOOLEAN NOT NULL DEFAULT false,
    "stakeholderDescription" TEXT,
    "peakMorningVolume" TEXT,
    "peakAfternoonVolume" TEXT,
    "offPeakVolume" TEXT,
    "eveningVolume" TEXT,
    "nightVolume" TEXT,
    "emergencyContact" TEXT,
    "emergencyPhone" TEXT,
    "projectManagerContact" TEXT,
    "projectManagerPhone" TEXT,
    "trafficSupervisorContact" TEXT,
    "trafficSupervisorPhone" TEXT,
    "submittedBy" TEXT,
    "submittedByPhone" TEXT,
    "contractorNameSignature" TEXT,
    "contractorSignature" TEXT,
    "contractorDate" DATETIME,
    "tmpEngineerName" TEXT,
    "tmpEngineerSignature" TEXT,
    "tmpEngineerDate" DATETIME,
    "localAuthorityName" TEXT,
    "localAuthoritySignature" TEXT,
    "localAuthorityDate" DATETIME,
    "status" TEXT NOT NULL DEFAULT 'Draft',
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "InspectionRequest" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "irNumber" TEXT NOT NULL,
    "project" TEXT NOT NULL,
    "engineer" TEXT NOT NULL,
    "date" DATETIME NOT NULL,
    "discipline" TEXT NOT NULL,
    "inspectionDate" DATETIME,
    "inspectionTime" TEXT,
    "location" TEXT,
    "drawingRef" TEXT,
    "itemDescription" TEXT,
    "worksReadyBy" TEXT,
    "worksReadyTime" TEXT,
    "worksReadyDate" DATETIME,
    "status" TEXT NOT NULL DEFAULT 'Draft',
    "notes" TEXT,
    "resultCode" TEXT,
    "inspectionNotes" TEXT,
    "structuralEngineer" TEXT,
    "mepEngineer" TEXT,
    "architecturalEngineer" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "InspectionPersonnel" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "irId" TEXT NOT NULL,
    "role" TEXT NOT NULL,
    "name" TEXT,
    "date" DATETIME,
    CONSTRAINT "InspectionPersonnel_irId_fkey" FOREIGN KEY ("irId") REFERENCES "InspectionRequest" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "InspectionResult" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "irId" TEXT NOT NULL,
    "resultCode" TEXT NOT NULL,
    "notes" TEXT,
    "time" TEXT,
    "date" DATETIME,
    CONSTRAINT "InspectionResult_irId_fkey" FOREIGN KEY ("irId") REFERENCES "InspectionRequest" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "QAPMaterial" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectName" TEXT NOT NULL,
    "materialCategory" TEXT NOT NULL,
    "supplierName" TEXT NOT NULL,
    "technicalSpecsRef" TEXT,
    "testingFrequency" TEXT,
    "acceptanceCriteria" TEXT,
    "submittalStatus" TEXT NOT NULL DEFAULT 'Pending',
    "remarks" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "QAPProcess" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectName" TEXT NOT NULL,
    "activityName" TEXT NOT NULL,
    "methodStatementRef" TEXT,
    "inspectionType" TEXT NOT NULL,
    "controlPoint" TEXT NOT NULL,
    "responsibleEngineer" TEXT,
    "checklistAttached" BOOLEAN NOT NULL DEFAULT false,
    "executionStage" TEXT NOT NULL DEFAULT 'Not Started',
    "remarks" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "QAPProduct" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectName" TEXT NOT NULL,
    "deliverableElement" TEXT NOT NULL,
    "tolerancesAllowed" TEXT,
    "punchlistStatus" TEXT NOT NULL DEFAULT 'Pending',
    "commissioningRequired" BOOLEAN NOT NULL DEFAULT false,
    "commissioningStatus" TEXT,
    "omManualStatus" TEXT NOT NULL DEFAULT 'Pending',
    "clientSignoff" TEXT NOT NULL DEFAULT 'Pending',
    "remarks" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "QAPSettings" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectName" TEXT NOT NULL,
    "contractor" TEXT,
    "revisionNumber" INTEGER NOT NULL DEFAULT 1,
    "qapDate" DATETIME,
    "clientName" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "QCOrganization" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectName" TEXT NOT NULL,
    "role" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "qualification" TEXT,
    "responsibilities" TEXT,
    "contactNumber" TEXT,
    "email" TEXT,
    "reportsTo" TEXT,
    "level" INTEGER NOT NULL DEFAULT 1,
    "sortOrder" INTEGER NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "QAPInspectionSchedule" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectName" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "location" TEXT,
    "inspectionType" TEXT NOT NULL,
    "scheduledDate" DATETIME NOT NULL,
    "scheduledTime" TEXT,
    "duration" INTEGER NOT NULL DEFAULT 60,
    "inspector" TEXT,
    "inspectionFor" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Scheduled',
    "priority" TEXT NOT NULL DEFAULT 'Medium',
    "signOffBy" TEXT,
    "signOffDate" DATETIME,
    "signOffNotes" TEXT,
    "reminderSent" BOOLEAN NOT NULL DEFAULT false,
    "remarks" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "NCR" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectName" TEXT NOT NULL,
    "ncrNumber" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "location" TEXT,
    "severity" TEXT NOT NULL DEFAULT 'Minor',
    "status" TEXT NOT NULL DEFAULT 'Open',
    "rootCause" TEXT,
    "correctiveAction" TEXT,
    "preventiveAction" TEXT,
    "raisedBy" TEXT,
    "assignedTo" TEXT,
    "dueDate" DATETIME,
    "closedDate" DATETIME,
    "costImpact" REAL,
    "costCurrency" TEXT,
    "reworkHours" REAL,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "RFI" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectName" TEXT NOT NULL,
    "rfiNumber" TEXT NOT NULL,
    "subject" TEXT NOT NULL,
    "question" TEXT NOT NULL,
    "answer" TEXT,
    "location" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Open',
    "raisedBy" TEXT,
    "assignedTo" TEXT,
    "dueDate" DATETIME,
    "answeredDate" DATETIME,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "QADocument" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectName" TEXT NOT NULL,
    "documentType" TEXT NOT NULL,
    "documentName" TEXT NOT NULL,
    "documentNumber" TEXT,
    "revision" TEXT,
    "uploadDate" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expiryDate" DATETIME,
    "fileUrl" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "remarks" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "LabTest" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectName" TEXT NOT NULL,
    "testNumber" TEXT NOT NULL,
    "testType" TEXT NOT NULL,
    "sampleLocation" TEXT,
    "sampleDate" DATETIME,
    "testDate" DATETIME,
    "result" TEXT,
    "value" REAL,
    "specification" TEXT,
    "labName" TEXT,
    "certificateNo" TEXT,
    "linkedNcrId" TEXT,
    "status" TEXT NOT NULL DEFAULT 'Pending',
    "remarks" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "QAAuditLog" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "projectName" TEXT NOT NULL,
    "module" TEXT NOT NULL,
    "action" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "userName" TEXT,
    "previousValue" TEXT,
    "newValue" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "auditType" TEXT,
    "area" TEXT,
    "auditor" TEXT,
    "finding" TEXT,
    "date" DATETIME,
    "status" TEXT,
    "correctiveAction" TEXT,
    "closedDate" DATETIME
);

-- CreateTable
CREATE TABLE "KanbanSprint" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "name" TEXT NOT NULL,
    "startDate" DATETIME NOT NULL,
    "endDate" DATETIME,
    "duration" INTEGER NOT NULL DEFAULT 14,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "totalPoints" REAL NOT NULL DEFAULT 0,
    "completedPoints" REAL NOT NULL DEFAULT 0,
    "velocity" REAL NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "KanbanTask" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "sprintId" TEXT NOT NULL,
    "activityId" TEXT,
    "taskType" TEXT NOT NULL,
    "taskStatus" TEXT NOT NULL,
    "role" TEXT,
    "name" TEXT NOT NULL,
    "reason" TEXT,
    "priority" TEXT NOT NULL DEFAULT 'Medium',
    "points" REAL NOT NULL DEFAULT 0,
    "hours" REAL NOT NULL DEFAULT 0,
    "details" TEXT,
    "sortOrder" INTEGER NOT NULL DEFAULT 0,
    "completedAt" DATETIME,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "KanbanTask_sprintId_fkey" FOREIGN KEY ("sprintId") REFERENCES "KanbanSprint" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "KanbanRecord" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "sprintName" TEXT NOT NULL,
    "startDate" DATETIME NOT NULL,
    "sprintDays" INTEGER NOT NULL DEFAULT 14,
    "pointsPlanned" REAL NOT NULL DEFAULT 0,
    "pointsDone" REAL NOT NULL DEFAULT 0,
    "velocity" REAL NOT NULL DEFAULT 0,
    "achievements" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Activity" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "activityNumber" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "activityType" TEXT NOT NULL,
    "startDate" DATETIME NOT NULL,
    "plannedEndDate" DATETIME,
    "actualEndDate" DATETIME,
    "status" TEXT NOT NULL DEFAULT 'Planned',
    "location" TEXT,
    "supervisor" TEXT,
    "notes" TEXT,
    "progress" INTEGER NOT NULL DEFAULT 0,
    "sortOrder" INTEGER NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    "quantity" REAL,
    "unit" TEXT,
    "unitRate" REAL,
    "totalAmount" REAL,
    "customFields" TEXT
);
INSERT INTO "new_Activity" ("activityNumber", "activityType", "actualEndDate", "createdAt", "id", "location", "name", "notes", "plannedEndDate", "startDate", "status", "supervisor", "updatedAt") SELECT "activityNumber", "activityType", "actualEndDate", "createdAt", "id", "location", "name", "notes", "plannedEndDate", "startDate", "status", "supervisor", "updatedAt" FROM "Activity";
DROP TABLE "Activity";
ALTER TABLE "new_Activity" RENAME TO "Activity";
CREATE UNIQUE INDEX "Activity_activityNumber_key" ON "Activity"("activityNumber");
CREATE TABLE "new_DailyReport" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "reportDate" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "projectName" TEXT NOT NULL,
    "weatherAM" TEXT,
    "weatherPM" TEXT,
    "weatherNotes" TEXT,
    "preparedBy" TEXT,
    "approvedBy" TEXT,
    "notes" TEXT,
    "totalVehiclesDeployed" INTEGER NOT NULL DEFAULT 0,
    "totalWorkers" INTEGER NOT NULL DEFAULT 0,
    "totalDailyCost" REAL NOT NULL DEFAULT 0,
    "status" TEXT NOT NULL DEFAULT 'Draft',
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);
INSERT INTO "new_DailyReport" ("approvedBy", "createdAt", "id", "notes", "preparedBy", "projectName", "reportDate", "status", "totalDailyCost", "totalVehiclesDeployed", "totalWorkers", "updatedAt") SELECT "approvedBy", "createdAt", "id", "notes", "preparedBy", "projectName", "reportDate", "status", "totalDailyCost", "totalVehiclesDeployed", "totalWorkers", "updatedAt" FROM "DailyReport";
DROP TABLE "DailyReport";
ALTER TABLE "new_DailyReport" RENAME TO "DailyReport";
CREATE TABLE "new_DailyReportLabor" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "dailyReportId" TEXT NOT NULL,
    "dailyReportActivityId" TEXT,
    "humanResourceId" TEXT,
    "workerType" TEXT NOT NULL,
    "numberOfWorkers" INTEGER NOT NULL DEFAULT 1,
    "hoursWorked" REAL NOT NULL DEFAULT 0,
    "hourlyRate" REAL NOT NULL DEFAULT 0,
    "overtimeHours" REAL NOT NULL DEFAULT 0,
    "overtimeRate" REAL NOT NULL DEFAULT 0,
    "totalCost" REAL NOT NULL DEFAULT 0,
    "notes" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "DailyReportLabor_dailyReportId_fkey" FOREIGN KEY ("dailyReportId") REFERENCES "DailyReport" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "DailyReportLabor_dailyReportActivityId_fkey" FOREIGN KEY ("dailyReportActivityId") REFERENCES "DailyReportActivity" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "DailyReportLabor_humanResourceId_fkey" FOREIGN KEY ("humanResourceId") REFERENCES "HumanResource" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_DailyReportLabor" ("createdAt", "dailyReportId", "hourlyRate", "hoursWorked", "id", "notes", "numberOfWorkers", "totalCost", "workerType") SELECT "createdAt", "dailyReportId", "hourlyRate", "hoursWorked", "id", "notes", "numberOfWorkers", "totalCost", "workerType" FROM "DailyReportLabor";
DROP TABLE "DailyReportLabor";
ALTER TABLE "new_DailyReportLabor" RENAME TO "DailyReportLabor";
CREATE TABLE "new_DailyReportMaterial" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "dailyReportId" TEXT NOT NULL,
    "dailyReportActivityId" TEXT,
    "materialId" TEXT NOT NULL,
    "quantityUsed" REAL NOT NULL DEFAULT 0,
    "unitCost" REAL NOT NULL DEFAULT 0,
    "totalCost" REAL NOT NULL DEFAULT 0,
    "vendorId" TEXT,
    "notes" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "DailyReportMaterial_dailyReportId_fkey" FOREIGN KEY ("dailyReportId") REFERENCES "DailyReport" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "DailyReportMaterial_dailyReportActivityId_fkey" FOREIGN KEY ("dailyReportActivityId") REFERENCES "DailyReportActivity" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "DailyReportMaterial_materialId_fkey" FOREIGN KEY ("materialId") REFERENCES "Material" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "DailyReportMaterial_vendorId_fkey" FOREIGN KEY ("vendorId") REFERENCES "Vendor" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_DailyReportMaterial" ("createdAt", "dailyReportId", "id", "materialId", "notes", "quantityUsed", "totalCost", "unitCost") SELECT "createdAt", "dailyReportId", "id", "materialId", "notes", "quantityUsed", "totalCost", "unitCost" FROM "DailyReportMaterial";
DROP TABLE "DailyReportMaterial";
ALTER TABLE "new_DailyReportMaterial" RENAME TO "DailyReportMaterial";
CREATE TABLE "new_DailyReportVehicle" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "dailyReportId" TEXT NOT NULL,
    "dailyReportActivityId" TEXT,
    "vehicleId" TEXT NOT NULL,
    "hoursUsed" REAL NOT NULL DEFAULT 0,
    "fuelCost" REAL NOT NULL DEFAULT 0,
    "maintenanceCost" REAL NOT NULL DEFAULT 0,
    "rentalCost" REAL NOT NULL DEFAULT 0,
    "otherCost" REAL NOT NULL DEFAULT 0,
    "notes" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "DailyReportVehicle_dailyReportId_fkey" FOREIGN KEY ("dailyReportId") REFERENCES "DailyReport" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "DailyReportVehicle_dailyReportActivityId_fkey" FOREIGN KEY ("dailyReportActivityId") REFERENCES "DailyReportActivity" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "DailyReportVehicle_vehicleId_fkey" FOREIGN KEY ("vehicleId") REFERENCES "Vehicle" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_DailyReportVehicle" ("createdAt", "dailyReportId", "fuelCost", "hoursUsed", "id", "maintenanceCost", "notes", "otherCost", "rentalCost", "vehicleId") SELECT "createdAt", "dailyReportId", "fuelCost", "hoursUsed", "id", "maintenanceCost", "notes", "otherCost", "rentalCost", "vehicleId" FROM "DailyReportVehicle";
DROP TABLE "DailyReportVehicle";
ALTER TABLE "new_DailyReportVehicle" RENAME TO "DailyReportVehicle";
CREATE TABLE "new_FuelExpense" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "vehicleId" TEXT NOT NULL,
    "refuelDate" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "quantity" REAL NOT NULL,
    "unitCost" REAL NOT NULL,
    "totalCost" REAL NOT NULL,
    "odometerReading" REAL NOT NULL DEFAULT 0,
    "fuelType" TEXT NOT NULL DEFAULT 'Diesel',
    "vendorName" TEXT,
    "vendorId" TEXT,
    "receiptReference" TEXT,
    "receiptUrl" TEXT,
    "notes" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "FuelExpense_vendorId_fkey" FOREIGN KEY ("vendorId") REFERENCES "Vendor" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "FuelExpense_vehicleId_fkey" FOREIGN KEY ("vehicleId") REFERENCES "Vehicle" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_FuelExpense" ("createdAt", "fuelType", "id", "odometerReading", "quantity", "receiptReference", "refuelDate", "totalCost", "unitCost", "vehicleId", "vendorName") SELECT "createdAt", "fuelType", "id", "odometerReading", "quantity", "receiptReference", "refuelDate", "totalCost", "unitCost", "vehicleId", "vendorName" FROM "FuelExpense";
DROP TABLE "FuelExpense";
ALTER TABLE "new_FuelExpense" RENAME TO "FuelExpense";
CREATE TABLE "new_Material" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "materialNumber" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "category" TEXT NOT NULL,
    "unitOfMeasurement" TEXT NOT NULL,
    "minimumThreshold" REAL NOT NULL DEFAULT 0,
    "requiredQuantity" REAL NOT NULL DEFAULT 0,
    "unitCost" REAL NOT NULL DEFAULT 0,
    "vendorId" TEXT,
    "notes" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "status" TEXT DEFAULT 'OK',
    "materialType" TEXT,
    "grade" TEXT,
    "specifications" TEXT,
    "qaRequirements" TEXT,
    "qmsStatus" TEXT DEFAULT 'NOT_INSPECTED',
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "Material_vendorId_fkey" FOREIGN KEY ("vendorId") REFERENCES "Vendor" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_Material" ("category", "createdAt", "id", "isActive", "materialNumber", "minimumThreshold", "name", "notes", "unitCost", "unitOfMeasurement", "updatedAt") SELECT "category", "createdAt", "id", "isActive", "materialNumber", "minimumThreshold", "name", "notes", "unitCost", "unitOfMeasurement", "updatedAt" FROM "Material";
DROP TABLE "Material";
ALTER TABLE "new_Material" RENAME TO "Material";
CREATE UNIQUE INDEX "Material_materialNumber_key" ON "Material"("materialNumber");
CREATE TABLE "new_MaterialArrival" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "materialId" TEXT NOT NULL,
    "vendorId" TEXT,
    "arrivalDate" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "quantity" REAL NOT NULL,
    "supplierName" TEXT,
    "purchaseOrderNumber" TEXT,
    "deliveryReference" TEXT,
    "qualityStatus" TEXT NOT NULL DEFAULT 'Pending',
    "notes" TEXT,
    "deliveryCost" REAL NOT NULL DEFAULT 0,
    "transportationCost" REAL NOT NULL DEFAULT 0,
    "loadingCost" REAL NOT NULL DEFAULT 0,
    "unloadingCost" REAL NOT NULL DEFAULT 0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "MaterialArrival_materialId_fkey" FOREIGN KEY ("materialId") REFERENCES "Material" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "MaterialArrival_vendorId_fkey" FOREIGN KEY ("vendorId") REFERENCES "Vendor" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
INSERT INTO "new_MaterialArrival" ("arrivalDate", "createdAt", "deliveryCost", "deliveryReference", "id", "materialId", "notes", "purchaseOrderNumber", "qualityStatus", "quantity", "supplierName", "vendorId") SELECT "arrivalDate", "createdAt", "deliveryCost", "deliveryReference", "id", "materialId", "notes", "purchaseOrderNumber", "qualityStatus", "quantity", "supplierName", "vendorId" FROM "MaterialArrival";
DROP TABLE "MaterialArrival";
ALTER TABLE "new_MaterialArrival" RENAME TO "MaterialArrival";
CREATE TABLE "new_MaterialUsage" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "materialId" TEXT NOT NULL,
    "activityId" TEXT,
    "usageDate" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "quantity" REAL NOT NULL,
    "purpose" TEXT,
    "authorizedBy" TEXT,
    "notes" TEXT,
    "source" TEXT NOT NULL DEFAULT 'Manual',
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "MaterialUsage_materialId_fkey" FOREIGN KEY ("materialId") REFERENCES "Material" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT "MaterialUsage_activityId_fkey" FOREIGN KEY ("activityId") REFERENCES "Activity" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_MaterialUsage" ("activityId", "authorizedBy", "createdAt", "id", "materialId", "purpose", "quantity", "usageDate") SELECT "activityId", "authorizedBy", "createdAt", "id", "materialId", "purpose", "quantity", "usageDate" FROM "MaterialUsage";
DROP TABLE "MaterialUsage";
ALTER TABLE "new_MaterialUsage" RENAME TO "MaterialUsage";
CREATE TABLE "new_Vehicle" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "vehicleNumber" TEXT NOT NULL,
    "vehicleType" TEXT NOT NULL,
    "make" TEXT NOT NULL,
    "model" TEXT NOT NULL,
    "yearOfManufacture" INTEGER NOT NULL,
    "registrationNumber" TEXT NOT NULL,
    "purchaseDate" DATETIME,
    "initialCost" REAL NOT NULL DEFAULT 0,
    "status" TEXT NOT NULL DEFAULT 'Active',
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    "ownershipType" TEXT NOT NULL DEFAULT 'Owned',
    "hiredCompany" TEXT,
    "hiringRate" REAL,
    "hiringStartDate" DATETIME,
    "hiringEndDate" DATETIME
);
INSERT INTO "new_Vehicle" ("createdAt", "id", "initialCost", "make", "model", "purchaseDate", "registrationNumber", "status", "updatedAt", "vehicleNumber", "vehicleType", "yearOfManufacture") SELECT "createdAt", "id", "initialCost", "make", "model", "purchaseDate", "registrationNumber", "status", "updatedAt", "vehicleNumber", "vehicleType", "yearOfManufacture" FROM "Vehicle";
DROP TABLE "Vehicle";
ALTER TABLE "new_Vehicle" RENAME TO "Vehicle";
CREATE UNIQUE INDEX "Vehicle_vehicleNumber_key" ON "Vehicle"("vehicleNumber");
CREATE TABLE "new_Vendor" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "vendorCode" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "contactPerson" TEXT,
    "phone" TEXT,
    "email" TEXT,
    "address" TEXT,
    "panVatNo" TEXT,
    "materials" TEXT,
    "totalSpent" REAL NOT NULL DEFAULT 0,
    "paymentRemaining" REAL NOT NULL DEFAULT 0,
    "notes" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL
);
INSERT INTO "new_Vendor" ("address", "contactPerson", "createdAt", "email", "id", "isActive", "materials", "name", "notes", "phone", "updatedAt", "vendorCode") SELECT "address", "contactPerson", "createdAt", "email", "id", "isActive", "materials", "name", "notes", "phone", "updatedAt", "vendorCode" FROM "Vendor";
DROP TABLE "Vendor";
ALTER TABLE "new_Vendor" RENAME TO "Vendor";
CREATE UNIQUE INDEX "Vendor_vendorCode_key" ON "Vendor"("vendorCode");
CREATE UNIQUE INDEX "Vendor_panVatNo_key" ON "Vendor"("panVatNo");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

-- CreateIndex
CREATE UNIQUE INDEX "HumanResource_resourceNumber_key" ON "HumanResource"("resourceNumber");

-- CreateIndex
CREATE UNIQUE INDEX "ActivityColumn_fieldKey_key" ON "ActivityColumn"("fieldKey");

-- CreateIndex
CREATE UNIQUE INDEX "ActivityDependency_predecessorId_dependentId_key" ON "ActivityDependency"("predecessorId", "dependentId");

-- CreateIndex
CREATE UNIQUE INDEX "QMSProject_projectCode_key" ON "QMSProject"("projectCode");

-- CreateIndex
CREATE INDEX "QMSProject_status_idx" ON "QMSProject"("status");

-- CreateIndex
CREATE INDEX "QMSSubProject_projectId_idx" ON "QMSSubProject"("projectId");

-- CreateIndex
CREATE UNIQUE INDEX "QMSSubProject_projectId_subProjectCode_key" ON "QMSSubProject"("projectId", "subProjectCode");

-- CreateIndex
CREATE UNIQUE INDEX "InspectionTestPlan_itpNumber_key" ON "InspectionTestPlan"("itpNumber");

-- CreateIndex
CREATE INDEX "InspectionTestPlan_projectId_idx" ON "InspectionTestPlan"("projectId");

-- CreateIndex
CREATE INDEX "InspectionTestPlan_status_idx" ON "InspectionTestPlan"("status");

-- CreateIndex
CREATE INDEX "InspectionTestPlan_discipline_idx" ON "InspectionTestPlan"("discipline");

-- CreateIndex
CREATE INDEX "ITPItem_inspectionTestPlanId_idx" ON "ITPItem"("inspectionTestPlanId");

-- CreateIndex
CREATE UNIQUE INDEX "ITPItem_inspectionTestPlanId_itemNumber_key" ON "ITPItem"("inspectionTestPlanId", "itemNumber");

-- CreateIndex
CREATE UNIQUE INDEX "QMSChecklist_checklistNumber_key" ON "QMSChecklist"("checklistNumber");

-- CreateIndex
CREATE INDEX "QMSChecklist_inspectionTestPlanId_idx" ON "QMSChecklist"("inspectionTestPlanId");

-- CreateIndex
CREATE INDEX "QMSChecklist_inspectionId_idx" ON "QMSChecklist"("inspectionId");

-- CreateIndex
CREATE INDEX "QMSChecklist_status_idx" ON "QMSChecklist"("status");

-- CreateIndex
CREATE INDEX "QMSChecklistItem_checklistId_idx" ON "QMSChecklistItem"("checklistId");

-- CreateIndex
CREATE UNIQUE INDEX "QMSInspection_inspectionNumber_key" ON "QMSInspection"("inspectionNumber");

-- CreateIndex
CREATE INDEX "QMSInspection_projectId_idx" ON "QMSInspection"("projectId");

-- CreateIndex
CREATE INDEX "QMSInspection_status_idx" ON "QMSInspection"("status");

-- CreateIndex
CREATE INDEX "QMSInspection_scheduledDate_idx" ON "QMSInspection"("scheduledDate");

-- CreateIndex
CREATE UNIQUE INDEX "QMSNCR_ncrNumber_key" ON "QMSNCR"("ncrNumber");

-- CreateIndex
CREATE INDEX "QMSNCR_projectId_idx" ON "QMSNCR"("projectId");

-- CreateIndex
CREATE INDEX "QMSNCR_status_idx" ON "QMSNCR"("status");

-- CreateIndex
CREATE INDEX "QMSNCR_severity_idx" ON "QMSNCR"("severity");

-- CreateIndex
CREATE INDEX "QMSNCR_dateIdentified_idx" ON "QMSNCR"("dateIdentified");

-- CreateIndex
CREATE INDEX "QMSUser_projectId_idx" ON "QMSUser"("projectId");

-- CreateIndex
CREATE INDEX "QMSNotification_userId_idx" ON "QMSNotification"("userId");

-- CreateIndex
CREATE INDEX "QMSNotification_isRead_idx" ON "QMSNotification"("isRead");

-- CreateIndex
CREATE INDEX "QMSNotification_type_idx" ON "QMSNotification"("type");

-- CreateIndex
CREATE INDEX "QMSOfflineQueue_syncStatus_idx" ON "QMSOfflineQueue"("syncStatus");

-- CreateIndex
CREATE UNIQUE INDEX "QAPlan_planNumber_key" ON "QAPlan"("planNumber");

-- CreateIndex
CREATE UNIQUE INDEX "QAPlanMaterial_qaPlanId_materialId_key" ON "QAPlanMaterial"("qaPlanId", "materialId");

-- CreateIndex
CREATE UNIQUE INDEX "QualityInspection_inspectionNumber_key" ON "QualityInspection"("inspectionNumber");

-- CreateIndex
CREATE INDEX "QualityInspection_status_idx" ON "QualityInspection"("status");

-- CreateIndex
CREATE INDEX "QualityInspection_qaPlanId_idx" ON "QualityInspection"("qaPlanId");

-- CreateIndex
CREATE INDEX "TestRecord_qualityInspectionId_idx" ON "TestRecord"("qualityInspectionId");

-- CreateIndex
CREATE INDEX "TestRecord_result_idx" ON "TestRecord"("result");

-- CreateIndex
CREATE UNIQUE INDEX "NonConformanceReport_ncrNumber_key" ON "NonConformanceReport"("ncrNumber");

-- CreateIndex
CREATE INDEX "NonConformanceReport_status_idx" ON "NonConformanceReport"("status");

-- CreateIndex
CREATE INDEX "NonConformanceReport_materialId_idx" ON "NonConformanceReport"("materialId");

-- CreateIndex
CREATE UNIQUE INDEX "IncidentReport_incidentNumber_key" ON "IncidentReport"("incidentNumber");

-- CreateIndex
CREATE INDEX "IncidentReport_status_idx" ON "IncidentReport"("status");

-- CreateIndex
CREATE INDEX "IncidentReport_dateOfIncident_idx" ON "IncidentReport"("dateOfIncident");

-- CreateIndex
CREATE UNIQUE INDEX "Subcontractor_subcontractorCode_key" ON "Subcontractor"("subcontractorCode");

-- CreateIndex
CREATE UNIQUE INDEX "SubcontractorAssignment_contractNumber_key" ON "SubcontractorAssignment"("contractNumber");

-- CreateIndex
CREATE UNIQUE INDEX "SubcontractorLabor_subcontractorId_humanResourceId_key" ON "SubcontractorLabor"("subcontractorId", "humanResourceId");

-- CreateIndex
CREATE UNIQUE INDEX "TrafficManagementPlan_planNumber_key" ON "TrafficManagementPlan"("planNumber");

-- CreateIndex
CREATE UNIQUE INDEX "InspectionRequest_irNumber_key" ON "InspectionRequest"("irNumber");

-- CreateIndex
CREATE UNIQUE INDEX "QAPSettings_projectName_key" ON "QAPSettings"("projectName");

-- CreateIndex
CREATE UNIQUE INDEX "NCR_ncrNumber_key" ON "NCR"("ncrNumber");

-- CreateIndex
CREATE UNIQUE INDEX "RFI_rfiNumber_key" ON "RFI"("rfiNumber");

-- CreateIndex
CREATE UNIQUE INDEX "LabTest_testNumber_key" ON "LabTest"("testNumber");

-- CreateIndex
CREATE INDEX "KanbanTask_sprintId_idx" ON "KanbanTask"("sprintId");

-- CreateIndex
CREATE INDEX "KanbanTask_taskStatus_idx" ON "KanbanTask"("taskStatus");

-- CreateIndex
CREATE INDEX "KanbanTask_activityId_idx" ON "KanbanTask"("activityId");

-- CreateIndex
CREATE INDEX "KanbanRecord_startDate_idx" ON "KanbanRecord"("startDate");
