"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); }
};
var AdminVendorController_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdminVendorController = void 0;
const common_1 = require("@nestjs/common");
const vendor_service_1 = require("./vendor.service");
const vendor_dto_1 = require("./vendor.dto");
const firebase_service_1 = require("../../appapi/fcm/firebase.service");
const user_services_1 = require("../../appapi/user/user.services");
const swagger_1 = require("@nestjs/swagger");
let AdminVendorController = AdminVendorController_1 = class AdminVendorController {
    constructor(adminVendorService, firebaseService, userService) {
        this.adminVendorService = adminVendorService;
        this.firebaseService = firebaseService;
        this.userService = userService;
        this.logger = new common_1.Logger(AdminVendorController_1.name);
    }
    async create(CreateVendorDto) {
        try {
            const res = await this.adminVendorService.create(CreateVendorDto);
            return {
                status: true,
                message: "Vendor Successfully created.",
            };
        }
        catch (error) {
            throw new common_1.BadRequestException(error.message);
        }
    }
    async findAll() {
        try {
            const vendors = await this.adminVendorService.findAll();
            return {
                status: true,
                message: "Vendor list.",
                data: vendors,
            };
        }
        catch (error) {
            throw new common_1.BadRequestException(error.message);
        }
    }
    async findOne(id) {
        try {
            const vendor = await this.adminVendorService.findOne(+id);
            return {
                status: true,
                message: "Vendor detail.",
                data: vendor,
            };
        }
        catch (error) {
            throw new common_1.BadRequestException(error.message);
        }
    }
    async update(id, UpdateVendorDto) {
        try {
            const res = await this.adminVendorService.update(+id, UpdateVendorDto);
            return {
                status: true,
                message: "Vendor Successfully updated.",
            };
        }
        catch (error) {
            throw new common_1.BadRequestException(error.message);
        }
    }
    async updateUserStatus(id, body) {
        this.logger.log(`Received request to update status for User ID ${id} to '${body.status}'`);
        const res = await this.adminVendorService.updateUserStatus(id, body.status);
        const user = await this.userService.findById(id);
        const deviceToken = user?.deviceId?.toString();
        if (res) {
            if (!deviceToken || deviceToken === 'null' || deviceToken === 'undefined') {
                this.logger.warn(`User ${id} has no valid deviceId. Skipping notification. Got: '${deviceToken}'`);
            }
            else {
                if (body.status === 'active') {
                    this.logger.log(`Sending activation notification to user ${id}`);
                    try {
                        await this.firebaseService.sendNotification(deviceToken, 'Your plan activation 🚀', 'Your plan has been activated successfully.', 'VendorPlan');
                    }
                    catch (err) {
                        this.logger.error(`Failed to send activation notification to user ${id}: ${err?.message ?? err}`, err?.stack);
                    }
                }
                else if (body.status === 'inactive') {
                    this.logger.warn(`Sending deactivation notification to user ${id}`);
                    try {
                        await this.firebaseService.sendNotification(deviceToken, 'Your plan deactivation ⚠️', 'Your plan has been deactivated. Please contact support for more information.', 'VendorPlan');
                    }
                    catch (err) {
                        this.logger.error(`Failed to send deactivation notification to user ${id}: ${err?.message ?? err}`, err?.stack);
                    }
                }
            }
            return {
                status: true,
                message: 'Status Successfully updated.',
            };
        }
        else {
            this.logger.error(`Failed to update status for User ID ${id}`);
            return {
                status: false,
                message: 'Status not updated.',
            };
        }
    }
    async remove(id) {
        try {
            const res = await this.adminVendorService.remove(+id);
            return {
                status: true,
                message: "Vendor Successfully deleted.",
            };
        }
        catch (error) {
            throw new common_1.BadRequestException(error.message);
        }
    }
    async getDashboard(vendorId) {
        try {
            const dashboard = await this.adminVendorService.getVendorDashboard(vendorId);
            return {
                status: true,
                message: "Vendor dashboard data fetched successfully.",
                data: dashboard,
            };
        }
        catch (error) {
            throw new common_1.BadRequestException(error.message);
        }
    }
};
exports.AdminVendorController = AdminVendorController;
__decorate([
    (0, common_1.Post)(),
    __param(0, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [vendor_dto_1.CreateVendorDto]),
    __metadata("design:returntype", Promise)
], AdminVendorController.prototype, "create", null);
__decorate([
    (0, common_1.Get)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], AdminVendorController.prototype, "findAll", null);
__decorate([
    (0, common_1.Get)(":id"),
    __param(0, (0, common_1.Param)("id")),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], AdminVendorController.prototype, "findOne", null);
__decorate([
    (0, common_1.Patch)(":id"),
    __param(0, (0, common_1.Param)("id")),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, vendor_dto_1.UpdateVendorDto]),
    __metadata("design:returntype", Promise)
], AdminVendorController.prototype, "update", null);
__decorate([
    (0, common_1.Patch)(':id/status'),
    (0, swagger_1.ApiParam)({ name: 'id', type: Number }),
    (0, swagger_1.ApiBody)({ type: vendor_dto_1.UpdateUserStatusDto }),
    __param(0, (0, common_1.Param)('id')),
    __param(1, (0, common_1.Body)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number, vendor_dto_1.UpdateUserStatusDto]),
    __metadata("design:returntype", Promise)
], AdminVendorController.prototype, "updateUserStatus", null);
__decorate([
    (0, common_1.Delete)(":id"),
    __param(0, (0, common_1.Param)("id")),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], AdminVendorController.prototype, "remove", null);
__decorate([
    (0, common_1.Get)('dashboard/:vendorId'),
    __param(0, (0, common_1.Param)('vendorId')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number]),
    __metadata("design:returntype", Promise)
], AdminVendorController.prototype, "getDashboard", null);
exports.AdminVendorController = AdminVendorController = AdminVendorController_1 = __decorate([
    (0, common_1.Controller)("admin/vendor"),
    __metadata("design:paramtypes", [vendor_service_1.AdminVendorService,
        firebase_service_1.FirebaseService,
        user_services_1.UserService])
], AdminVendorController);
//# sourceMappingURL=vendor.controller.js.map