"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.blocksService = void 0;
const database_1 = require("../../config/database");
const error_handler_1 = require("../../middleware/error-handler");
const blockSelect = {
    id: true,
    type: true,
    label: true,
    schema: true,
    defaults: true,
    thumbnail: true,
    category: true,
    sortOrder: true,
    createdAt: true,
    updatedAt: true,
};
exports.blocksService = {
    async findAll(filters) {
        const where = {};
        if (filters.category) {
            where.category = filters.category;
        }
        return database_1.prisma.pageBlock.findMany({
            where,
            select: blockSelect,
            orderBy: { sortOrder: 'asc' },
        });
    },
    async findById(id) {
        const block = await database_1.prisma.pageBlock.findUnique({
            where: { id },
            select: blockSelect,
        });
        if (!block) {
            throw new error_handler_1.AppError(404, 'Page block not found', 'BLOCK_NOT_FOUND');
        }
        return block;
    },
    async create(data) {
        return database_1.prisma.pageBlock.create({
            data: {
                ...data,
                schema: data.schema,
                defaults: data.defaults,
            },
            select: blockSelect,
        });
    },
    async update(id, data) {
        const existing = await database_1.prisma.pageBlock.findUnique({ where: { id } });
        if (!existing) {
            throw new error_handler_1.AppError(404, 'Page block not found', 'BLOCK_NOT_FOUND');
        }
        const updateData = { ...data };
        if (data.schema !== undefined) {
            updateData.schema = data.schema;
        }
        if (data.defaults !== undefined) {
            updateData.defaults = data.defaults;
        }
        return database_1.prisma.pageBlock.update({
            where: { id },
            data: updateData,
            select: blockSelect,
        });
    },
    async delete(id) {
        const existing = await database_1.prisma.pageBlock.findUnique({ where: { id } });
        if (!existing) {
            throw new error_handler_1.AppError(404, 'Page block not found', 'BLOCK_NOT_FOUND');
        }
        await database_1.prisma.pageBlock.delete({ where: { id } });
    },
};
//# sourceMappingURL=blocks.service.js.map