const mongoose = require("mongoose");

const vehicleEntrySchema = new mongoose.Schema(
  {
    qcSample: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "QualityCheck",
      required: false,
      // index: true,
    },

    sampleId: {
      type: String,
      uppercase: true,
      trim: true,
    },

    poNumber: {
      type: String,
      uppercase: true,
      trim: true,
    },

    vendor: {
      type: String,
      trim: true,
    },
    
    slot: {
      type: Number,
      required: true,
    },

    vehicleNumber: {
      type: String,
      required: true,
      uppercase: true,
      trim: true,
      // index: true,
    },

    driverName: {
      type: String,
      trim: true,
      default: null,
    },

    driverPhone: {
      type: String,
      trim: true,
      default: null,
    },

    parameters: [
      {
        parameterName: String,
        value: String,
        result: String,
      },
    ],

    status: {
      type: String,
      enum: ["PARKED", "EXITED"],
      default: "PARKED",
      // index: true,
    },

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

    checkOutTime: {
      type: Date,
      default: null,
    },
  },
  { _id: false },
);

const parkingSchema = new mongoose.Schema(
  {
    code: {
      type: String,
      required: true,
      uppercase: true,
      trim: true,
      unique: true,
      // index: true,
    },

    // category: {
    //   type: String,
    //   enum: ["VISITOR", "COMMERCIAL", null],
    //   default : null,
    //   // required: true,
    //   // index: true,
    // },

    type: {
      type: String,
      enum: ["CAR", "TRUCK", "BUS", "OTHER", "BIKE"],
      default: "TRUCK",
    },

    capacity: {
      type: Number,
      required: true,
      default: 12,
      min: 1,
    },

    vehicles: {
      type: [vehicleEntrySchema],
      default: [],
    },

    status: {
      type: String,
      enum: [
        "AVAILABLE",
        "PARTIAL",
        "FULL",
        "OCCUPIED",
        "RESERVED",
        "MAINTENANCE",
      ],
      default: "AVAILABLE",
      // index: true,
    },

    location: {
      floor: {
        type: String,
        trim: true,
      },

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

    isActive: {
      type: Boolean,
      default: true,
      // index: true,
    },

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

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

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

module.exports = mongoose.model("Parking", parkingSchema);
