"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.UsersController = void 0;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const users_service_1 = require("./users.service");
const invitation_service_1 = require("./services/invitation.service");
const jwt_auth_guard_1 = require("../auth/guards/jwt-auth.guard");
const current_user_decorator_1 = require("../auth/decorators/current-user.decorator");
const invite_user_dto_1 = require("./dto/invite-user.dto");
let UsersController = class UsersController {
    constructor(usersService, invitationService) {
        this.usersService = usersService;
        this.invitationService = invitationService;
    }
    async findAll(user, page = 1, limit = 10) {
        return this.usersService.findAllByTenant(user.tenant_id, page, limit);
    }
    async findOne(id, user) {
        return this.usersService.findOne(id, user.tenant_id);
    }
    async update(id, updateDto, user) {
        return this.usersService.update(id, updateDto, user.tenant_id);
    }
    async invite(inviteDto, user) {
        return this.invitationService.invite(inviteDto, user.id, user.tenant_id);
    }
    async listInvitations(user) {
        return this.invitationService.findAllByTenant(user.tenant_id);
    }
    async resendInvitation(token, user) {
        return this.invitationService.resend(token, user.id, user.tenant_id);
    }
    async cancelInvitation(id, user) {
        return this.invitationService.cancel(id, user.tenant_id);
    }
};
exports.UsersController = UsersController;
__decorate([
    (0, common_1.Get)(),
    (0, swagger_1.ApiOperation)({ summary: 'List users in tenant' }),
    __param(0, (0, current_user_decorator_1.CurrentUser)()),
    __param(1, (0, common_1.Query)('page')),
    __param(2, (0, common_1.Query)('limit')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, Object]),
    __metadata("design:returntype", Promise)
], UsersController.prototype, "findAll", null);
__decorate([
    (0, common_1.Get)(':id'),
    (0, swagger_1.ApiOperation)({ summary: 'Get user by ID' }),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], UsersController.prototype, "findOne", null);
__decorate([
    (0, common_1.Patch)(':id'),
    (0, swagger_1.ApiOperation)({ summary: 'Update user' }),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object, Object]),
    __metadata("design:returntype", Promise)
], UsersController.prototype, "update", null);
__decorate([
    (0, common_1.Post)('invite'),
    (0, swagger_1.ApiOperation)({ summary: 'Send invitation to join tenant' }),
    (0, swagger_1.ApiResponse)({ status: 201, description: 'Invitation sent successfully', type: invite_user_dto_1.InvitationResponseDto }),
    (0, swagger_1.ApiResponse)({ status: 409, description: 'Email already registered or invitation pending' }),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [invite_user_dto_1.InviteUserDto, Object]),
    __metadata("design:returntype", Promise)
], UsersController.prototype, "invite", null);
__decorate([
    (0, common_1.Get)('invitations'),
    (0, swagger_1.ApiOperation)({ summary: 'List pending invitations for tenant' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'List of invitations', type: [invite_user_dto_1.InvitationResponseDto] }),
    __param(0, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], UsersController.prototype, "listInvitations", null);
__decorate([
    (0, common_1.Post)('invitations/:token/resend'),
    (0, swagger_1.ApiOperation)({ summary: 'Resend invitation email' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Invitation resent successfully', type: invite_user_dto_1.InvitationResponseDto }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Invitation not found' }),
    (0, swagger_1.ApiResponse)({ status: 400, description: 'Only pending invitations can be resent' }),
    __param(0, (0, common_1.Param)('token')),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], UsersController.prototype, "resendInvitation", null);
__decorate([
    (0, common_1.Delete)('invitations/:id'),
    (0, common_1.HttpCode)(common_1.HttpStatus.NO_CONTENT),
    (0, swagger_1.ApiOperation)({ summary: 'Cancel pending invitation' }),
    (0, swagger_1.ApiResponse)({ status: 204, description: 'Invitation cancelled' }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Invitation not found' }),
    (0, swagger_1.ApiResponse)({ status: 400, description: 'Only pending invitations can be cancelled' }),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], UsersController.prototype, "cancelInvitation", null);
exports.UsersController = UsersController = __decorate([
    (0, swagger_1.ApiTags)('users'),
    (0, common_1.Controller)('users'),
    (0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard),
    (0, swagger_1.ApiBearerAuth)(),
    __metadata("design:paramtypes", [users_service_1.UsersService,
        invitation_service_1.InvitationService])
], UsersController);
//# sourceMappingURL=users.controller.js.map