"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.VouchersController = void 0;
const common_1 = require("@nestjs/common");
const vouchers_service_1 = require("./vouchers.service");
const generate_voucher_dto_1 = require("./dto/generate-voucher.dto");
const redeem_voucher_dto_1 = require("./dto/redeem-voucher.dto");
const voucher_entity_1 = require("../database/entities/voucher.entity");
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 VouchersController = class VouchersController {
    constructor(vouchersService) {
        this.vouchersService = vouchersService;
    }
    async getAll(req, status, batchId, page, limit) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId)
            throw new Error('Business ID is required');
        return this.vouchersService.getAll(businessId, {
            status,
            batchId,
            page: page ? Number(page) : undefined,
            limit: limit ? Number(limit) : undefined,
        });
    }
    async getStats(req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId)
            throw new Error('Business ID is required');
        return this.vouchersService.getVoucherStats(businessId);
    }
    async getAllBatches(req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId)
            throw new Error('Business ID is required');
        return this.vouchersService.getAllBatches(businessId);
    }
    async getBatch(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId)
            throw new Error('Business ID is required');
        return this.vouchersService.getBatch(id, businessId);
    }
    async exportVouchers(batchId, req, res) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId)
            throw new Error('Business ID is required');
        const csv = await this.vouchersService.exportVouchers(businessId, batchId);
        res.setHeader('Content-Type', 'text/csv');
        res.setHeader('Content-Disposition', `attachment; filename="vouchers-${Date.now()}.csv"`);
        res.send(csv);
    }
    async generateSingle(dto, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId)
            throw new Error('Business ID is required');
        return this.vouchersService.generateSingle(dto, businessId);
    }
    async generateBulk(dto, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId)
            throw new Error('Business ID is required');
        return this.vouchersService.generateBulk(dto, businessId);
    }
    async redeem(dto, req) {
        const businessId = req.businessId || req.user?.businessId;
        return this.vouchersService.redeem(dto, businessId);
    }
    async disable(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId)
            throw new Error('Business ID is required');
        return this.vouchersService.disable(id, businessId);
    }
};
exports.VouchersController = VouchersController;
__decorate([
    (0, common_1.Get)(),
    (0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Query)('status')),
    __param(2, (0, common_1.Query)('batchId')),
    __param(3, (0, common_1.Query)('page')),
    __param(4, (0, common_1.Query)('limit')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, String, Number, Number]),
    __metadata("design:returntype", Promise)
], VouchersController.prototype, "getAll", null);
__decorate([
    (0, common_1.Get)('stats'),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], VouchersController.prototype, "getStats", null);
__decorate([
    (0, common_1.Get)('batches'),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], VouchersController.prototype, "getAllBatches", null);
__decorate([
    (0, common_1.Get)('batches/: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)
], VouchersController.prototype, "getBatch", null);
__decorate([
    (0, common_1.Get)('export'),
    __param(0, (0, common_1.Query)('batchId')),
    __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)
], VouchersController.prototype, "exportVouchers", null);
__decorate([
    (0, common_1.Post)('generate'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('business_owner', 'business_admin', 'manager'),
    (0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [generate_voucher_dto_1.GenerateVoucherDto, Object]),
    __metadata("design:returntype", Promise)
], VouchersController.prototype, "generateSingle", null);
__decorate([
    (0, common_1.Post)('generate-bulk'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('business_owner', 'business_admin', 'manager'),
    (0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [generate_voucher_dto_1.GenerateBulkDto, Object]),
    __metadata("design:returntype", Promise)
], VouchersController.prototype, "generateBulk", null);
__decorate([
    (0, common_1.Post)('redeem'),
    (0, public_decorator_1.Public)(),
    (0, common_1.UsePipes)(new common_1.ValidationPipe({ transform: true })),
    __param(0, (0, common_1.Body)()),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [redeem_voucher_dto_1.RedeemVoucherDto, Object]),
    __metadata("design:returntype", Promise)
], VouchersController.prototype, "redeem", null);
__decorate([
    (0, common_1.Patch)(':id/disable'),
    (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)
], VouchersController.prototype, "disable", null);
exports.VouchersController = VouchersController = __decorate([
    (0, common_1.Controller)('vouchers'),
    (0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard, tenant_guard_1.TenantGuard),
    __metadata("design:paramtypes", [vouchers_service_1.VouchersService])
], VouchersController);
