"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.BusinessController = void 0;
const common_1 = require("@nestjs/common");
const business_service_1 = require("./business.service");
const update_business_dto_1 = require("./dto/update-business.dto");
const dashboard_filter_dto_1 = require("./dto/dashboard-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");
let BusinessController = class BusinessController {
    constructor(businessService) {
        this.businessService = businessService;
    }
    async getProfile(req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new common_1.ForbiddenException({
                success: false,
                message: 'Business account association not found',
                code: 'BUSINESS_NOT_LINKED',
            });
        }
        const business = await this.businessService.getBusiness(businessId);
        if (!business) {
            throw new common_1.NotFoundException({
                success: false,
                message: 'Business profile not found',
                code: 'BUSINESS_NOT_FOUND',
            });
        }
        return business;
    }
    async updateProfile(dto, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new common_1.ForbiddenException('Business ID is required');
        }
        return this.businessService.updateBusiness(businessId, dto);
    }
    async getDashboard(filters, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new common_1.ForbiddenException('Business ID is required');
        }
        return this.businessService.getDashboardOverview(businessId, filters);
    }
    async getRevenueChart(days, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new common_1.ForbiddenException('Business ID is required');
        }
        return this.businessService.getRevenueChartData(businessId, days && days > 0 ? Number(days) : 30);
    }
    async getSettings(req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new common_1.ForbiddenException('Business ID is required');
        }
        return this.businessService.getBusinessSettings(businessId);
    }
    async updateSetting(key, value, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new common_1.ForbiddenException('Business ID is required');
        }
        if (!value) {
            throw new common_1.BadRequestException('Value is required');
        }
        return this.businessService.updateBusinessSettings(businessId, key, value);
    }
    async getLimits(req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new common_1.ForbiddenException('Business ID is required');
        }
        return this.businessService.enforceSubscriptionLimits(businessId);
    }
    async getAllBusinesses() {
        return this.businessService.getAllBusinesses();
    }
    async suspendBusiness(id, reason) {
        if (!reason) {
            throw new common_1.BadRequestException('Suspension reason is required');
        }
        return this.businessService.suspendBusiness(id, reason);
    }
    async unsuspendBusiness(id) {
        return this.businessService.unsuspendBusiness(id);
    }
    async deleteBusiness(id) {
        return this.businessService.deleteBusiness(id);
    }
};
exports.BusinessController = BusinessController;
__decorate([
    (0, common_1.Get)('profile'),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], BusinessController.prototype, "getProfile", null);
__decorate([
    (0, common_1.Put)('profile'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('business_owner', 'business_admin'),
    (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", [update_business_dto_1.UpdateBusinessDto, Object]),
    __metadata("design:returntype", Promise)
], BusinessController.prototype, "updateProfile", null);
__decorate([
    (0, common_1.Get)('dashboard'),
    (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", [dashboard_filter_dto_1.DashboardFilterDto, Object]),
    __metadata("design:returntype", Promise)
], BusinessController.prototype, "getDashboard", null);
__decorate([
    (0, common_1.Get)('dashboard/revenue-chart'),
    __param(0, (0, common_1.Query)('days')),
    __param(1, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, Object]),
    __metadata("design:returntype", Promise)
], BusinessController.prototype, "getRevenueChart", null);
__decorate([
    (0, common_1.Get)('settings'),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], BusinessController.prototype, "getSettings", null);
__decorate([
    (0, common_1.Put)('settings/:key'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('business_owner', 'business_admin'),
    __param(0, (0, common_1.Param)('key')),
    __param(1, (0, common_1.Body)('value')),
    __param(2, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object]),
    __metadata("design:returntype", Promise)
], BusinessController.prototype, "updateSetting", null);
__decorate([
    (0, common_1.Get)('limits'),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], BusinessController.prototype, "getLimits", null);
__decorate([
    (0, common_1.Get)('admin/all'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('super_admin'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], BusinessController.prototype, "getAllBusinesses", null);
__decorate([
    (0, common_1.Post)('admin/:id/suspend'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('super_admin'),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Body)('reason')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], BusinessController.prototype, "suspendBusiness", null);
__decorate([
    (0, common_1.Post)('admin/:id/unsuspend'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('super_admin'),
    __param(0, (0, common_1.Param)('id')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], BusinessController.prototype, "unsuspendBusiness", null);
__decorate([
    (0, common_1.Delete)('admin/:id'),
    (0, common_1.UseGuards)(roles_guard_1.RolesGuard),
    (0, roles_decorator_1.Roles)('super_admin'),
    __param(0, (0, common_1.Param)('id')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], BusinessController.prototype, "deleteBusiness", null);
exports.BusinessController = BusinessController = __decorate([
    (0, common_1.Controller)('business'),
    (0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard, tenant_guard_1.TenantGuard),
    __metadata("design:paramtypes", [business_service_1.BusinessService])
], BusinessController);
