"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.PackagesController = void 0;
const common_1 = require("@nestjs/common");
const packages_service_1 = require("./packages.service");
const package_dto_1 = require("./dto/package.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");
let PackagesController = class PackagesController {
    constructor(packagesService) {
        this.packagesService = packagesService;
    }
    async findAll(req, isActive, isFeatured, type) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        const filters = {};
        if (isActive !== undefined) {
            filters.isActive = isActive === 'true';
        }
        if (isFeatured !== undefined) {
            filters.isFeatured = isFeatured === 'true';
        }
        if (type) {
            filters.type = type;
        }
        return this.packagesService.findAll(businessId, filters);
    }
    async getFeatured(req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.packagesService.getFeatured(businessId);
    }
    async getPackagesForRouter(routerId) {
        return this.packagesService.getPackagesForRouter(routerId);
    }
    async findOne(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.packagesService.findOne(id, businessId);
    }
    async create(dto, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.packagesService.create(dto, businessId);
    }
    async update(id, dto, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.packagesService.update(id, dto, businessId);
    }
    async delete(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        await this.packagesService.delete(id, businessId);
        return { message: 'Package deleted successfully' };
    }
    async duplicate(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.packagesService.duplicate(id, businessId);
    }
    async toggleActive(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.packagesService.toggleActive(id, businessId);
    }
    async assignToRouter(id, routerId, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.packagesService.assignToRouter(id, routerId, businessId);
    }
    async removeFromRouter(id, routerId) {
        await this.packagesService.removeFromRouter(id, routerId);
        return { message: 'Package removed from router successfully' };
    }
};
exports.PackagesController = PackagesController;
__decorate([
    (0, common_1.Get)(),
    __param(0, (0, common_1.Req)()),
    __param(1, (0, common_1.Query)('isActive')),
    __param(2, (0, common_1.Query)('isFeatured')),
    __param(3, (0, common_1.Query)('type')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, String, String]),
    __metadata("design:returntype", Promise)
], PackagesController.prototype, "findAll", null);
__decorate([
    (0, common_1.Get)('featured'),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], PackagesController.prototype, "getFeatured", null);
__decorate([
    (0, common_1.Get)('router/:routerId'),
    __param(0, (0, common_1.Param)('routerId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], PackagesController.prototype, "getPackagesForRouter", 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)
], PackagesController.prototype, "findOne", null);
__decorate([
    (0, common_1.Post)(),
    (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", [package_dto_1.CreatePackageDto, Object]),
    __metadata("design:returntype", Promise)
], PackagesController.prototype, "create", null);
__decorate([
    (0, common_1.Put)(':id'),
    (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.Param)('id')),
    __param(1, (0, common_1.Body)()),
    __param(2, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, package_dto_1.UpdatePackageDto, Object]),
    __metadata("design:returntype", Promise)
], PackagesController.prototype, "update", null);
__decorate([
    (0, common_1.Delete)(':id'),
    (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)
], PackagesController.prototype, "delete", null);
__decorate([
    (0, common_1.Post)(':id/duplicate'),
    (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)
], PackagesController.prototype, "duplicate", null);
__decorate([
    (0, common_1.Patch)(':id/toggle'),
    (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)
], PackagesController.prototype, "toggleActive", null);
__decorate([
    (0, common_1.Post)(':id/routers/:routerId'),
    (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.Param)('routerId')),
    __param(2, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object]),
    __metadata("design:returntype", Promise)
], PackagesController.prototype, "assignToRouter", null);
__decorate([
    (0, common_1.Delete)(':id/routers/:routerId'),
    (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.Param)('routerId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], PackagesController.prototype, "removeFromRouter", null);
exports.PackagesController = PackagesController = __decorate([
    (0, common_1.Controller)('packages'),
    (0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard, tenant_guard_1.TenantGuard),
    __metadata("design:paramtypes", [packages_service_1.PackagesService])
], PackagesController);
