"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateVariantStock = exports.deleteVariant = exports.updateVariant = exports.createVariant = exports.getVariantById = exports.getVariantsByBookId = void 0;
const database_1 = __importDefault(require("../config/database"));
const response_utils_1 = require("../utils/response.utils");
const getVariantsByBookId = async (req, res) => {
    try {
        const { bookId } = req.params;
        const variants = await database_1.default.bookVariant.findMany({
            where: { bookId, isActive: true },
            orderBy: [{ isDefault: 'desc' }, { format: 'asc' }],
        });
        return (0, response_utils_1.sendSuccess)(res, 'Book variants retrieved successfully', variants);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Error fetching variants', error.message, 500);
    }
};
exports.getVariantsByBookId = getVariantsByBookId;
const getVariantById = async (req, res) => {
    try {
        const { id } = req.params;
        const variant = await database_1.default.bookVariant.findUnique({
            where: { id },
            include: { book: true },
        });
        if (!variant) {
            return (0, response_utils_1.sendError)(res, 'Variant not found', null, 404);
        }
        return (0, response_utils_1.sendSuccess)(res, 'Variant details retrieved', variant);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Error fetching variant', error.message, 500);
    }
};
exports.getVariantById = getVariantById;
const createVariant = async (req, res) => {
    try {
        const { bookId } = req.params;
        const input = req.body;
        const book = await database_1.default.book.findUnique({ where: { id: bookId } });
        if (!book) {
            return (0, response_utils_1.sendError)(res, 'Book not found', null, 404);
        }
        // Check SKU uniqueness
        const existingSku = await database_1.default.bookVariant.findUnique({ where: { sku: input.sku } });
        if (existingSku) {
            return (0, response_utils_1.sendError)(res, `Variant SKU '${input.sku}' already exists`, null, 400);
        }
        // If this variant is set as default, unset other defaults for this book
        if (input.isDefault) {
            await database_1.default.bookVariant.updateMany({
                where: { bookId },
                data: { isDefault: false },
            });
        }
        const variant = await database_1.default.bookVariant.create({
            data: {
                bookId,
                title: input.title || `${book.title} (${input.format})`,
                format: input.format,
                isbn: input.isbn || null,
                sku: input.sku,
                barcode: input.barcode || null,
                price: input.price,
                discountPrice: input.discountPrice ?? null,
                stock: input.stock ?? 0,
                weight: input.weight ?? null,
                dimensions: input.dimensions ?? null,
                coverImage: input.coverImage ?? book.coverImage ?? null,
                digitalFileUrl: input.digitalFileUrl ?? null,
                digitalFormat: input.digitalFormat ?? null,
                attributes: input.attributes ?? undefined,
                isDefault: input.isDefault ?? false,
                isActive: input.isActive ?? true,
            },
        });
        // Update parent book total stock aggregate
        const totalVariantStock = await database_1.default.bookVariant.aggregate({
            where: { bookId, isActive: true },
            _sum: { stock: true },
        });
        if (totalVariantStock._sum.stock !== null) {
            await database_1.default.book.update({
                where: { id: bookId },
                data: { stock: totalVariantStock._sum.stock },
            });
            await database_1.default.bookInventory.upsert({
                where: { bookId },
                create: { bookId, stock: totalVariantStock._sum.stock },
                update: { stock: totalVariantStock._sum.stock },
            });
        }
        return (0, response_utils_1.sendSuccess)(res, 'Book variant created successfully', variant, 201);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to create variant', error.message, 500);
    }
};
exports.createVariant = createVariant;
const updateVariant = async (req, res) => {
    try {
        const { id } = req.params;
        const input = req.body;
        const existing = await database_1.default.bookVariant.findUnique({ where: { id } });
        if (!existing) {
            return (0, response_utils_1.sendError)(res, 'Variant not found', null, 404);
        }
        if (input.sku && input.sku !== existing.sku) {
            const duplicateSku = await database_1.default.bookVariant.findUnique({ where: { sku: input.sku } });
            if (duplicateSku) {
                return (0, response_utils_1.sendError)(res, `Variant SKU '${input.sku}' is already in use`, null, 400);
            }
        }
        if (input.isDefault) {
            await database_1.default.bookVariant.updateMany({
                where: { bookId: existing.bookId, id: { not: id } },
                data: { isDefault: false },
            });
        }
        const updated = await database_1.default.bookVariant.update({
            where: { id },
            data: {
                ...(input.title !== undefined && { title: input.title }),
                ...(input.format !== undefined && { format: input.format }),
                ...(input.isbn !== undefined && { isbn: input.isbn }),
                ...(input.sku !== undefined && { sku: input.sku }),
                ...(input.barcode !== undefined && { barcode: input.barcode }),
                ...(input.price !== undefined && { price: input.price }),
                ...(input.discountPrice !== undefined && { discountPrice: input.discountPrice }),
                ...(input.stock !== undefined && { stock: input.stock }),
                ...(input.weight !== undefined && { weight: input.weight }),
                ...(input.dimensions !== undefined && { dimensions: input.dimensions }),
                ...(input.coverImage !== undefined && { coverImage: input.coverImage }),
                ...(input.digitalFileUrl !== undefined && { digitalFileUrl: input.digitalFileUrl }),
                ...(input.digitalFormat !== undefined && { digitalFormat: input.digitalFormat }),
                ...(input.attributes !== undefined && { attributes: input.attributes }),
                ...(input.isDefault !== undefined && { isDefault: input.isDefault }),
                ...(input.isActive !== undefined && { isActive: input.isActive }),
            },
        });
        // Update parent book total stock aggregate if stock changed
        if (input.stock !== undefined) {
            const totalVariantStock = await database_1.default.bookVariant.aggregate({
                where: { bookId: existing.bookId, isActive: true },
                _sum: { stock: true },
            });
            if (totalVariantStock._sum.stock !== null) {
                await database_1.default.book.update({
                    where: { id: existing.bookId },
                    data: { stock: totalVariantStock._sum.stock },
                });
                await database_1.default.bookInventory.upsert({
                    where: { bookId: existing.bookId },
                    create: { bookId: existing.bookId, stock: totalVariantStock._sum.stock },
                    update: { stock: totalVariantStock._sum.stock },
                });
            }
        }
        return (0, response_utils_1.sendSuccess)(res, 'Book variant updated successfully', updated);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to update variant', error.message, 500);
    }
};
exports.updateVariant = updateVariant;
const deleteVariant = async (req, res) => {
    try {
        const { id } = req.params;
        const existing = await database_1.default.bookVariant.findUnique({ where: { id } });
        if (!existing) {
            return (0, response_utils_1.sendError)(res, 'Variant not found', null, 404);
        }
        await database_1.default.bookVariant.update({
            where: { id },
            data: { isActive: false },
        });
        return (0, response_utils_1.sendSuccess)(res, 'Variant disabled successfully');
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to delete variant', error.message, 500);
    }
};
exports.deleteVariant = deleteVariant;
const updateVariantStock = async (req, res) => {
    try {
        const { id } = req.params;
        const { stock } = req.body;
        const variant = await database_1.default.bookVariant.findUnique({ where: { id } });
        if (!variant) {
            return (0, response_utils_1.sendError)(res, 'Variant not found', null, 404);
        }
        const updated = await database_1.default.bookVariant.update({
            where: { id },
            data: { stock: Number(stock) },
        });
        // Update parent book stock
        const totalVariantStock = await database_1.default.bookVariant.aggregate({
            where: { bookId: variant.bookId, isActive: true },
            _sum: { stock: true },
        });
        if (totalVariantStock._sum.stock !== null) {
            await database_1.default.book.update({
                where: { id: variant.bookId },
                data: { stock: totalVariantStock._sum.stock },
            });
            await database_1.default.bookInventory.upsert({
                where: { bookId: variant.bookId },
                create: { bookId: variant.bookId, stock: totalVariantStock._sum.stock },
                update: { stock: totalVariantStock._sum.stock },
            });
        }
        return (0, response_utils_1.sendSuccess)(res, `Variant stock updated to ${stock}`, updated);
    }
    catch (error) {
        return (0, response_utils_1.sendError)(res, 'Failed to update variant stock', error.message, 500);
    }
};
exports.updateVariantStock = updateVariantStock;
//# sourceMappingURL=variant.controller.js.map