const mongoose = require("mongoose");

/* ===== DOCUMENT FILE META ===== */
const documentSchema = new mongoose.Schema(
  {
    originalName: String,
    fileName: String,
    fileUrl: String,
    uploadedAt: {
      type: Date,
      default: Date.now,
    },
  },
  { _id: false },
);

/* ===== TANKER DETAILS ===== */
const tankerSchema = new mongoose.Schema(
  {
    capacity: String,
    sealNumber: String,
    previousLoad: String,
  },
  { _id: false },
);

/* ===== MATERIAL DETAILS ===== */
const materialSchema = new mongoose.Schema(
  {
    poNumber: String,
    supplierName: String,
    materialType: String,
    materialName: String,
    quantityUnit: String,
  },
  { _id: false },
);

const commercialVehicleSchema = new mongoose.Schema(
  {
    /* ===== EXISTING FIELDS ===== */

    vehicleNumber: {
      type: String,
      trim: true,
    },

    inwardId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Inward",
    },

    vehicleType: {
      type: String,
      enum: [
        "Goods Carrier",
        "Tanker",
        "Container",
        "Load",
        "Empty",
        "Bike",
        "Car",
        "Other",
        "Truck",
      ],
    },

    movementType: {
      type: String,
      enum: ["Loading", "Unloading"],
    },

    movementTypeOut: {
      type: String,
      enum: ["Load", "Empty", "Other"],
      default: null,
    },

    referenceNo: String,

    driverName: String,

    driverNumber: String,

    qcId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "QualityCheck",
    },

    qcStatus: {
      type: String,
      enum: [
        "APPROVED",
        "REJECTED",
        "WAITING",
        "ENTRY DONE",
        "IN PROGRESS",
        "PENDING",
      ],
      default: "ENTRY DONE",
    },

    warehouseId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Warehouse",
      required: false, // keep optional if not mandatory
    },

    stockId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Stock",
    },
    blacklistId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "BlacklistVehicle",
    },

    weighbridgeStatus: {
      type: String,
      enum: ["PENDING", "COMPLETED"],
      default: "PENDING",
    },

    isBlacklisted: {
      type: Boolean,
      default: false,
    },

    goods: String,

    inWeight: Number,

    outWeight: Number,

    timeIn: {
      type: Date,
      default: Date.now,
    },

    exitTime: Date,

    gateStatus: {
      type: String,
      enum: ["Waiting", "Inside", "Exited"],
      default: "Inside",
    },

    /* ===== NEW FRONTEND GROUPED FIELDS ===== */
    tankerDetails: tankerSchema,

    materialDetails: materialSchema,

    documents: {
      invoiceFiles: [documentSchema],
      idProofFiles: [documentSchema],
      deliveryFiles: [documentSchema],
      materialFiles: [documentSchema],
    },

    stockCreated: {
      type: Boolean,
      default: false,
    },

    securityRemarks: String,

    isDeleted: {
      type: Boolean,
      default: false,
    },

    createdBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
    },

    updatedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
    },
  },
  {
    timestamps: true,
  },
);

module.exports = mongoose.model("CommercialVehicle", commercialVehicleSchema);
