"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.AdminRolesController = void 0;
const common_1 = require("@nestjs/common");
const admin_session_guard_1 = require("../guards/admin-session.guard");
const role_service_1 = require("../../role/services/role.service");
const admin_role_dto_1 = require("../dto/admin-role.dto");
const auth_constants_1 = require("../../auth.constants");
let AdminRolesController = class AdminRolesController {
    roles;
    constructor(roles) {
        this.roles = roles;
    }
    async listRoles() {
        const roles = await this.roles.getRoles();
        return {
            data: roles.map((role) => this.toSafeRole(role)),
        };
    }
    async createRole(dto) {
        const role = await this.roles.createRole(dto.name, dto.guard ?? auth_constants_1.DEFAULT_GUARD_NAME, dto.tenantId, dto.isSystem ?? false, dto.permissions);
        return { role: this.toSafeRole(role) };
    }
    async updateRole(id, dto) {
        if (!dto.permissions && !dto.name && !dto.guard) {
            throw new common_1.BadRequestException('At least one field must be provided for update (permissions, name, or guard)');
        }
        if (dto.permissions) {
            await this.roles.updateRolePermissions(id, dto.permissions);
        }
        if (dto.name || dto.guard) {
            await this.roles.updateRole(id, {
                name: dto.name,
                guard: dto.guard,
            });
        }
        const role = await this.roles.getRoleById(id);
        return { role: this.toSafeRole(role) };
    }
    async deleteRole(id) {
        await this.roles.deleteRole(id);
        return { message: 'Role removed' };
    }
    toSafeRole(role) {
        return {
            id: role.id,
            name: role.name,
            guard: role.guard,
            isSystem: role.isSystem,
            tenantId: role.tenantId,
            permissions: role.permissions ?? [],
            createdAt: role.createdAt,
            updatedAt: role.updatedAt,
        };
    }
};
exports.AdminRolesController = AdminRolesController;
__decorate([
    (0, common_1.Get)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], AdminRolesController.prototype, "listRoles", null);
__decorate([
    (0, common_1.Post)(),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [admin_role_dto_1.AdminCreateRoleDto]),
    __metadata("design:returntype", Promise)
], AdminRolesController.prototype, "createRole", null);
__decorate([
    (0, common_1.Patch)(':id'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, admin_role_dto_1.AdminUpdateRoleDto]),
    __metadata("design:returntype", Promise)
], AdminRolesController.prototype, "updateRole", null);
__decorate([
    (0, common_1.Delete)(':id'),
    __param(0, (0, common_1.Param)('id')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], AdminRolesController.prototype, "deleteRole", null);
exports.AdminRolesController = AdminRolesController = __decorate([
    (0, common_1.Controller)('auth/admin/api/roles'),
    (0, common_1.UseGuards)(admin_session_guard_1.AdminSessionGuard),
    __metadata("design:paramtypes", [role_service_1.RoleService])
], AdminRolesController);
//# sourceMappingURL=admin-roles.controller.js.map