"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DashboardComponentRoleService = void 0;
const api_locale_1 = require("@hed-hog/api-locale");
const api_pagination_1 = require("@hed-hog/api-pagination");
const api_prisma_1 = require("@hed-hog/api-prisma");
const common_1 = require("@nestjs/common");
let DashboardComponentRoleService = class DashboardComponentRoleService {
    constructor(prismaService, paginationService) {
        this.prismaService = prismaService;
        this.paginationService = paginationService;
    }
    async getAll(paginationParams, locale) {
        return this.paginationService.paginate(this.prismaService.dashboard_component_role, paginationParams, {
            include: {
                dashboard_component: {
                    include: {
                        dashboard_component_locale: {
                            include: {
                                locale: true,
                            },
                        },
                    },
                },
                role: {
                    include: {
                        role_locale: {
                            include: {
                                locale: true,
                            },
                        },
                    },
                },
            },
            orderBy: {
                id: 'desc',
            },
        }, 'dashboardComponentRole');
    }
    async getAllByComponent(componentId, locale) {
        const relations = await this.prismaService.dashboard_component_role.findMany({
            where: { component_id: componentId },
            include: {
                role: {
                    include: {
                        role_locale: {
                            include: {
                                locale: true,
                            },
                        },
                    },
                },
            },
        });
        return relations;
    }
    async create(data, locale) {
        // Verificar se a relação já existe
        const existing = await this.prismaService.dashboard_component_role.findFirst({
            where: {
                component_id: data.component_id,
                role_id: data.role_id,
            },
        });
        if (existing) {
            throw new Error((0, api_locale_1.getLocaleText)('dashboardComponentRoleAlreadyExists', locale, 'Dashboard component role already exists'));
        }
        return this.prismaService.dashboard_component_role.create({
            data: {
                component_id: data.component_id,
                role_id: data.role_id,
            },
            include: {
                dashboard_component: {
                    include: {
                        dashboard_component_locale: {
                            include: {
                                locale: true,
                            },
                        },
                    },
                },
                role: {
                    include: {
                        role_locale: {
                            include: {
                                locale: true,
                            },
                        },
                    },
                },
            },
        });
    }
    async createBatch(data, locale) {
        const uniqueRoleIds = [...new Set(data.role_ids)];
        const existingRelations = await this.prismaService.dashboard_component_role.findMany({
            where: {
                component_id: data.component_id,
                role_id: {
                    in: uniqueRoleIds,
                },
            },
            select: {
                role_id: true,
            },
        });
        const existingRoleIds = new Set(existingRelations.map((item) => item.role_id));
        const roleIdsToCreate = uniqueRoleIds.filter((roleId) => !existingRoleIds.has(roleId));
        if (roleIdsToCreate.length > 0) {
            await this.prismaService.dashboard_component_role.createMany({
                data: roleIdsToCreate.map((roleId) => ({
                    component_id: data.component_id,
                    role_id: roleId,
                })),
                skipDuplicates: true,
            });
        }
        return {
            success: true,
            created: roleIdsToCreate.length,
            skipped: uniqueRoleIds.length - roleIdsToCreate.length,
            message: (0, api_locale_1.getLocaleText)('dashboardComponentRoleBatchCreated', locale, 'Dashboard component roles processed successfully'),
        };
    }
    async delete(id, locale) {
        const relation = await this.prismaService.dashboard_component_role.findUnique({
            where: { id },
        });
        if (!relation) {
            throw new common_1.NotFoundException((0, api_locale_1.getLocaleText)('dashboardComponentRoleNotFound', locale, 'Dashboard component role not found'));
        }
        await this.prismaService.dashboard_component_role.delete({
            where: { id },
        });
        return { success: true };
    }
    async deleteByComponentAndRole(componentId, roleId, locale) {
        const relation = await this.prismaService.dashboard_component_role.findFirst({
            where: {
                component_id: componentId,
                role_id: roleId,
            },
        });
        if (!relation) {
            throw new common_1.NotFoundException((0, api_locale_1.getLocaleText)('dashboardComponentRoleNotFound', locale, 'Dashboard component role not found'));
        }
        await this.prismaService.dashboard_component_role.delete({
            where: { id: relation.id },
        });
        return { success: true };
    }
};
exports.DashboardComponentRoleService = DashboardComponentRoleService;
exports.DashboardComponentRoleService = DashboardComponentRoleService = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, common_1.Inject)((0, common_1.forwardRef)(() => api_prisma_1.PrismaService))),
    __param(1, (0, common_1.Inject)((0, common_1.forwardRef)(() => api_pagination_1.PaginationService))),
    __metadata("design:paramtypes", [api_prisma_1.PrismaService,
        api_pagination_1.PaginationService])
], DashboardComponentRoleService);
//# sourceMappingURL=dashboard-component-role.service.js.map