"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 auth_exception_filter_1 = require("../../auth/filters/auth-exception.filter");
const swagger_1 = require("@nestjs/swagger");
const core_1 = require("../../core");
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");
const role_mapper_util_1 = require("../../role/utils/role-mapper.util");
let AdminRolesController = class AdminRolesController {
    roles;
    constructor(roles) {
        this.roles = roles;
    }
    async listRoles(tenantId, guard) {
        const roles = await this.roles.getRoles({
            ...(guard ? { guard } : {}),
            ...(tenantId ? { tenantId } : {}),
            includeTenant: true,
        });
        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, dto.isActive ?? true);
        return { role: this.toSafeRole(role) };
    }
    async updateRole(id, dto) {
        if (dto.permissions === undefined
            && dto.name === undefined
            && dto.isActive === undefined) {
            throw new common_1.BadRequestException('At least one field must be provided for update (name, isActive, or permissions)');
        }
        const updateData = {};
        if (dto.name !== undefined)
            updateData.name = dto.name;
        if (dto.isActive !== undefined)
            updateData.isActive = dto.isActive;
        if (dto.permissions !== undefined)
            updateData.permissions = dto.permissions;
        const role = await this.roles.updateRole(id, updateData);
        return { role: this.toSafeRole(role) };
    }
    async deleteRole(id) {
        await this.roles.deleteRole(id);
        return { message: 'Role removed' };
    }
    toSafeRole(role) {
        return (0, role_mapper_util_1.mapRoleToResponse)(role);
    }
};
exports.AdminRolesController = AdminRolesController;
__decorate([
    (0, swagger_1.ApiOperation)({ summary: 'List roles' }),
    (0, common_1.Get)(),
    __param(0, (0, common_1.Query)('tenantId')),
    __param(1, (0, common_1.Query)('guard')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], AdminRolesController.prototype, "listRoles", null);
__decorate([
    (0, swagger_1.ApiOperation)({ summary: 'Create a role' }),
    (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, swagger_1.ApiOperation)({ summary: 'Update a role' }),
    (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, swagger_1.ApiOperation)({ summary: 'Delete a role' }),
    (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)('api/roles'),
    (0, common_1.UseFilters)(auth_exception_filter_1.AuthExceptionFilter),
    (0, common_1.UseGuards)(admin_session_guard_1.AdminSessionGuard),
    (0, swagger_1.ApiTags)('Admin · Roles'),
    (0, swagger_1.ApiCookieAuth)('admin-session'),
    (0, core_1.ApiUnauthorized)('Admin session missing or invalid.'),
    (0, core_1.ApiForbidden)(),
    (0, core_1.ApiValidationError)(),
    (0, core_1.ApiNotFoundError)('Role not found.'),
    (0, core_1.Public)(),
    __metadata("design:paramtypes", [role_service_1.RoleService])
], AdminRolesController);
//# sourceMappingURL=admin-roles.controller.js.map