const mongoose = require('mongoose');

// Sub-schema for fuel-wise pricing
const fuelPricingSchema = new mongoose.Schema({
    fuelType: {
        type: String,
        enum: ['petrol', 'diesel', 'cng', 'electric'],
        required: true
    },
    bootSpace: {
        type: String,
        enum: ['available', 'unavailable'],
        default: 'available'
    },
    roof: {
        type: String,
        enum: ['available', 'unavailable'],
        default: 'unavailable'
    },
    luggageCapacity: {
        type: Number,
        default: 0
    },
    acBaseFare: {
        type: Number,
        default: 0
    },
    nonAcBaseFare: {
        type: Number,
        default: 0
    },
    baseKm: {
        type: Number,
        default: 0
    },
    minFare: {
        type: Number,
        default: 0
    },
    ratePerKmAc: {
        type: Number,
        default: 0
    },
    ratePerKmNonAc: {
        type: Number,
        default: 0
    },
    waitingCharge: {
        type: Number,
        default: 0
    }
}, { _id: true });

// Sub-schema for vehicle images
const vehicleImagesSchema = new mongoose.Schema({
    frontView: { type: String },
    insideView: { type: String },
    backView: { type: String },
    bootView: { type: String }
}, { _id: false });

const vehicleGroupSchema = new mongoose.Schema({
    groupName: {
        type: String,
        required: [true, 'Group name is required'],
        trim: true
    },
    category: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'VehicleCategory',
        required: [true, 'Vehicle Category is required'],
        index: true
    },
    tier: {
        type: String,
        // enum: ["budget", "premium", "luxury"],
        required: [true, 'Vehicle Tier is required']
    },
    seats: {
        type: String,
        required: [true, 'Group seats is required']
    },
    description: {
        type: String,
        trim: true
    },
    status: {
        type: String,
        enum: ['active', 'inactive'],
        default: 'active',
        index: true
    },

    // Fuel-wise pricing (supports multiple fuel types)
    fuelPricing: [fuelPricingSchema],

    // Rental pricing for hour-based packages (1hr, 2hr, 3hr, etc.)
    rentalPricing: [{
        hours: {
            type: Number,
            required: true
        },
        baseKm: {
            type: Number,
            required: true
        },
        baseFare: {
            type: Number,
            required: true
        },
        extraFarePerKm: {
            type: Number,
            required: true
        },
        fuelType: {
            type: String,
            enum: ['petrol', 'diesel', 'cng', 'electric'],
            default: 'petrol'
        }
    }],

    // Night charge settings
    nightChargeType: {
        type: String,
        enum: ['percentage', 'fixed'],
        default: 'percentage'
    },
    nightChargeValue: {
        type: Number,
        default: 0
    },
    nightStartTime: {
        type: String,  // Format: "HH:mm" e.g., "22:00"
        default: '22:00'
    },
    nightEndTime: {
        type: String,  // Format: "HH:mm" e.g., "06:00"
        default: '06:00'
    },

    // Cancellation policy - Array of time-based refund rules
    cancellationPolicy: [{
        timeBeforePickup: {
            type: Number, // in hours
            required: true,
            min: 0
        },
        refundPercentage: {
            type: Number, // 0-100
            required: true,
            min: 0,
            max: 100
        },
        description: {
            type: String,
            trim: true,
            required: true
        }
    }],
    
    // Legacy cancellation policy string (for backward compatibility)
    cancellationPolicyText: {
        type: String,
        trim: true
    },

    // Vehicle images
    vehicleImages: vehicleImagesSchema,

    // Discount settings
    discount: {
        discountType: {
            type: String,
            enum: ['percentage', 'flat', 'none'],
            default: 'none'
        },
        value: {
            type: Number,
            default: 0,
            min: 0
        },
        maxDiscount: {
            type: Number,
            default: 0  // Cap for percentage discounts (0 = no cap)
        },
        minFare: {
            type: Number,
            default: 0  // Minimum fare required to apply discount
        },
        validFrom: {
            type: Date
        },
        validUntil: {
            type: Date
        },
        isActive: {
            type: Boolean,
            default: false
        },
        description: {
            type: String,
            trim: true  // e.g., "New Year Sale", "15% Off"
        }
    },

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

module.exports = mongoose.model('VehicleGroup', vehicleGroupSchema);