"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listBanners = listBanners;
exports.getBannerById = getBannerById;
exports.createBanner = createBanner;
exports.updateBanner = updateBanner;
exports.deleteBanner = deleteBanner;
exports.listActiveBanners = listActiveBanners;
const drizzle_orm_1 = require("drizzle-orm");
const index_js_1 = require("../db/index.js");
const schema_js_1 = require("../db/schema.js");
const bannerColumns = {
    id: schema_js_1.banners.id,
    title: schema_js_1.banners.title,
    subtitle: schema_js_1.banners.subtitle,
    imageUrl: schema_js_1.banners.imageUrl,
    linkUrl: schema_js_1.banners.linkUrl,
    linkLabel: schema_js_1.banners.linkLabel,
    isActive: schema_js_1.banners.isActive,
    sortOrder: schema_js_1.banners.sortOrder,
    createdAt: schema_js_1.banners.createdAt,
    updatedAt: schema_js_1.banners.updatedAt,
};
async function listBanners(query) {
    const conditions = [];
    if (query.q) {
        conditions.push((0, drizzle_orm_1.or)((0, drizzle_orm_1.ilike)(schema_js_1.banners.title, `%${query.q}%`), (0, drizzle_orm_1.ilike)(schema_js_1.banners.subtitle, `%${query.q}%`)));
    }
    if (!query.includeInactive) {
        conditions.push((0, drizzle_orm_1.eq)(schema_js_1.banners.isActive, true));
    }
    const where = conditions.length > 0 ? (0, drizzle_orm_1.and)(...conditions) : undefined;
    const [{ total }] = await index_js_1.db
        .select({ total: (0, drizzle_orm_1.count)() })
        .from(schema_js_1.banners)
        .where(where);
    const items = await index_js_1.db
        .select(bannerColumns)
        .from(schema_js_1.banners)
        .where(where)
        .orderBy((0, drizzle_orm_1.asc)(schema_js_1.banners.sortOrder), (0, drizzle_orm_1.asc)(schema_js_1.banners.id))
        .limit(query.perPage)
        .offset((query.page - 1) * query.perPage);
    return {
        items,
        total,
        page: query.page,
        perPage: query.perPage,
        totalPages: Math.ceil(total / query.perPage),
    };
}
async function getBannerById(id) {
    const rows = await index_js_1.db.select(bannerColumns).from(schema_js_1.banners).where((0, drizzle_orm_1.eq)(schema_js_1.banners.id, id)).limit(1);
    return rows[0] || null;
}
async function createBanner(input) {
    const rows = await index_js_1.db.insert(schema_js_1.banners).values(input).$returningId();
    const id = rows[0].id;
    return getBannerById(id);
}
async function updateBanner(id, input) {
    await index_js_1.db.update(schema_js_1.banners).set({ ...input, updatedAt: (0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP` }).where((0, drizzle_orm_1.eq)(schema_js_1.banners.id, id));
    return getBannerById(id);
}
async function deleteBanner(id) {
    await index_js_1.db.update(schema_js_1.banners).set({ isActive: false, updatedAt: (0, drizzle_orm_1.sql) `CURRENT_TIMESTAMP` }).where((0, drizzle_orm_1.eq)(schema_js_1.banners.id, id));
}
async function listActiveBanners() {
    return index_js_1.db
        .select(bannerColumns)
        .from(schema_js_1.banners)
        .where((0, drizzle_orm_1.eq)(schema_js_1.banners.isActive, true))
        .orderBy((0, drizzle_orm_1.asc)(schema_js_1.banners.sortOrder), (0, drizzle_orm_1.asc)(schema_js_1.banners.id));
}
//# sourceMappingURL=bannerService.js.map