"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.PaymentsController = void 0;
const common_1 = require("@nestjs/common");
const payments_service_1 = require("./payments.service");
const receipt_service_1 = require("./receipt.service");
const invoice_service_1 = require("./invoice.service");
const payment_filter_dto_1 = require("./dto/payment-filter.dto");
const date_filter_dto_1 = require("./dto/date-filter.dto");
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");
const public_decorator_1 = require("../common/decorators/public.decorator");
let PaymentsController = class PaymentsController {
    constructor(paymentsService, receiptService, invoiceService) {
        this.paymentsService = paymentsService;
        this.receiptService = receiptService;
        this.invoiceService = invoiceService;
    }
    async findAll(filters, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.paymentsService.findAll(businessId, filters);
    }
    async getStats(filters, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.paymentsService.getPaymentStats(businessId, filters);
    }
    async getRecent(limit, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        const limitNum = limit ? parseInt(String(limit), 10) : 10;
        return this.paymentsService.getRecentTransactions(businessId, limitNum);
    }
    async getReconciliation(req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.paymentsService.getTransactionReconciliation(businessId);
    }
    async exportCsv(filters, req, res) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        const csv = await this.paymentsService.exportPaymentsCsv(businessId, filters);
        res.setHeader('Content-Type', 'text/csv');
        res.setHeader('Content-Disposition', `attachment; filename="payments-export-${Date.now()}.csv"`);
        res.send(csv);
    }
    async findOne(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.paymentsService.findOne(id, businessId);
    }
    async downloadReceipt(id, req, res) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        const { pdfBuffer, receiptNumber } = await this.paymentsService.generateReceipt(id, businessId);
        res.setHeader('Content-Type', 'application/pdf');
        res.setHeader('Content-Disposition', `inline; filename="receipt-${receiptNumber}.pdf"`);
        res.send(pdfBuffer);
    }
    async downloadInvoice(id, req, res) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        const { pdfBuffer, invoiceNumber } = await this.invoiceService.generateInvoice(id);
        res.setHeader('Content-Type', 'application/pdf');
        res.setHeader('Content-Disposition', `inline; filename="invoice-${invoiceNumber}.pdf"`);
        res.send(pdfBuffer);
    }
    async reconcile(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        await this.paymentsService.findOne(id, businessId);
        return this.paymentsService.processCallback({ paymentId: id, action: 'reconcile' });
    }
    async retryActivation(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        await this.paymentsService.findOne(id, businessId);
        return this.paymentsService.retryActivation(id);
    }
    async handleCallback(payload) {
        return this.paymentsService.processCallback(payload);
    }
};
exports.PaymentsController = PaymentsController;
__decorate([
    (0, common_1.Get)(),
    (0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
    __param(0, (0, common_1.Query)()),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [payment_filter_dto_1.PaymentFilterDto, Object]),
    __metadata("design:returntype", Promise)
], PaymentsController.prototype, "findAll", null);
__decorate([
    (0, common_1.Get)('stats'),
    (0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
    __param(0, (0, common_1.Query)()),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [date_filter_dto_1.DateFilterDto, Object]),
    __metadata("design:returntype", Promise)
], PaymentsController.prototype, "getStats", null);
__decorate([
    (0, common_1.Get)('recent'),
    __param(0, (0, common_1.Query)('limit')),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", Promise)
], PaymentsController.prototype, "getRecent", null);
__decorate([
    (0, common_1.Get)('reconciliation'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('business_owner', 'business_admin', 'manager'),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], PaymentsController.prototype, "getReconciliation", null);
__decorate([
    (0, common_1.Get)('export'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('business_owner', 'business_admin', 'manager'),
    __param(0, (0, common_1.Query)()),
    __param(1, (0, common_1.Req)()),
    __param(2, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [payment_filter_dto_1.PaymentFilterDto, Object, Object]),
    __metadata("design:returntype", Promise)
], PaymentsController.prototype, "exportCsv", null);
__decorate([
    (0, common_1.Get)(':id'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], PaymentsController.prototype, "findOne", null);
__decorate([
    (0, common_1.Get)(':id/receipt'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Req)()),
    __param(2, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object, Object]),
    __metadata("design:returntype", Promise)
], PaymentsController.prototype, "downloadReceipt", null);
__decorate([
    (0, common_1.Get)(':id/invoice'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Req)()),
    __param(2, (0, common_1.Res)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object, Object]),
    __metadata("design:returntype", Promise)
], PaymentsController.prototype, "downloadInvoice", null);
__decorate([
    (0, common_1.Post)(':id/reconcile'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('business_owner', 'business_admin', 'manager'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], PaymentsController.prototype, "reconcile", null);
__decorate([
    (0, common_1.Post)(':id/retry-activation'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('business_owner', 'business_admin', 'manager'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], PaymentsController.prototype, "retryActivation", 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)
], PaymentsController.prototype, "handleCallback", null);
exports.PaymentsController = PaymentsController = __decorate([
    (0, common_1.Controller)('payments'),
    (0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard, tenant_guard_1.TenantGuard),
    __metadata("design:paramtypes", [payments_service_1.PaymentsService,
        receipt_service_1.ReceiptService,
        invoice_service_1.InvoiceService])
], PaymentsController);
