"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProductFilters = void 0;
const drizzle_orm_1 = require("drizzle-orm");
const index_js_1 = require("../db/index.js");
const schema_js_1 = require("../db/schema.js");
const activeLookup = async (table) => {
    return index_js_1.db
        .select({ id: table.id, name: table.name, slug: table.slug })
        .from(table)
        .where((0, drizzle_orm_1.eq)(table.isActive, true))
        .orderBy((0, drizzle_orm_1.asc)(table.name));
};
const getProductFilters = async () => {
    const [brandRows, productTypeRows, collectionRows, occasionRows, frameShapeRows, faceShapeRows, genderRows, frameColorRows, lensFeatureRows, ageGroupRows, sizeRows, priceRangeRows,] = await Promise.all([
        index_js_1.db
            .select({ id: schema_js_1.brands.id, name: schema_js_1.brands.name, slug: schema_js_1.brands.slug })
            .from(schema_js_1.brands)
            .where((0, drizzle_orm_1.eq)(schema_js_1.brands.isActive, true))
            .orderBy((0, drizzle_orm_1.asc)(schema_js_1.brands.name)),
        activeLookup(schema_js_1.productTypes),
        activeLookup(schema_js_1.collections),
        activeLookup(schema_js_1.occasions),
        activeLookup(schema_js_1.frameShapes),
        activeLookup(schema_js_1.faceShapes),
        activeLookup(schema_js_1.genders),
        index_js_1.db
            .select({ id: schema_js_1.frameColors.id, name: schema_js_1.frameColors.name, slug: schema_js_1.frameColors.slug, hexCode: schema_js_1.frameColors.hexCode })
            .from(schema_js_1.frameColors)
            .where((0, drizzle_orm_1.eq)(schema_js_1.frameColors.isActive, true))
            .orderBy((0, drizzle_orm_1.asc)(schema_js_1.frameColors.name)),
        activeLookup(schema_js_1.lensFeatures),
        index_js_1.db
            .select({ id: schema_js_1.ageGroups.id, name: schema_js_1.ageGroups.name, slug: schema_js_1.ageGroups.slug, minAge: schema_js_1.ageGroups.minAge, maxAge: schema_js_1.ageGroups.maxAge })
            .from(schema_js_1.ageGroups)
            .where((0, drizzle_orm_1.eq)(schema_js_1.ageGroups.isActive, true))
            .orderBy((0, drizzle_orm_1.asc)(schema_js_1.ageGroups.sortOrder), (0, drizzle_orm_1.asc)(schema_js_1.ageGroups.name)),
        activeLookup(schema_js_1.sizes),
        index_js_1.db
            .select({ id: schema_js_1.priceRanges.id, name: schema_js_1.priceRanges.name, slug: schema_js_1.priceRanges.slug, minPrice: schema_js_1.priceRanges.minPrice, maxPrice: schema_js_1.priceRanges.maxPrice })
            .from(schema_js_1.priceRanges)
            .where((0, drizzle_orm_1.eq)(schema_js_1.priceRanges.isActive, true))
            .orderBy((0, drizzle_orm_1.asc)(schema_js_1.priceRanges.sortOrder), (0, drizzle_orm_1.asc)(schema_js_1.priceRanges.minPrice)),
    ]);
    // Fetch media for brands
    const brandMediaRows = await index_js_1.db
        .select({
        ownerId: schema_js_1.mediaAssets.ownerId,
        id: schema_js_1.mediaAssets.id,
        url: schema_js_1.mediaAssets.url,
        altText: schema_js_1.mediaAssets.altText,
        isPrimary: schema_js_1.mediaAssets.isPrimary,
        sortOrder: schema_js_1.mediaAssets.sortOrder,
    })
        .from(schema_js_1.mediaAssets)
        .where((0, drizzle_orm_1.eq)(schema_js_1.mediaAssets.ownerType, "brand"))
        .orderBy((0, drizzle_orm_1.asc)(schema_js_1.mediaAssets.sortOrder));
    const mediaByBrandId = new Map();
    for (const row of brandMediaRows) {
        const existing = mediaByBrandId.get(row.ownerId) || [];
        existing.push({
            id: row.id,
            url: row.url,
            altText: row.altText,
            isPrimary: row.isPrimary,
            sortOrder: row.sortOrder,
        });
        mediaByBrandId.set(row.ownerId, existing);
    }
    const brandsWithMedia = brandRows.map((b) => ({
        id: b.id,
        name: b.name,
        slug: b.slug,
        media: mediaByBrandId.get(b.id) || [],
    }));
    // Fetch media for frame shapes
    const frameShapeMediaRows = await index_js_1.db
        .select({
        ownerId: schema_js_1.mediaAssets.ownerId,
        id: schema_js_1.mediaAssets.id,
        url: schema_js_1.mediaAssets.url,
        altText: schema_js_1.mediaAssets.altText,
        isPrimary: schema_js_1.mediaAssets.isPrimary,
        sortOrder: schema_js_1.mediaAssets.sortOrder,
    })
        .from(schema_js_1.mediaAssets)
        .where((0, drizzle_orm_1.eq)(schema_js_1.mediaAssets.ownerType, "frame_shape"))
        .orderBy((0, drizzle_orm_1.asc)(schema_js_1.mediaAssets.sortOrder));
    const mediaByShapeId = new Map();
    for (const row of frameShapeMediaRows) {
        const existing = mediaByShapeId.get(row.ownerId) || [];
        existing.push({
            id: row.id,
            url: row.url,
            altText: row.altText,
            isPrimary: row.isPrimary,
            sortOrder: row.sortOrder,
        });
        mediaByShapeId.set(row.ownerId, existing);
    }
    const frameShapesWithMedia = frameShapeRows.map((fs) => ({
        id: fs.id,
        name: fs.name,
        slug: fs.slug,
        media: mediaByShapeId.get(fs.id) || [],
    }));
    // Fetch media for product types
    const productTypeMediaRows = await index_js_1.db
        .select({
        ownerId: schema_js_1.mediaAssets.ownerId,
        id: schema_js_1.mediaAssets.id,
        url: schema_js_1.mediaAssets.url,
        altText: schema_js_1.mediaAssets.altText,
        isPrimary: schema_js_1.mediaAssets.isPrimary,
        sortOrder: schema_js_1.mediaAssets.sortOrder,
    })
        .from(schema_js_1.mediaAssets)
        .where((0, drizzle_orm_1.eq)(schema_js_1.mediaAssets.ownerType, "product_type"))
        .orderBy((0, drizzle_orm_1.asc)(schema_js_1.mediaAssets.sortOrder));
    const mediaByProductTypeId = new Map();
    for (const row of productTypeMediaRows) {
        const existing = mediaByProductTypeId.get(row.ownerId) || [];
        existing.push({
            id: row.id,
            url: row.url,
            altText: row.altText,
            isPrimary: row.isPrimary,
            sortOrder: row.sortOrder,
        });
        mediaByProductTypeId.set(row.ownerId, existing);
    }
    const productTypesWithMedia = productTypeRows.map((pt) => ({
        id: pt.id,
        name: pt.name,
        slug: pt.slug,
        media: mediaByProductTypeId.get(pt.id) || [],
    }));
    return {
        brands: brandsWithMedia,
        productTypes: productTypesWithMedia,
        collections: collectionRows,
        occasions: occasionRows,
        frameShapes: frameShapesWithMedia,
        faceShapes: faceShapeRows,
        genders: genderRows,
        frameColors: frameColorRows,
        lensFeatures: lensFeatureRows,
        ageGroups: ageGroupRows,
        sizes: sizeRows,
        priceRanges: priceRangeRows,
        sortOptions: [
            { value: "newest", label: "Newest" },
            { value: "oldest", label: "Oldest" },
            { value: "price_asc", label: "Price: Low to High" },
            { value: "price_desc", label: "Price: High to Low" },
            { value: "rating_desc", label: "Top Rated" },
            { value: "popular", label: "Most Popular" },
            { value: "name_asc", label: "Name: A to Z" },
        ],
    };
};
exports.getProductFilters = getProductFilters;
//# sourceMappingURL=productFilters.js.map