const mongoose = require("mongoose");

const qcParameterSchema = new mongoose.Schema(
  {
    parameterId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "QCParam",
      // required: true,
    },

    parameterName: {
      type: String,
      required: true,
    },
    history: [
      {
        value: String,
        tempStatus: {
          type: String,
          enum: ["Approved", "Pending", "Rejected"],
          // default: "Pending",
        },
        testedAt: {
          type: Date,
          default: Date.now,
        },
        testedBy: {
          type: mongoose.Schema.Types.ObjectId,
          ref: "User",
          default: null,
        },
      },
    ],
    // value:{
    //   type:String,
    //   default:null
    // },

    // result:{
    //   type:String,
    //   enum:["PASS","FAIL","PENDING"],
    //   default:"PENDING"
    // }
  },
  { _id: false },
);

const qcSchema = new mongoose.Schema(
  {
    sampleId: {
      type: String,
      required: true,
      //   unique: true,
      //   trim: true,
      uppercase: true,
    },

    poNumber: {
      type: String,
      required: true,
      // unique: true,
      //   trim: true,
      uppercase: true,
    },

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

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

    parameters: {
      type: [qcParameterSchema],
      default: [],
    },

    vehicleId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "CommercialVehicle",
      required: false,
    },

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

    completeStatus: {
      type: String,
      enum: ["Pending", "Completed"],
      default: "Pending",
    },

    remarks: {
      type: String,
      default: null,
    },
    approvedDate: {
      type: Date,
      default: null,
    },

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

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

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

    isActive: {
      type: Boolean,
      default: true,
    },
  },
  {
    timestamps: true,
  },
);

module.exports = mongoose.model("QualityCheck", qcSchema);
