const { getTimeBucket } = require("../utils/time.utils");

exports.applyFilters = (flights, filters = {}) => {
    let result = [...flights];

    // ✅ Stops filter (0,1,2)
    if (filters.stops?.length) {
        result = result.filter(f => filters.stops.includes(f.stops));
    }

    // ✅ Departure time
    if (filters.departureTime?.length) {
        result = result.filter(f =>
            filters.departureTime.includes(getTimeBucket(f.departureTime))
        );
    }

    // ✅ Airlines
    if (filters.airlines?.length) {
        result = result.filter(f =>
            filters.airlines.includes(f.airline)
        );
    }

    // ✅ Price
    if (filters.priceRange) {
        result = result.filter(f =>
            f.price >= filters.priceRange[0] &&
            f.price <= filters.priceRange[1]
        );
    }

    return result;
};