"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.CustomersController = void 0;
const common_1 = require("@nestjs/common");
const customers_service_1 = require("./customers.service");
const customer_filter_dto_1 = require("./dto/customer-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 CustomersController = class CustomersController {
    constructor(customersService) {
        this.customersService = customersService;
    }
    async findAll(filters, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.customersService.findAll(businessId, filters);
    }
    async getStats(req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.customersService.getCustomersStats(businessId);
    }
    async getSelfProfile(req) {
        const businessId = req.businessId || req.user?.businessId;
        const phoneNumber = req.user?.phoneNumber;
        if (!businessId || !phoneNumber) {
            throw new Error('Business ID and phone number are required');
        }
        return this.customersService.findByPhone(phoneNumber, businessId);
    }
    async findById(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.customersService.findById(id, businessId);
    }
    async getSessions(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        await this.customersService.findById(id, businessId);
        return this.customersService.getCustomerSessions(id);
    }
    async getPayments(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        await this.customersService.findById(id, businessId);
        return this.customersService.getCustomerPayments(id);
    }
    async getDevices(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        await this.customersService.findById(id, businessId);
        return this.customersService.getCustomerDevices(id);
    }
    async removeDevice(id, deviceId, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        await this.customersService.findById(id, businessId);
        await this.customersService.removeDevice(id, deviceId);
        return { success: true, message: 'Device removed' };
    }
    async blockCustomer(id, reason, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.customersService.blockCustomer(id, businessId, reason);
    }
    async unblockCustomer(id, req) {
        const businessId = req.businessId || req.user?.businessId;
        if (!businessId) {
            throw new Error('Business ID is required');
        }
        return this.customersService.unblockCustomer(id, businessId);
    }
};
exports.CustomersController = CustomersController;
__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", [customer_filter_dto_1.CustomerFilterDto, Object]),
    __metadata("design:returntype", Promise)
], CustomersController.prototype, "findAll", 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)
], CustomersController.prototype, "getStats", null);
__decorate([
    (0, common_1.Get)('self'),
    (0, public_decorator_1.Public)(),
    __param(0, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], CustomersController.prototype, "getSelfProfile", 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)
], CustomersController.prototype, "findById", null);
__decorate([
    (0, common_1.Get)(':id/sessions'),
    __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)
], CustomersController.prototype, "getSessions", null);
__decorate([
    (0, common_1.Get)(':id/payments'),
    __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)
], CustomersController.prototype, "getPayments", null);
__decorate([
    (0, common_1.Get)(':id/devices'),
    __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)
], CustomersController.prototype, "getDevices", null);
__decorate([
    (0, common_1.Delete)(':id/devices/:deviceId'),
    (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)('deviceId')),
    __param(2, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object]),
    __metadata("design:returntype", Promise)
], CustomersController.prototype, "removeDevice", null);
__decorate([
    (0, common_1.Post)(':id/block'),
    (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.Body)('reason')),
    __param(2, (0, common_1.Req)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object]),
    __metadata("design:returntype", Promise)
], CustomersController.prototype, "blockCustomer", null);
__decorate([
    (0, common_1.Post)(':id/unblock'),
    (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)
], CustomersController.prototype, "unblockCustomer", null);
exports.CustomersController = CustomersController = __decorate([
    (0, common_1.Controller)('customers'),
    (0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard, tenant_guard_1.TenantGuard),
    __metadata("design:paramtypes", [customers_service_1.CustomersService])
], CustomersController);
