"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.MpesaController = void 0;
const common_1 = require("@nestjs/common");
const mpesa_service_1 = require("./mpesa.service");
const stk_push_dto_1 = require("./dto/stk-push.dto");
const phone_validation_pipe_1 = require("../common/pipes/phone-validation.pipe");
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 public_decorator_1 = require("../common/decorators/public.decorator");
const roles_decorator_1 = require("../common/decorators/roles.decorator");
let MpesaController = class MpesaController {
    constructor(mpesaService) {
        this.mpesaService = mpesaService;
    }
    async initiateStkPush(dto, req) {
        const businessId = req.businessId || req.user?.businessId;
        const customerId = req.user?.customerId || null;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.mpesaService.initiateStkPush(dto, businessId, customerId);
    }
    async handleCallback(payload) {
        return this.mpesaService.handleCallback(payload);
    }
    async getPaymentStatus(checkoutRequestId) {
        return this.mpesaService.verifyTransactionStatus(checkoutRequestId);
    }
    async reconcilePayment(paymentId) {
        return this.mpesaService.reconcilePayment(paymentId);
    }
    async reversePayment(paymentId) {
        return this.mpesaService.reversePayment(paymentId);
    }
    async testConfig() {
        return this.mpesaService.validateMpesaConfig();
    }
    async getAccountBalance() {
        return this.mpesaService.getAccountBalance();
    }
};
exports.MpesaController = MpesaController;
__decorate([
    (0, common_1.Post)('stkpush'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
    __param(0, (0, common_1.Body)(phone_validation_pipe_1.PhoneValidationPipe)),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [stk_push_dto_1.StkPushDto, Object]),
    __metadata("design:returntype", Promise)
], MpesaController.prototype, "initiateStkPush", null);
__decorate([
    (0, common_1.Post)('callback'),
    (0, public_decorator_1.Public)(),
    (0, common_1.HttpCode)(common_1.HttpStatus.OK),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], MpesaController.prototype, "handleCallback", null);
__decorate([
    (0, common_1.Get)('status/:checkoutRequestId'),
    __param(0, (0, common_1.Param)('checkoutRequestId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], MpesaController.prototype, "getPaymentStatus", null);
__decorate([
    (0, common_1.Post)('reconcile/:paymentId'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('super_admin', 'system_admin', 'business_admin'),
    __param(0, (0, common_1.Param)('paymentId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], MpesaController.prototype, "reconcilePayment", null);
__decorate([
    (0, common_1.Post)('reverse/:paymentId'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('super_admin', 'system_admin', 'business_admin'),
    __param(0, (0, common_1.Param)('paymentId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], MpesaController.prototype, "reversePayment", null);
__decorate([
    (0, common_1.Get)('config/test'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('super_admin', 'system_admin'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], MpesaController.prototype, "testConfig", null);
__decorate([
    (0, common_1.Get)('balance'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('super_admin', 'system_admin'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], MpesaController.prototype, "getAccountBalance", null);
exports.MpesaController = MpesaController = __decorate([
    (0, common_1.Controller)('mpesa'),
    (0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard, tenant_guard_1.TenantGuard),
    __metadata("design:paramtypes", [mpesa_service_1.MpesaService])
], MpesaController);
