"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.FeatureAccessController = void 0;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const client_1 = require("@prisma/client");
const require_role_decorator_1 = require("../auth/require-role.decorator");
const current_user_decorator_1 = require("../common/current-user.decorator");
const swagger_docs_1 = require("../common/swagger-docs");
const feature_access_dto_1 = require("./dto/feature-access.dto");
const feature_access_service_1 = require("./feature-access.service");
const feature_catalog_1 = require("./feature-catalog");
const require_feature_decorator_1 = require("./require-feature.decorator");
const ALL_ROLES = [
    client_1.UserRole.SUPER_ADMIN,
    client_1.UserRole.SALES_ADMIN,
    client_1.UserRole.ESTATE_ADMIN,
    client_1.UserRole.SECURITY_SUPERVISOR,
    client_1.UserRole.GUARD,
    client_1.UserRole.RESIDENT
];
let FeatureAccessController = class FeatureAccessController {
    featureAccess;
    constructor(featureAccess) {
        this.featureAccess = featureAccess;
    }
    listCatalog() {
        return this.featureAccess.listCatalog();
    }
    me(user) {
        return this.featureAccess.getMyFeatures(user);
    }
    getRoleSettings(role, estateId, user) {
        return this.featureAccess.getManageableRoleSettings(role, estateId, user);
    }
    updateRoleSettings(role, dto, user) {
        return this.featureAccess.updateRoleSettings(role, dto, user);
    }
    getPlanSettings(code) {
        return this.featureAccess.getPlanSettings(code);
    }
    updatePlanSettings(code, dto, user) {
        return this.featureAccess.updatePlanSettings(code, dto, user);
    }
};
exports.FeatureAccessController = FeatureAccessController;
__decorate([
    (0, common_1.Get)('catalog'),
    (0, require_role_decorator_1.RequireRole)(...ALL_ROLES),
    (0, swagger_1.ApiOperation)({
        summary: 'List feature catalog',
        description: 'Returns the stable feature keys, labels, categories, and default roles used by WebApp and MobileApp permission screens.'
    }),
    (0, swagger_1.ApiResponse)({
        status: 200,
        description: 'Feature catalog returned.',
        schema: {
            example: [
                {
                    key: feature_catalog_1.FeatureKey.VISITOR_PASSES_CREATE,
                    label: 'Create visitor passes',
                    category: 'Visitors',
                    defaultRoles: ['SUPER_ADMIN', 'ESTATE_ADMIN', 'RESIDENT'],
                    estateConfigurable: true
                }
            ]
        }
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], FeatureAccessController.prototype, "listCatalog", null);
__decorate([
    (0, common_1.Get)('me'),
    (0, require_role_decorator_1.RequireRole)(...ALL_ROLES),
    (0, swagger_1.ApiOperation)({
        summary: 'List current user effective features',
        description: 'Returns the current user role features after applying global defaults, global overrides, and estate overrides.'
    }),
    (0, swagger_1.ApiResponse)({
        status: 200,
        description: 'Effective feature settings returned.',
        schema: {
            example: [
                {
                    key: feature_catalog_1.FeatureKey.CHECKIN_SCAN,
                    role: 'GUARD',
                    estateId: swagger_docs_1.ESTATE_ID_EXAMPLE,
                    defaultEnabled: true,
                    globalEnabled: true,
                    estateEnabled: null,
                    effectiveEnabled: true,
                    source: 'DEFAULT'
                }
            ]
        }
    }),
    __param(0, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], FeatureAccessController.prototype, "me", null);
__decorate([
    (0, common_1.Get)('role-settings'),
    (0, require_role_decorator_1.RequireRole)(client_1.UserRole.SUPER_ADMIN, client_1.UserRole.ESTATE_ADMIN),
    (0, require_feature_decorator_1.RequireFeature)(feature_catalog_1.FeatureKey.FEATURE_SETTINGS_MANAGE),
    (0, swagger_1.ApiOperation)({
        summary: 'Inspect role feature settings',
        description: 'SUPER_ADMIN can inspect any global or estate role settings. ESTATE_ADMIN can inspect only resident or guard settings for their own estate.'
    }),
    (0, swagger_1.ApiQuery)({ name: 'role', enum: client_1.UserRole, example: client_1.UserRole.RESIDENT }),
    (0, swagger_1.ApiQuery)({ name: 'estateId', required: false, example: swagger_docs_1.ESTATE_ID_EXAMPLE }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Role feature settings returned.' }),
    __param(0, (0, common_1.Query)('role')),
    __param(1, (0, common_1.Query)('estateId')),
    __param(2, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object, Object]),
    __metadata("design:returntype", void 0)
], FeatureAccessController.prototype, "getRoleSettings", null);
__decorate([
    (0, common_1.Patch)('role-settings/:role'),
    (0, require_role_decorator_1.RequireRole)(client_1.UserRole.SUPER_ADMIN, client_1.UserRole.ESTATE_ADMIN),
    (0, require_feature_decorator_1.RequireFeature)(feature_catalog_1.FeatureKey.FEATURE_SETTINGS_MANAGE),
    (0, swagger_1.ApiOperation)({
        summary: 'Update role feature settings',
        description: 'SUPER_ADMIN can update global settings for any role or estate overrides when estateId is supplied. ESTATE_ADMIN can update only RESIDENT and GUARD settings for their own estate.'
    }),
    (0, swagger_1.ApiParam)({ name: 'role', enum: client_1.UserRole, example: client_1.UserRole.GUARD }),
    (0, swagger_1.ApiBody)({
        type: feature_access_dto_1.UpdateRoleFeatureSettingsDto,
        examples: {
            disableGuardManualCheckin: {
                summary: 'Disable manual check-in for guards in one estate',
                value: {
                    estateId: swagger_docs_1.ESTATE_ID_EXAMPLE,
                    settings: [{ feature: feature_catalog_1.FeatureKey.CHECKIN_MANUAL, enabled: false }]
                }
            },
            enableSalesAdminPipeline: {
                summary: 'Enable sales admin signup review globally',
                value: {
                    settings: [
                        { feature: feature_catalog_1.FeatureKey.ESTATE_SIGNUPS_VIEW, enabled: true },
                        { feature: feature_catalog_1.FeatureKey.ESTATE_SIGNUPS_REVIEW, enabled: true }
                    ]
                }
            }
        }
    }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Role feature settings updated.' }),
    (0, swagger_docs_1.ApiValidationError)(),
    __param(0, (0, common_1.Param)('role')),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, feature_access_dto_1.UpdateRoleFeatureSettingsDto, Object]),
    __metadata("design:returntype", void 0)
], FeatureAccessController.prototype, "updateRoleSettings", null);
__decorate([
    (0, common_1.Get)('plan-settings/:code'),
    (0, require_role_decorator_1.RequireRole)(client_1.UserRole.SUPER_ADMIN),
    (0, require_feature_decorator_1.RequireFeature)(feature_catalog_1.FeatureKey.FEATURE_SETTINGS_MANAGE),
    (0, swagger_1.ApiOperation)({
        summary: 'Inspect plan feature settings',
        description: 'SUPER_ADMIN view of every feature and whether it is enabled for a subscription plan.'
    }),
    (0, swagger_1.ApiParam)({ name: 'code', example: 'SECURE' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Plan feature settings returned.' }),
    __param(0, (0, common_1.Param)('code')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", void 0)
], FeatureAccessController.prototype, "getPlanSettings", null);
__decorate([
    (0, common_1.Patch)('plan-settings/:code'),
    (0, require_role_decorator_1.RequireRole)(client_1.UserRole.SUPER_ADMIN),
    (0, require_feature_decorator_1.RequireFeature)(feature_catalog_1.FeatureKey.FEATURE_SETTINGS_MANAGE),
    (0, swagger_1.ApiOperation)({
        summary: 'Update plan feature settings',
        description: 'SUPER_ADMIN can activate or deactivate any feature for a subscription plan. Enabled features become available to plan-gated estate roles.'
    }),
    (0, swagger_1.ApiParam)({ name: 'code', example: 'SECURE' }),
    (0, swagger_1.ApiBody)({
        type: feature_access_dto_1.UpdatePlanFeatureSettingsDto,
        examples: {
            enableLuxuryRiskScoring: {
                summary: 'Enable visitor risk scoring',
                value: { settings: [{ feature: feature_catalog_1.FeatureKey.VISITOR_RISK_SCORING, enabled: true }] }
            },
            disableStarterExports: {
                summary: 'Disable exports',
                value: { settings: [{ feature: feature_catalog_1.FeatureKey.EXPORT_REPORTS, enabled: false }] }
            }
        }
    }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Plan feature settings updated.' }),
    (0, swagger_docs_1.ApiValidationError)(),
    __param(0, (0, common_1.Param)('code')),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, current_user_decorator_1.CurrentUser)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, feature_access_dto_1.UpdatePlanFeatureSettingsDto, Object]),
    __metadata("design:returntype", void 0)
], FeatureAccessController.prototype, "updatePlanSettings", null);
exports.FeatureAccessController = FeatureAccessController = __decorate([
    (0, swagger_1.ApiTags)('Feature Access'),
    (0, swagger_docs_1.ApiProtectedEndpoint)(),
    (0, common_1.Controller)({ path: 'features', version: '1' }),
    __metadata("design:paramtypes", [feature_access_service_1.FeatureAccessService])
], FeatureAccessController);
//# sourceMappingURL=feature-access.controller.js.map