"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.FeatureFlagsController = void 0;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const feature_flags_service_1 = require("./services/feature-flags.service");
const create_flag_dto_1 = require("./dto/create-flag.dto");
const update_flag_dto_1 = require("./dto/update-flag.dto");
const set_tenant_flag_dto_1 = require("./dto/set-tenant-flag.dto");
const guards_1 = require("../auth/guards");
const decorators_1 = require("../auth/decorators");
let FeatureFlagsController = class FeatureFlagsController {
    constructor(featureFlagsService) {
        this.featureFlagsService = featureFlagsService;
    }
    async createFlag(dto) {
        return this.featureFlagsService.createFlag(dto);
    }
    async getAllFlags() {
        return this.featureFlagsService.getAllFlags();
    }
    async getFlagById(id) {
        return this.featureFlagsService.getFlagById(id);
    }
    async updateFlag(id, dto) {
        return this.featureFlagsService.updateFlag(id, dto);
    }
    async deleteFlag(id) {
        await this.featureFlagsService.deleteFlag(id);
        return { success: true };
    }
    async toggleFlag(id, enabled) {
        return this.featureFlagsService.toggleFlag(id, enabled === 'true');
    }
    async getTenantFlags(user) {
        return this.featureFlagsService.getTenantFlags(user.tenant_id);
    }
    async setTenantFlag(user, dto) {
        return this.featureFlagsService.setTenantFlag(user.tenant_id, dto);
    }
    async removeTenantFlag(user, flagId) {
        await this.featureFlagsService.removeTenantFlag(user.tenant_id, flagId);
        return { success: true };
    }
    async getUserFlags(user, userId) {
        return this.featureFlagsService.getUserFlags(user.tenant_id, userId);
    }
    async setUserFlag(user, dto) {
        return this.featureFlagsService.setUserFlag(user.tenant_id, dto);
    }
    async removeUserFlag(userId, flagId) {
        await this.featureFlagsService.removeUserFlag(userId, flagId);
        return { success: true };
    }
    async evaluateFlag(user, key) {
        const context = {
            tenantId: user.tenant_id,
            userId: user.id,
        };
        return this.featureFlagsService.evaluateFlag(key, context);
    }
    async evaluateAllFlags(user) {
        const context = {
            tenantId: user.tenant_id,
            userId: user.id,
        };
        return this.featureFlagsService.evaluateAllFlags(context);
    }
    async isEnabled(user, key) {
        const context = {
            tenantId: user.tenant_id,
            userId: user.id,
        };
        const enabled = await this.featureFlagsService.isEnabled(key, context);
        return { key, enabled };
    }
};
exports.FeatureFlagsController = FeatureFlagsController;
__decorate([
    (0, common_1.Post)(),
    (0, swagger_1.ApiOperation)({ summary: 'Create a new feature flag' }),
    (0, swagger_1.ApiResponse)({ status: 201, description: 'Flag created successfully' }),
    (0, swagger_1.ApiResponse)({ status: 409, description: 'Flag with this key already exists' }),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [create_flag_dto_1.CreateFlagDto]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "createFlag", null);
__decorate([
    (0, common_1.Get)(),
    (0, swagger_1.ApiOperation)({ summary: 'Get all feature flags' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'List of all flags' }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "getAllFlags", null);
__decorate([
    (0, common_1.Get)(':id'),
    (0, swagger_1.ApiOperation)({ summary: 'Get flag by ID' }),
    (0, swagger_1.ApiParam)({ name: 'id', description: 'Flag ID' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Flag details' }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Flag not found' }),
    __param(0, (0, common_1.Param)('id')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "getFlagById", null);
__decorate([
    (0, common_1.Put)(':id'),
    (0, swagger_1.ApiOperation)({ summary: 'Update a feature flag' }),
    (0, swagger_1.ApiParam)({ name: 'id', description: 'Flag ID' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Flag updated successfully' }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Flag not found' }),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, update_flag_dto_1.UpdateFlagDto]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "updateFlag", null);
__decorate([
    (0, common_1.Delete)(':id'),
    (0, swagger_1.ApiOperation)({ summary: 'Delete a feature flag' }),
    (0, swagger_1.ApiParam)({ name: 'id', description: 'Flag ID' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Flag deleted successfully' }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Flag not found' }),
    __param(0, (0, common_1.Param)('id')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "deleteFlag", null);
__decorate([
    (0, common_1.Post)(':id/toggle'),
    (0, swagger_1.ApiOperation)({ summary: 'Toggle flag enabled state' }),
    (0, swagger_1.ApiParam)({ name: 'id', description: 'Flag ID' }),
    (0, swagger_1.ApiQuery)({ name: 'enabled', type: 'boolean', required: true }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Flag toggled successfully' }),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Query)('enabled')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "toggleFlag", null);
__decorate([
    (0, common_1.Get)('tenant/overrides'),
    (0, swagger_1.ApiOperation)({ summary: 'Get all flag overrides for current tenant' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'List of tenant flag overrides' }),
    __param(0, (0, decorators_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "getTenantFlags", null);
__decorate([
    (0, common_1.Post)('tenant/override'),
    (0, swagger_1.ApiOperation)({ summary: 'Set flag override for current tenant' }),
    (0, swagger_1.ApiResponse)({ status: 201, description: 'Tenant flag set successfully' }),
    __param(0, (0, decorators_1.CurrentUser)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, set_tenant_flag_dto_1.SetTenantFlagDto]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "setTenantFlag", null);
__decorate([
    (0, common_1.Delete)('tenant/override/:flagId'),
    (0, swagger_1.ApiOperation)({ summary: 'Remove flag override for current tenant' }),
    (0, swagger_1.ApiParam)({ name: 'flagId', description: 'Flag ID' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Tenant flag removed successfully' }),
    __param(0, (0, decorators_1.CurrentUser)()),
    __param(1, (0, common_1.Param)('flagId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "removeTenantFlag", null);
__decorate([
    (0, common_1.Get)('user/:userId/overrides'),
    (0, swagger_1.ApiOperation)({ summary: 'Get all flag overrides for a user' }),
    (0, swagger_1.ApiParam)({ name: 'userId', description: 'User ID' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'List of user flag overrides' }),
    __param(0, (0, decorators_1.CurrentUser)()),
    __param(1, (0, common_1.Param)('userId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "getUserFlags", null);
__decorate([
    (0, common_1.Post)('user/override'),
    (0, swagger_1.ApiOperation)({ summary: 'Set flag override for a user' }),
    (0, swagger_1.ApiResponse)({ status: 201, description: 'User flag set successfully' }),
    __param(0, (0, decorators_1.CurrentUser)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, set_tenant_flag_dto_1.SetUserFlagDto]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "setUserFlag", null);
__decorate([
    (0, common_1.Delete)('user/:userId/override/:flagId'),
    (0, swagger_1.ApiOperation)({ summary: 'Remove flag override for a user' }),
    (0, swagger_1.ApiParam)({ name: 'userId', description: 'User ID' }),
    (0, swagger_1.ApiParam)({ name: 'flagId', description: 'Flag ID' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'User flag removed successfully' }),
    __param(0, (0, common_1.Param)('userId')),
    __param(1, (0, common_1.Param)('flagId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "removeUserFlag", null);
__decorate([
    (0, common_1.Get)('evaluate/:key'),
    (0, swagger_1.ApiOperation)({ summary: 'Evaluate a single flag for current context' }),
    (0, swagger_1.ApiParam)({ name: 'key', description: 'Flag key' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Flag evaluation result' }),
    __param(0, (0, decorators_1.CurrentUser)()),
    __param(1, (0, common_1.Param)('key')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "evaluateFlag", null);
__decorate([
    (0, common_1.Get)('evaluate'),
    (0, swagger_1.ApiOperation)({ summary: 'Evaluate all flags for current context' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'All flag evaluations' }),
    __param(0, (0, decorators_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "evaluateAllFlags", null);
__decorate([
    (0, common_1.Get)('check/:key'),
    (0, swagger_1.ApiOperation)({ summary: 'Quick check if flag is enabled' }),
    (0, swagger_1.ApiParam)({ name: 'key', description: 'Flag key' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Boolean enabled status' }),
    __param(0, (0, decorators_1.CurrentUser)()),
    __param(1, (0, common_1.Param)('key')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], FeatureFlagsController.prototype, "isEnabled", null);
exports.FeatureFlagsController = FeatureFlagsController = __decorate([
    (0, swagger_1.ApiTags)('Feature Flags'),
    (0, swagger_1.ApiBearerAuth)(),
    (0, common_1.UseGuards)(guards_1.JwtAuthGuard),
    (0, common_1.Controller)('feature-flags'),
    __metadata("design:paramtypes", [feature_flags_service_1.FeatureFlagsService])
], FeatureFlagsController);
//# sourceMappingURL=feature-flags.controller.js.map