"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteCustomer = exports.updateCustomer = exports.toggleCustomerStatus = exports.getCustomerDetails = exports.getAllCustomers = void 0;
const database_1 = __importDefault(require("../config/database"));
const response_utils_1 = require("../utils/response.utils");
// 1. List all customers
const getAllCustomers = async (req, res) => {
    try {
        if (res.locals.userRole !== 'ADMIN' && req.user?.role !== 'ADMIN') {
            return (0, response_utils_1.sendError)(res, 'Forbidden', null, 403);
        }
        const customers = await database_1.default.user.findMany({
            orderBy: { createdAt: 'desc' },
            select: {
                id: true,
                email: true,
                firstName: true,
                lastName: true,
                phone: true,
                avatarUrl: true,
                status: true,
                isEmailVerified: true,
                createdAt: true,
            },
        });
        return (0, response_utils_1.sendSuccess)(res, 'Customers list retrieved', { customers });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to fetch customers list', error.message, 500);
    }
};
exports.getAllCustomers = getAllCustomers;
// 2. View customer details and their order history
const getCustomerDetails = 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);
        }
        const customer = await database_1.default.user.findUnique({
            where: { id },
            include: {
                orders: {
                    orderBy: { createdAt: 'desc' },
                },
            },
        });
        if (!customer)
            return (0, response_utils_1.sendError)(res, 'Customer not found', null, 404);
        return (0, response_utils_1.sendSuccess)(res, 'Customer details retrieved', { customer });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to fetch customer details', error.message, 500);
    }
};
exports.getCustomerDetails = getCustomerDetails;
// 3. Toggle suspend/activate account
const toggleCustomerStatus = 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);
        }
        const customer = await database_1.default.user.findUnique({ where: { id } });
        if (!customer)
            return (0, response_utils_1.sendError)(res, 'Customer not found', null, 404);
        const newStatus = customer.status === 'SUSPENDED' ? 'ACTIVE' : 'SUSPENDED';
        const updated = await database_1.default.user.update({
            where: { id },
            data: { status: newStatus },
        });
        // Log Activity
        await database_1.default.activityLog.create({
            data: {
                adminId: req.user?.adminId,
                action: newStatus === 'SUSPENDED' ? 'USER_SUSPENDED' : 'USER_ACTIVATED',
                details: { userId: id, userEmail: customer.email },
            },
        });
        return (0, response_utils_1.sendSuccess)(res, `Customer account status updated to ${newStatus}`, { status: updated.status });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to update customer status', error.message, 500);
    }
};
exports.toggleCustomerStatus = toggleCustomerStatus;
// 4. Update customer parameters
const updateCustomer = async (req, res) => {
    try {
        const { id } = req.params;
        const { firstName, lastName, phone } = req.body;
        if (res.locals.userRole !== 'ADMIN' && req.user?.role !== 'ADMIN') {
            return (0, response_utils_1.sendError)(res, 'Forbidden', null, 403);
        }
        const updated = await database_1.default.user.update({
            where: { id },
            data: { firstName, lastName, phone },
        });
        return (0, response_utils_1.sendSuccess)(res, 'Customer parameters updated successfully', { customer: updated });
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to update customer', error.message, 500);
    }
};
exports.updateCustomer = updateCustomer;
// 5. Delete customer account
const deleteCustomer = 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.user.delete({ where: { id } });
        return (0, response_utils_1.sendSuccess)(res, 'Customer account deleted successfully');
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to delete customer account', error.message, 500);
    }
};
exports.deleteCustomer = deleteCustomer;
//# sourceMappingURL=admin-user.controller.js.map