"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.AdminTenantsController = void 0;
const common_1 = require("@nestjs/common");
const admin_session_guard_1 = require("../guards/admin-session.guard");
const tenant_service_1 = require("../../tenant/services/tenant.service");
const admin_tenant_dto_1 = require("../dto/admin-tenant.dto");
let AdminTenantsController = class AdminTenantsController {
    tenants;
    constructor(tenants) {
        this.tenants = tenants;
    }
    async listTenants() {
        const tenants = await this.tenants.getTenants({ order: { createdAt: 'DESC' } });
        return {
            data: tenants.map((tenant) => this.toSafeTenant(tenant)),
        };
    }
    async createTenant(dto) {
        const tenant = await this.tenants.createTenant({
            name: dto.name,
            slug: dto.slug,
            description: dto.description,
            metadata: dto.metadata ?? {},
        });
        return { tenant: this.toSafeTenant(tenant) };
    }
    async updateTenant(id, dto) {
        const tenant = await this.tenants.updateTenant(id, {
            name: dto.name,
            slug: dto.slug,
            description: dto.description,
            metadata: dto.metadata ?? {},
        });
        return { tenant: this.toSafeTenant(tenant) };
    }
    async deleteTenant(id) {
        await this.tenants.deleteTenant(id);
        return { message: 'Tenant removed' };
    }
    toSafeTenant(tenant) {
        return {
            id: tenant.id,
            name: tenant.name,
            slug: tenant.slug,
            description: tenant.description,
            metadata: tenant.metadata ?? {},
            createdAt: tenant.createdAt,
            updatedAt: tenant.updatedAt,
        };
    }
};
exports.AdminTenantsController = AdminTenantsController;
__decorate([
    (0, common_1.Get)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], AdminTenantsController.prototype, "listTenants", null);
__decorate([
    (0, common_1.Post)(),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [admin_tenant_dto_1.AdminCreateTenantDto]),
    __metadata("design:returntype", Promise)
], AdminTenantsController.prototype, "createTenant", 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_tenant_dto_1.AdminUpdateTenantDto]),
    __metadata("design:returntype", Promise)
], AdminTenantsController.prototype, "updateTenant", 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)
], AdminTenantsController.prototype, "deleteTenant", null);
exports.AdminTenantsController = AdminTenantsController = __decorate([
    (0, common_1.Controller)('auth/admin/api/tenants'),
    (0, common_1.UseGuards)(admin_session_guard_1.AdminSessionGuard),
    __metadata("design:paramtypes", [tenant_service_1.TenantService])
], AdminTenantsController);
//# sourceMappingURL=admin-tenants.controller.js.map