"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteBanner = exports.updateBanner = exports.createBanner = exports.getAdminBanners = exports.getBanners = void 0;
const database_1 = __importDefault(require("../config/database"));
const response_utils_1 = require("../utils/response.utils");
// 1. List all banners (Public)
const getBanners = async (req, res) => {
    try {
        const banners = await database_1.default.banner.findMany({
            where: { isActive: true },
            orderBy: { sortOrder: 'asc' },
        });
        return (0, response_utils_1.sendSuccess)(res, 'Active banners retrieved', { banners });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to fetch banners', error.message, 500);
    }
};
exports.getBanners = getBanners;
// 2. Admin: List all banners (including inactive ones)
const getAdminBanners = async (req, res) => {
    try {
        if (res.locals.userRole !== 'ADMIN' && req.user?.role !== 'ADMIN') {
            return (0, response_utils_1.sendError)(res, 'Forbidden', null, 403);
        }
        const banners = await database_1.default.banner.findMany({
            orderBy: { sortOrder: 'asc' },
        });
        return (0, response_utils_1.sendSuccess)(res, 'Admin banners list retrieved', { banners });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to fetch banners list', error.message, 500);
    }
};
exports.getAdminBanners = getAdminBanners;
// 3. Admin: Create banner
const createBanner = async (req, res) => {
    try {
        const { title, imageUrl, linkUrl, sortOrder, isActive } = req.body;
        if (res.locals.userRole !== 'ADMIN' && req.user?.role !== 'ADMIN') {
            return (0, response_utils_1.sendError)(res, 'Forbidden', null, 403);
        }
        const banner = await database_1.default.banner.create({
            data: {
                title,
                imageUrl,
                linkUrl,
                sortOrder: sortOrder ? parseInt(sortOrder) : 0,
                isActive: isActive ?? true,
            },
        });
        return (0, response_utils_1.sendSuccess)(res, 'Banner created successfully', { banner });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to create banner', error.message, 500);
    }
};
exports.createBanner = createBanner;
// 4. Admin: Update banner details
const updateBanner = async (req, res) => {
    try {
        const { id } = req.params;
        const { title, imageUrl, linkUrl, sortOrder, isActive } = req.body;
        if (res.locals.userRole !== 'ADMIN' && req.user?.role !== 'ADMIN') {
            return (0, response_utils_1.sendError)(res, 'Forbidden', null, 403);
        }
        const banner = await database_1.default.banner.findUnique({ where: { id } });
        if (!banner)
            return (0, response_utils_1.sendError)(res, 'Banner not found', null, 404);
        const updated = await database_1.default.banner.update({
            where: { id },
            data: {
                title,
                imageUrl,
                linkUrl,
                sortOrder: sortOrder !== undefined ? parseInt(sortOrder) : undefined,
                isActive: isActive !== undefined ? isActive : undefined,
            },
        });
        return (0, response_utils_1.sendSuccess)(res, 'Banner updated successfully', { banner: updated });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to update banner', error.message, 500);
    }
};
exports.updateBanner = updateBanner;
// 5. Admin: Delete banner
const deleteBanner = async (req, res) => {
    try {
        const { id } = req.params;
        if (res.locals.userRole !== 'ADMIN' && req.user?.role !== 'ADMIN') {
            return (0, response_utils_1.sendError)(res, 'Forbidden', null, 403);
        }
        await database_1.default.banner.delete({ where: { id } });
        return (0, response_utils_1.sendSuccess)(res, 'Banner deleted successfully');
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to delete banner', error.message, 500);
    }
};
exports.deleteBanner = deleteBanner;
//# sourceMappingURL=banner.controller.js.map