"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.TransactionsController = void 0;
const common_1 = require("@nestjs/common");
const passport_1 = require("@nestjs/passport");
const swagger_1 = require("@nestjs/swagger");
const roles_decorator_1 = require("../auth/decorators/roles.decorator");
const roles_guard_1 = require("../auth/guards/roles.guard");
const bootstrap_table_request_dto_1 = require("../commons/dto/bootstrap-table-request.dto");
const export_service_1 = require("../export/export.service");
const export_types_1 = require("../export/export.types");
const export_utils_1 = require("../export/export.utils");
const transaction_formatter_1 = require("../export/formatters/transaction.formatter");
const cancel_transaction_dto_1 = require("./dto/cancel-transaction.dto");
const create_payment_dto_1 = require("./dto/create-payment.dto");
const create_transaction_dto_1 = require("./dto/create-transaction.dto");
const transaction_chart_query_dto_1 = require("./dto/transaction-chart-query.dto");
const transaction_performance_query_dto_1 = require("./dto/transaction-performance-query.dto");
const transaction_summary_query_dto_1 = require("./dto/transaction-summary-query.dto");
const update_transaction_dto_1 = require("./dto/update-transaction.dto");
const transactions_service_1 = require("./transactions.service");
let TransactionsController = class TransactionsController {
    constructor(transactionsService, exportService) {
        this.transactionsService = transactionsService;
        this.exportService = exportService;
    }
    async generateItemId() {
        const data = await this.transactionsService.generateItemId();
        return {
            message: 'Item ID generated successfully',
            data,
        };
    }
    async create(req, dto) {
        const data = await this.transactionsService.create(dto, req.user);
        return {
            message: 'Transaction created successfully',
            data,
        };
    }
    async getSummary(filter) {
        const data = await this.transactionsService.getSummary(filter);
        return { message: 'Transaction summary fetched successfully', data };
    }
    async findAll(req, query) {
        const data = await this.transactionsService.findAll(query, req.user);
        return { message: 'Transactions fetched successfully', data };
    }
    async export(query, req, res) {
        const user = req.user;
        const isAllBranch = user.role?.accessAllBranch === true;
        let branchLabel;
        if (isAllBranch && query.branchId) {
            const branch = user.branches?.find((b) => b.branchId === query.branchId);
            branchLabel = branch?.branch?.name ?? query.branchId;
        }
        else if (isAllBranch) {
            branchLabel = 'all_branches';
        }
        else {
            branchLabel = user.branches?.[0]?.branch?.name;
        }
        const transactions = await this.transactionsService.getTransactionsForExport(query, user);
        await this.exportService.export({
            filename: (0, export_utils_1.exportFilename)('transactions', branchLabel),
            sheetName: 'Transactions',
            columns: transaction_formatter_1.TRANSACTION_COLUMNS,
            data: (0, transaction_formatter_1.formatTransactionRows)(transactions),
            format: query.format,
        }, res);
    }
    async findOne(id) {
        const data = await this.transactionsService.findOne(id);
        return { message: 'Transaction fetched successfully', data };
    }
    async getAuditLogs(id, query) {
        const data = await this.transactionsService.getAuditLogsForTransaction(id, query);
        return { message: 'Audit logs fetched successfully', data };
    }
    async complete(id, dto, req) {
        const data = await this.transactionsService.complete(id, dto.note, req.user);
        return { message: 'Transaction completed successfully', data };
    }
    async addPayment(id, dto, req) {
        const data = await this.transactionsService.addPayment(id, dto, req.user);
        return { message: 'Payment proof uploaded successfully', data };
    }
    async cancel(id, dto, req) {
        const data = await this.transactionsService.cancel(id, dto, req.user);
        return { message: 'Transaction cancelled successfully', data };
    }
    async update(id, dto, req) {
        const data = await this.transactionsService.update(id, dto, req.user);
        return { message: 'Transaction updated successfully', data };
    }
    async getChart(query, req) {
        const data = await this.transactionsService.getAnalyticsChart(query, req.user);
        return {
            message: 'Transaction chart analytics fetched successfully',
            data,
        };
    }
    async getAnalyticsSummary(query, req) {
        const data = await this.transactionsService.getAnalyticsSummary(query, req.user);
        return {
            message: 'Transaction analytics summary fetched successfully',
            data,
        };
    }
    async getPerformanceSummary(query, req) {
        const data = await this.transactionsService.getPerformanceSummary(query, req.user);
        return {
            message: 'Transaction performance summary fetched successfully',
            data
        };
    }
    async getCancellationReasons(query, req) {
        const data = await this.transactionsService.getCancellationReasons(query, req.user);
        return {
            message: 'Transaction cancellation reasons fetched successfully',
            data
        };
    }
    getPerformanceChart(query, req) {
        return this.transactionsService.getPerformanceChart(query, req.user);
    }
};
exports.TransactionsController = TransactionsController;
__decorate([
    (0, common_1.Post)('generate-item-id'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_OPS', 'FRONTLINER', 'HEAD_STORE'),
    (0, common_1.HttpCode)(common_1.HttpStatus.CREATED),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "generateItemId", null);
__decorate([
    (0, common_1.Post)(),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_OPS', 'FRONTLINER', 'HEAD_STORE'),
    (0, common_1.HttpCode)(common_1.HttpStatus.CREATED),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, create_transaction_dto_1.CreateTransactionDto]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "create", null);
__decorate([
    (0, common_1.Get)('summary'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_FINANCE', 'ADMIN_FINANCE', 'HEAD_STORE', 'HEAD_OPS', 'FRONTLINER', 'STAKEHOLDERS'),
    __param(0, (0, common_1.Query)('filter')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "getSummary", null);
__decorate([
    (0, common_1.Get)(),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_FINANCE', 'ADMIN_FINANCE', 'HEAD_STORE', 'HEAD_OPS', 'FRONTLINER', 'STAKEHOLDERS'),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Query)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, bootstrap_table_request_dto_1.BootstrapTableRequestDto]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "findAll", null);
__decorate([
    (0, common_1.Get)('export'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_FINANCE', 'ADMIN_FINANCE', 'FRONTLINER', 'HEAD_STORE', 'STAKEHOLDERS'),
    __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", [export_types_1.ExportQueryDto, Object, Object]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "export", null);
__decorate([
    (0, common_1.Get)(':id'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_FINANCE', 'ADMIN_FINANCE', 'HEAD_STORE', 'HEAD_OPS', 'FRONTLINER', 'STAKEHOLDERS'),
    __param(0, (0, common_1.Param)('id')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "findOne", null);
__decorate([
    (0, common_1.Get)(':id/audit-logs'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_FINANCE', 'ADMIN_FINANCE', 'HEAD_STORE', 'HEAD_OPS', 'FRONTLINER', 'STAKEHOLDERS'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Query)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, bootstrap_table_request_dto_1.BootstrapTableRequestDto]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "getAuditLogs", null);
__decorate([
    (0, common_1.Patch)(':id/complete'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_STORE', 'HEAD_OPS'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object, Object]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "complete", null);
__decorate([
    (0, common_1.Patch)(':id/payment'),
    (0, roles_decorator_1.Roles)('FRONTLINER', 'HEAD_FINANCE', 'HEAD_OPS', 'HEAD_STORE', 'SUPER_ADMIN', 'ADMIN_FINANCE'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, create_payment_dto_1.CreatePaymentDto, Object]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "addPayment", null);
__decorate([
    (0, common_1.Patch)(':id/cancel'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_STORE', 'HEAD_OPS'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, cancel_transaction_dto_1.CancelTransactionDto, Object]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "cancel", null);
__decorate([
    (0, common_1.Patch)(':id'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_FINANCE', 'HEAD_STORE', 'HEAD_OPS', 'FRONTLINER'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, update_transaction_dto_1.UpdateTransactionDto, Object]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "update", null);
__decorate([
    (0, swagger_1.ApiOperation)({
        summary: 'Get transaction analytics chart',
        description: 'Returns success and bounce series for chart visualization',
    }),
    (0, common_1.Get)('analytics/chart'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_FINANCE', 'ADMIN_FINANCE', 'HEAD_STORE', 'HEAD_OPS', 'FRONTLINER', 'STAKEHOLDERS'),
    __param(0, (0, common_1.Query)()),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [transaction_chart_query_dto_1.TransactionChartQueryDto, Object]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "getChart", null);
__decorate([
    (0, swagger_1.ApiOperation)({
        summary: 'Get transaction analytics chart',
        description: 'Returns success and bounce series for chart visualization',
    }),
    (0, common_1.Get)('analytics/summary'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_FINANCE', 'ADMIN_FINANCE', 'HEAD_STORE', 'HEAD_OPS', 'FRONTLINER', 'STAKEHOLDERS'),
    __param(0, (0, common_1.Query)()),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [transaction_summary_query_dto_1.TransactionSummaryQueryDto, Object]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "getAnalyticsSummary", null);
__decorate([
    (0, swagger_1.ApiOperation)({
        summary: 'Transaction performance summary',
        description: 'Returns performance metrics for all, completed, canceled and rejected transactions'
    }),
    (0, common_1.Get)('analytics/performance-summary'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_FINANCE', 'ADMIN_FINANCE', 'HEAD_STORE', 'HEAD_OPS', 'FRONTLINER', 'STAKEHOLDERS'),
    __param(0, (0, common_1.Query)()),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [transaction_performance_query_dto_1.TransactionPerformanceQueryDto, Object]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "getPerformanceSummary", null);
__decorate([
    (0, swagger_1.ApiOperation)({
        summary: 'Transaction cancellation reasons',
        description: 'Returns distinct cancellation reasons with their counts ordered from most to least'
    }),
    (0, common_1.Get)('analytics/cancellation-reasons'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_FINANCE', 'ADMIN_FINANCE', 'HEAD_STORE', 'HEAD_OPS', 'FRONTLINER', 'STAKEHOLDERS'),
    __param(0, (0, common_1.Query)()),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [transaction_performance_query_dto_1.TransactionPerformanceQueryDto, Object]),
    __metadata("design:returntype", Promise)
], TransactionsController.prototype, "getCancellationReasons", null);
__decorate([
    (0, common_1.Get)('analytics/performance-chart'),
    (0, roles_decorator_1.Roles)('SUPER_ADMIN', 'HEAD_FINANCE', 'ADMIN_FINANCE', 'HEAD_STORE', 'HEAD_OPS', 'FRONTLINER', 'STAKEHOLDERS'),
    __param(0, (0, common_1.Query)()),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [transaction_chart_query_dto_1.TransactionChartQueryDto, Object]),
    __metadata("design:returntype", void 0)
], TransactionsController.prototype, "getPerformanceChart", null);
exports.TransactionsController = TransactionsController = __decorate([
    (0, swagger_1.ApiTags)('Transactions'),
    (0, swagger_1.ApiBearerAuth)(),
    (0, common_1.Controller)('transactions'),
    (0, common_1.UseGuards)((0, passport_1.AuthGuard)('jwt'), roles_guard_1.RolesGuard),
    __metadata("design:paramtypes", [transactions_service_1.TransactionsService,
        export_service_1.ExportService])
], TransactionsController);
//# sourceMappingURL=transactions.controller.js.map