import mongoose from "mongoose";

const rentalSchema = new mongoose.Schema({
  client: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
    required: true
  },
  car: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Car",
    required: true
  },
  rentalDate: {
    type: Date,
    required: true,
    default: Date.now
  },
  expectedReturnDate: {
    type: Date,
    required: true
  },
  actualReturnDate: {
    type: Date,
    default: null
  },
  totalAmount: {
    type: Number,
    default: 0
  },
  status: {
    type: String,
    enum: ["active", "returned"],
    default: "active"
  }
}, {
  timestamps: true
});

// Calculate rental days
rentalSchema.methods.calculateRentalDays = function() {
  const returnDate = this.actualReturnDate || new Date();
  const diffTime = Math.abs(returnDate - this.rentalDate);
  const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
  return diffDays || 1; // Minimum 1 day
};

const Rental = mongoose.model("Rental", rentalSchema);

export default Rental;