"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.AuditController = void 0;
const common_1 = require("@nestjs/common");
const audit_service_1 = require("./audit.service");
const jwt_auth_guard_1 = require("../common/guard/jwt-auth.guard");
const tenant_guard_1 = require("../common/guard/tenant.guard");
const roles_guard_1 = require("../common/guard/roles.guard");
const roles_decorator_1 = require("../common/decorators/roles.decorator");
let AuditController = class AuditController {
    constructor(auditService) {
        this.auditService = auditService;
    }
    async getLogs(businessId, userId, action, entityType, entityId, startDate, endDate, page, limit, req) {
        const userBusinessId = req.businessId || req.user?.businessId;
        const userRole = req.user?.role;
        const isSuperAdmin = userRole === 'super_admin';
        const filterBusinessId = isSuperAdmin ? (businessId || undefined) : userBusinessId;
        return this.auditService.getAuditLogs({
            businessId: filterBusinessId,
            userId: userId || undefined,
            action: action || undefined,
            entityType: entityType || undefined,
            entityId: entityId || undefined,
            startDate: startDate || undefined,
            endDate: endDate || undefined,
            page: page ? parseInt(page, 10) : undefined,
            limit: limit ? parseInt(limit, 10) : undefined,
        });
    }
    async getEntityHistory(entityType, entityId) {
        return this.auditService.getEntityHistory(entityType, entityId);
    }
    async getApiLogs(businessId, method, url, statusCode, startDate, endDate, page, limit, req) {
        const userBusinessId = req.businessId || req.user?.businessId;
        const userRole = req.user?.role;
        const isSuperAdmin = userRole === 'super_admin';
        const filterBusinessId = isSuperAdmin ? (businessId || undefined) : userBusinessId;
        return this.auditService.getApiLogs({
            businessId: filterBusinessId,
            method: method || undefined,
            url: url || undefined,
            statusCode: statusCode ? parseInt(statusCode, 10) : undefined,
            startDate: startDate || undefined,
            endDate: endDate || undefined,
            page: page ? parseInt(page, 10) : undefined,
            limit: limit ? parseInt(limit, 10) : undefined,
        });
    }
};
exports.AuditController = AuditController;
__decorate([
    (0, common_1.Get)('logs'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('business_owner', 'business_admin', 'super_admin'),
    __param(0, (0, common_1.Query)('businessId')),
    __param(1, (0, common_1.Query)('userId')),
    __param(2, (0, common_1.Query)('action')),
    __param(3, (0, common_1.Query)('entityType')),
    __param(4, (0, common_1.Query)('entityId')),
    __param(5, (0, common_1.Query)('startDate')),
    __param(6, (0, common_1.Query)('endDate')),
    __param(7, (0, common_1.Query)('page')),
    __param(8, (0, common_1.Query)('limit')),
    __param(9, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, String, String, String, String, String, String, String, Object]),
    __metadata("design:returntype", Promise)
], AuditController.prototype, "getLogs", null);
__decorate([
    (0, common_1.Get)('entity/:entityType/:entityId'),
    __param(0, (0, common_1.Param)('entityType')),
    __param(1, (0, common_1.Param)('entityId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], AuditController.prototype, "getEntityHistory", null);
__decorate([
    (0, common_1.Get)('api-logs'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('business_owner', 'super_admin'),
    __param(0, (0, common_1.Query)('businessId')),
    __param(1, (0, common_1.Query)('method')),
    __param(2, (0, common_1.Query)('url')),
    __param(3, (0, common_1.Query)('statusCode')),
    __param(4, (0, common_1.Query)('startDate')),
    __param(5, (0, common_1.Query)('endDate')),
    __param(6, (0, common_1.Query)('page')),
    __param(7, (0, common_1.Query)('limit')),
    __param(8, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, String, String, String, String, String, String, Object]),
    __metadata("design:returntype", Promise)
], AuditController.prototype, "getApiLogs", null);
exports.AuditController = AuditController = __decorate([
    (0, common_1.Controller)('audit'),
    (0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard, tenant_guard_1.TenantGuard),
    __metadata("design:paramtypes", [audit_service_1.AuditService])
], AuditController);
