"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.DevicesController = void 0;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const services_1 = require("../services");
const dto_1 = require("../dto");
const CurrentUser = () => (target, key, index) => { };
const CurrentTenant = () => (target, key, index) => { };
const JwtAuthGuard = class {
};
const TenantGuard = class {
};
const Public = () => (target, key, descriptor) => { };
let DevicesController = class DevicesController {
    constructor(devicesService, pushService) {
        this.devicesService = devicesService;
        this.pushService = pushService;
    }
    getVapidKey() {
        const vapidPublicKey = this.pushService.getVapidPublicKey();
        return {
            vapidPublicKey,
            isEnabled: this.pushService.isEnabled(),
        };
    }
    async getDevices(user, tenantId) {
        const userId = user?.id || '';
        const tenant = tenantId || user?.tenant_id || '';
        return this.devicesService.findByUser(userId, tenant);
    }
    async registerDevice(user, tenantId, dto) {
        const userId = user?.id || '';
        const tenant = tenantId || user?.tenant_id || '';
        if (!this.pushService.validateSubscription(dto.deviceToken)) {
            return {
                success: false,
                error: 'Invalid push subscription format',
            };
        }
        const device = await this.devicesService.register(userId, tenant, dto);
        return {
            success: true,
            device: {
                id: device.id,
                device_type: device.device_type,
                device_name: device.device_name,
                browser: device.browser,
                os: device.os,
                created_at: device.created_at,
            },
        };
    }
    async updateDevice(user, tenantId, deviceId, dto) {
        const userId = user?.id || '';
        const tenant = tenantId || user?.tenant_id || '';
        return this.devicesService.update(deviceId, userId, tenant, dto);
    }
    async unregisterDevice(user, tenantId, deviceId) {
        const userId = user?.id || '';
        const tenant = tenantId || user?.tenant_id || '';
        await this.devicesService.unregister(deviceId, userId, tenant);
    }
    async getStats(user, tenantId) {
        const userId = user?.id || '';
        const tenant = tenantId || user?.tenant_id || '';
        const activeCount = await this.devicesService.countActiveDevices(userId, tenant);
        const devices = await this.devicesService.findByUser(userId, tenant);
        return {
            total: devices.length,
            active: activeCount,
            inactive: devices.length - activeCount,
            byType: {
                web: devices.filter((d) => d.device_type === 'web').length,
                mobile: devices.filter((d) => d.device_type === 'mobile').length,
                desktop: devices.filter((d) => d.device_type === 'desktop').length,
            },
        };
    }
};
exports.DevicesController = DevicesController;
__decorate([
    (0, common_1.Get)('vapid-key'),
    (0, swagger_1.ApiOperation)({ summary: 'Get VAPID public key for push subscription' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Returns VAPID public key' }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], DevicesController.prototype, "getVapidKey", null);
__decorate([
    (0, common_1.Get)(),
    (0, common_1.UseGuards)(JwtAuthGuard, TenantGuard),
    (0, swagger_1.ApiBearerAuth)(),
    (0, swagger_1.ApiOperation)({ summary: 'List my registered devices' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Returns list of devices' }),
    __param(0, CurrentUser()),
    __param(1, CurrentTenant()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], DevicesController.prototype, "getDevices", null);
__decorate([
    (0, common_1.Post)(),
    (0, common_1.UseGuards)(JwtAuthGuard, TenantGuard),
    (0, swagger_1.ApiBearerAuth)(),
    (0, swagger_1.ApiOperation)({ summary: 'Register device for push notifications' }),
    (0, swagger_1.ApiResponse)({ status: 201, description: 'Device registered' }),
    (0, swagger_1.ApiResponse)({ status: 400, description: 'Invalid subscription' }),
    __param(0, CurrentUser()),
    __param(1, CurrentTenant()),
    __param(2, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, dto_1.RegisterDeviceDto]),
    __metadata("design:returntype", Promise)
], DevicesController.prototype, "registerDevice", null);
__decorate([
    (0, common_1.Patch)(':id'),
    (0, common_1.UseGuards)(JwtAuthGuard, TenantGuard),
    (0, swagger_1.ApiBearerAuth)(),
    (0, swagger_1.ApiOperation)({ summary: 'Update device' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Device updated' }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Device not found' }),
    __param(0, CurrentUser()),
    __param(1, CurrentTenant()),
    __param(2, (0, common_1.Param)('id')),
    __param(3, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, String, dto_1.UpdateDeviceDto]),
    __metadata("design:returntype", Promise)
], DevicesController.prototype, "updateDevice", null);
__decorate([
    (0, common_1.Delete)(':id'),
    (0, common_1.UseGuards)(JwtAuthGuard, TenantGuard),
    (0, swagger_1.ApiBearerAuth)(),
    (0, common_1.HttpCode)(common_1.HttpStatus.NO_CONTENT),
    (0, swagger_1.ApiOperation)({ summary: 'Unregister device' }),
    (0, swagger_1.ApiResponse)({ status: 204, description: 'Device unregistered' }),
    (0, swagger_1.ApiResponse)({ status: 404, description: 'Device not found' }),
    __param(0, CurrentUser()),
    __param(1, CurrentTenant()),
    __param(2, (0, common_1.Param)('id')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, String]),
    __metadata("design:returntype", Promise)
], DevicesController.prototype, "unregisterDevice", null);
__decorate([
    (0, common_1.Get)('stats'),
    (0, common_1.UseGuards)(JwtAuthGuard, TenantGuard),
    (0, swagger_1.ApiBearerAuth)(),
    (0, swagger_1.ApiOperation)({ summary: 'Get device stats for current user' }),
    (0, swagger_1.ApiResponse)({ status: 200, description: 'Returns device statistics' }),
    __param(0, CurrentUser()),
    __param(1, CurrentTenant()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], DevicesController.prototype, "getStats", null);
exports.DevicesController = DevicesController = __decorate([
    (0, swagger_1.ApiTags)('Notification Devices'),
    (0, common_1.Controller)('notifications/devices'),
    __metadata("design:paramtypes", [services_1.DevicesService,
        services_1.PushNotificationService])
], DevicesController);
//# sourceMappingURL=devices.controller.js.map