All files / Backend/Controller certificate.js

3.91% Statements 11/281
2.94% Branches 4/136
0% Functions 0/22
3.97% Lines 11/277

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 9311x 1x 1x 1x 1x 1x 1x 1x 1x     1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   1x                          
const Certificate = require('../Models/Certificate');
const User = require('../Models/User');
const Event = require('../Models/Event');
const EventParticipationLog = require('../Models/EventParticipationLog');
const Institution = require('../Models/Institution');
const axios = require('axios');
const QRCode = require('qrcode');
const nodemailer = require('nodemailer'); // Added for email notifications
const { notifyUser } = require('../Services/notification');
 
// ML Certificate Generation API Configuration
const ML_API_CONFIG = {
  baseURL: process.env.ML_CERTIFICATE_API_URL || 'https://ml-certificate-api.example.com',
  timeout: 30000, // 30 seconds
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${process.env.ML_API_KEY || ''}`
  }
};
 
/**
 * Generate certificate data for ML API
 */
async function prepareCertificateData(userId, eventId, certificateType) {
  try {
    // Fetch user, event, and participation data
    const [user, event, participationLog] = await Promise.all([
      User.findById(userId).populate('institutionId'),
      Event.findById(eventId).populate('hostUserId'),
      EventParticipationLog.findOne({ userId, eventId, status: 'attended' })
    ]);
 
    if (!user || !event || !participationLog) {
      throw new Error('User, event, or attendance data not found');
    }
 
    // Generate QR code for certificate verification
    const qrData = JSON.stringify({
      certificateId: `cert_${userId}_${eventId}_${Date.now()}`,
      userId: userId.toString(),
      eventId: eventId.toString(),
      issuedAt: new Date().toISOString()
    });
    const qrCode = await QRCode.toDataURL(qrData);
 
    // Prepare certificate data for ML API
    const certificateData = {
      userName: user.name,
      userEmail: user.email,
      eventTitle: event.title,
      eventDescription: event.description,
      eventDate: event.schedule.start,
      eventLocation: event.location || 'Online',
      organizerName: event.hostUserId ? event.hostUserId.name : event.organizer,
      institutionName: user.institutionId ? user.institutionId.name : 'Unknown Institution',
      certificateType,
      userSkills: user.skills || [],
      eventTags: event.tags || [],
      attendanceDate: participationLog.attendanceTimestamp,
      qrCode,
      // Additional metadata for ML processing
      eventDuration: event.schedule.end - event.schedule.start,
      userInterests: user.interests || [],
      eventType: event.type,
      isPaid: event.isPaid,
      price: event.price
    };
 
    return certificateData;
  } catch (error) {
    console.error('Error preparing certificate data:', error);
    throw error;
  }
}
 
/**
 * Send certificate data to ML API for generation
 */
async function sendToMLAPI(certificateData) {
  try {
    const response = await axios.post(
      `${ML_API_CONFIG.baseURL}/generate-certificate`,
      certificateData,
      {
        timeout: ML_API_CONFIG.timeout,
        headers: ML_API_CONFIG.headers
      }
    );
 
    return {
      success: true,
      requestId: response.data.requestId,
      certificateURL: response.data.certificateURL,
      generationStatus: 'generated'
    };
  } catch (error) {
    console.error('ML API Error:', error.response?.data || error.message);
    return {
      success: false,
      requestId: null,
      errorMessage: error.response?.data?.message || error.message,
      generationStatus: 'failed'
    };
  }
}
 
/**
 * Generate certificate for attended user
 */
async function generateCertificate(req, res) {
  try {
    const { userId, eventId, certificateType = 'participant' } = req.body;
    const requesterId = req.user.id;
 
    // Validate inputs
    if (!userId || !eventId) {
      return res.status(400).json({ error: 'User ID and Event ID are required' });
    }
 
    // Check if user has permission (host, co-host, or platform admin)
    const event = await Event.findById(eventId);
    if (!event) {
      return res.status(404).json({ error: 'Event not found' });
    }
 
    const isHost = event.hostUserId.toString() === requesterId;
    const isCoHost = event.coHosts.includes(requesterId);
    const isAdmin = req.user.roles.includes('platformAdmin');
 
    if (!isHost && !isCoHost && !isAdmin) {
      return res.status(403).json({ error: 'Only event hosts, co-hosts, or admins can generate certificates' });
    }
 
    // Check if user attended the event
    const participationLog = await EventParticipationLog.findOne({
      userId,
      eventId,
      status: 'attended'
    });
 
    if (!participationLog) {
      return res.status(400).json({ error: 'User must have attended the event to receive a certificate' });
    }
 
    // Check if certificate already exists
    const existingCertificate = await Certificate.findOne({ userId, eventId });
    if (existingCertificate) {
      return res.status(400).json({ error: 'Certificate already exists for this user and event' });
    }
 
    // Prepare certificate data
    const certificateData = await prepareCertificateData(userId, eventId, certificateType);
 
    // Create certificate record
    const certificate = new Certificate({
      userId,
      eventId,
      type: certificateType,
      status: 'pending',
      certificateData
    });
 
    // Send to ML API
    const mlResponse = await sendToMLAPI(certificateData);
 
    // Update certificate with ML API response
    certificate.mlApiResponse = {
      requestId: mlResponse.requestId,
      generationStatus: mlResponse.generationStatus,
      errorMessage: mlResponse.errorMessage,
      generatedAt: new Date()
    };
 
    if (mlResponse.success) {
      certificate.status = 'generated';
      certificate.certificateURL = mlResponse.certificateURL;
      // Notify user of certificate generation
      const user = await User.findById(userId);
      if (user) {
        await notifyUser({
          userId,
          type: 'certificate',
          message: `Your certificate for ${event.title} is ready!`,
          data: { eventId, certificateId: certificate._id, certificateURL: certificate.certificateURL },
          emailOptions: {
            to: user.email,
            subject: `Your Certificate for ${event.title} is Ready!`,
            html: `<h2>🎉 Your Certificate is Ready!</h2><p>Dear ${user.name},</p><p>Your certificate for <strong>${event.title}</strong> has been generated and is now available.</p><p><strong>Certificate Type:</strong> ${certificate.type}</p><p>You can view and download your certificate from your CampVerse dashboard.</p><br><p>Best regards,<br>CampVerse Team</p>`
          }
        });
      }
    } else {
      certificate.status = 'failed';
    }
 
    await certificate.save();
 
    return res.status(201).json({
      message: 'Certificate generation initiated',
      certificate: {
        id: certificate._id,
        status: certificate.status,
        type: certificate.type,
        certificateURL: certificate.certificateURL,
        issuedAt: certificate.issuedAt
      }
    });
 
  } catch (error) {
    console.error('Certificate generation error:', error);
    return res.status(500).json({ error: 'Error generating certificate' });
  }
}
 
/**
 * Generate certificates for all attended users (batch processing)
 */
async function generateBatchCertificates(req, res) {
  try {
    const { eventId, certificateType = 'participant' } = req.body;
    const requesterId = req.user.id;
 
    // Validate inputs
    if (!eventId) {
      return res.status(400).json({ error: 'Event ID is required' });
    }
 
    // Check permissions
    const event = await Event.findById(eventId);
    if (!event) {
      return res.status(404).json({ error: 'Event not found' });
    }
 
    const isHost = event.hostUserId.toString() === requesterId;
    const isCoHost = event.coHosts.includes(requesterId);
    const isAdmin = req.user.roles.includes('platformAdmin');
 
    if (!isHost && !isCoHost && !isAdmin) {
      return res.status(403).json({ error: 'Only event hosts, co-hosts, or admins can generate batch certificates' });
    }
 
    // Get all attended users for the event
    const attendedUsers = await EventParticipationLog.find({
      eventId,
      status: 'attended'
    }).populate('userId', 'name email');
 
    if (attendedUsers.length === 0) {
      return res.status(400).json({ error: 'No attended users found for this event' });
    }
 
    const results = {
      total: attendedUsers.length,
      generated: 0,
      skipped: 0,
      failed: 0,
      certificates: []
    };
 
    // Process each attended user
    for (const attendance of attendedUsers) {
      try {
        // Check if certificate already exists
        const existingCertificate = await Certificate.findOne({ 
          userId: attendance.userId._id, 
          eventId 
        });
 
        if (existingCertificate) {
          results.skipped++;
          results.certificates.push({
            userId: attendance.userId._id,
            userName: attendance.userId.name,
            userEmail: attendance.userId.email,
            status: 'skipped',
            reason: 'Certificate already exists'
          });
          continue;
        }
 
        // Prepare certificate data
        const certificateData = await prepareCertificateData(
          attendance.userId._id, 
          eventId, 
          certificateType
        );
 
        // Create certificate record
        const certificate = new Certificate({
          userId: attendance.userId._id,
          eventId,
          type: certificateType,
          status: 'pending',
          certificateData
        });
 
        // Send to ML API
        const mlResponse = await sendToMLAPI(certificateData);
 
        // Update certificate with ML API response
        certificate.mlApiResponse = {
          requestId: mlResponse.requestId,
          generationStatus: mlResponse.generationStatus,
          errorMessage: mlResponse.errorMessage,
          generatedAt: new Date()
        };
 
        if (mlResponse.success) {
          certificate.status = 'generated';
          certificate.certificateURL = mlResponse.certificateURL;
          results.generated++;
        } else {
          certificate.status = 'failed';
          results.failed++;
        }
 
        await certificate.save();
 
        results.certificates.push({
          userId: attendance.userId._id,
          userName: attendance.userId.name,
          userEmail: attendance.userId.email,
          status: certificate.status,
          certificateId: certificate._id,
          certificateURL: certificate.certificateURL
        });
 
      } catch (error) {
        console.error(`Error generating certificate for user ${attendance.userId._id}:`, error);
        results.failed++;
        results.certificates.push({
          userId: attendance.userId._id,
          userName: attendance.userId.name,
          userEmail: attendance.userId.email,
          status: 'failed',
          reason: error.message
        });
      }
    }
 
    return res.status(200).json({
      message: 'Batch certificate generation completed',
      eventId,
      eventTitle: event.title,
      results
    });
 
  } catch (error) {
    console.error('Batch certificate generation error:', error);
    return res.status(500).json({ error: 'Error generating batch certificates' });
  }
}
 
/**
 * Get user's certificates
 */
async function getUserCertificates(req, res) {
  try {
    const userId = req.params.userId || req.user.id;
    const certificates = await Certificate.find({ userId })
      .populate('eventId', 'title description schedule')
      .sort({ issuedAt: -1 });
 
    return res.json({ certificates });
  } catch (error) {
    console.error('Error fetching user certificates:', error);
    return res.status(500).json({ error: 'Error fetching certificates' });
  }
}
 
/**
 * Get certificate by ID
 */
async function getCertificateById(req, res) {
  try {
    const certificateId = req.params.id;
    const certificate = await Certificate.findById(certificateId)
      .populate('userId', 'name email')
      .populate('eventId', 'title description schedule organizer');
 
    if (!certificate) {
      return res.status(404).json({ error: 'Certificate not found' });
    }
 
    return res.json({ certificate });
  } catch (error) {
    console.error('Error fetching certificate:', error);
    return res.status(500).json({ error: 'Error fetching certificate' });
  }
}
 
/**
 * Verify certificate using QR code
 */
async function verifyCertificate(req, res) {
  try {
    const { qrCode } = req.body;
 
    if (!qrCode) {
      return res.status(400).json({ error: 'QR code is required' });
    }
 
    // Decode QR code data
    let qrData;
    try {
      qrData = JSON.parse(qrCode);
    } catch (error) {
      return res.status(400).json({ error: 'Invalid QR code format' });
    }
 
    // Find certificate
    const certificate = await Certificate.findOne({
      userId: qrData.userId,
      eventId: qrData.eventId
    }).populate('userId', 'name email')
      .populate('eventId', 'title description schedule');
 
    if (!certificate) {
      return res.status(404).json({ error: 'Certificate not found' });
    }
 
    if (certificate.status !== 'generated') {
      return res.status(400).json({ error: 'Certificate not yet generated' });
    }
 
    return res.json({
      valid: true,
      certificate: {
        userName: certificate.userId.name,
        userEmail: certificate.userId.email,
        eventTitle: certificate.eventId.title,
        eventDate: certificate.eventId.schedule.start,
        certificateType: certificate.type,
        issuedAt: certificate.issuedAt,
        certificateURL: certificate.certificateURL
      }
    });
 
  } catch (error) {
    console.error('Certificate verification error:', error);
    return res.status(500).json({ error: 'Error verifying certificate' });
  }
}
 
/**
 * Export attended users for ML certificate generation (batch processing)
 */
async function exportAttendedUsers(req, res) {
  try {
    const { eventId } = req.params;
    const requesterId = req.user.id;
 
    // Check permissions
    const event = await Event.findById(eventId);
    if (!event) {
      return res.status(404).json({ error: 'Event not found' });
    }
 
    const isHost = event.hostUserId.toString() === requesterId;
    const isCoHost = event.coHosts.includes(requesterId);
    const isAdmin = req.user.roles.includes('platformAdmin');
 
    if (!isHost && !isCoHost && !isAdmin) {
      return res.status(403).json({ error: 'Only event hosts, co-hosts, or admins can export attended users' });
    }
 
    // Get all attended users for the event
    const attendedUsers = await EventParticipationLog.find({
      eventId,
      status: 'attended'
    }).populate('userId', 'name email skills interests')
      .populate('eventId', 'title description schedule tags type');
 
    // Prepare data for ML API
    const exportData = {
      eventId,
      eventTitle: event.title,
      eventDescription: event.description,
      eventDate: event.schedule.start,
      eventLocation: event.location || 'Online',
      organizerName: event.organizer,
      totalAttended: attendedUsers.length,
      attendedUsers: attendedUsers.map(log => ({
        userId: log.userId._id,
        userName: log.userId.name,
        userEmail: log.userId.email,
        userSkills: log.userId.skills || [],
        userInterests: log.userId.interests || [],
        attendanceDate: log.attendanceTimestamp,
        certificateType: 'participant' // Default type
      }))
    };
 
    return res.json({
      message: 'Attended users exported successfully',
      data: exportData,
      count: attendedUsers.length
    });
 
  } catch (error) {
    console.error('Export attended users error:', error);
    return res.status(500).json({ error: 'Error exporting attended users' });
  }
}
 
/**
 * Retry failed certificate generation
 */
async function retryCertificateGeneration(req, res) {
  try {
    const { certificateId } = req.params;
    const requesterId = req.user.id;
 
    const certificate = await Certificate.findById(certificateId)
      .populate('eventId');
 
    if (!certificate) {
      return res.status(404).json({ error: 'Certificate not found' });
    }
 
    // Check permissions
    const isHost = certificate.eventId.hostUserId.toString() === requesterId;
    const isCoHost = certificate.eventId.coHosts.includes(requesterId);
    const isAdmin = req.user.roles.includes('platformAdmin');
 
    if (!isHost && !isCoHost && !isAdmin) {
      return res.status(403).json({ error: 'Only event hosts, co-hosts, or admins can retry certificate generation' });
    }
 
    if (certificate.status !== 'failed') {
      return res.status(400).json({ error: 'Certificate is not in failed status' });
    }
 
    // Retry ML API call
    const mlResponse = await sendToMLAPI(certificate.certificateData);
 
    // Update certificate
    certificate.mlApiResponse = {
      requestId: mlResponse.requestId,
      generationStatus: mlResponse.generationStatus,
      errorMessage: mlResponse.errorMessage,
      generatedAt: new Date()
    };
 
    if (mlResponse.success) {
      certificate.status = 'generated';
      certificate.certificateURL = mlResponse.certificateURL;
    }
 
    await certificate.save();
 
    return res.json({
      message: 'Certificate generation retry completed',
      status: certificate.status,
      certificateURL: certificate.certificateURL
    });
 
  } catch (error) {
    console.error('Retry certificate generation error:', error);
    return res.status(500).json({ error: 'Error retrying certificate generation' });
  }
}
 
/**
 * Get certificate statistics
 */
async function getCertificateStats(req, res) {
  try {
    const userId = req.user.id;
 
    const stats = await Certificate.aggregate([
      { $match: { userId: new require('mongoose').Types.ObjectId(userId) } },
      {
        $group: {
          _id: '$status',
          count: { $sum: 1 }
        }
      }
    ]);
 
    const totalCertificates = await Certificate.countDocuments({ userId });
    const generatedCertificates = await Certificate.countDocuments({ 
      userId, 
      status: 'generated' 
    });
 
    return res.json({
      totalCertificates,
      generatedCertificates,
      pendingCertificates: totalCertificates - generatedCertificates,
      stats
    });
 
  } catch (error) {
    console.error('Certificate stats error:', error);
    return res.status(500).json({ error: 'Error fetching certificate statistics' });
  }
}
 
/**
 * Get certificate generation progress for an event
 */
async function getCertificateProgress(req, res) {
  try {
    const { eventId } = req.params;
    const requesterId = req.user.id;
 
    // Check permissions
    const event = await Event.findById(eventId);
    if (!event) {
      return res.status(404).json({ error: 'Event not found' });
    }
 
    const isHost = event.hostUserId.toString() === requesterId;
    const isCoHost = event.coHosts.includes(requesterId);
    const isAdmin = req.user.roles.includes('platformAdmin');
 
    if (!isHost && !isCoHost && !isAdmin) {
      return res.status(403).json({ error: 'Only event hosts, co-hosts, or admins can view certificate progress' });
    }
 
    // Get total attended users
    const totalAttended = await EventParticipationLog.countDocuments({
      eventId,
      status: 'attended'
    });
 
    // Get certificate statistics
    const certificates = await Certificate.find({ eventId });
    const generated = certificates.filter(c => c.status === 'generated').length;
    const pending = certificates.filter(c => c.status === 'pending').length;
    const failed = certificates.filter(c => c.status === 'failed').length;
    const notGenerated = totalAttended - certificates.length;
 
    const progress = {
      eventId,
      eventTitle: event.title,
      totalAttended,
      certificatesGenerated: generated,
      certificatesPending: pending,
      certificatesFailed: failed,
      certificatesNotGenerated: notGenerated,
      generationProgress: totalAttended > 0 ? Math.round((certificates.length / totalAttended) * 100) : 0,
      successRate: certificates.length > 0 ? Math.round((generated / certificates.length) * 100) : 0
    };
 
    return res.json(progress);
 
  } catch (error) {
    console.error('Certificate progress error:', error);
    return res.status(500).json({ error: 'Error fetching certificate progress' });
  }
}
 
/**
 * Send certificate notification to user
 */
async function sendCertificateNotification(req, res) {
  try {
    const { certificateId } = req.params;
    const requesterId = req.user.id;
 
    const certificate = await Certificate.findById(certificateId)
      .populate('userId', 'name email')
      .populate('eventId', 'title');
 
    if (!certificate) {
      return res.status(404).json({ error: 'Certificate not found' });
    }
 
    // Check permissions
    const isHost = certificate.eventId.hostUserId.toString() === requesterId;
    const isCoHost = certificate.eventId.coHosts.includes(requesterId);
    const isAdmin = req.user.roles.includes('platformAdmin');
 
    if (!isHost && !isCoHost && !isAdmin) {
      return res.status(403).json({ error: 'Only event hosts, co-hosts, or admins can send certificate notifications' });
    }
 
    if (certificate.status !== 'generated') {
      return res.status(400).json({ error: 'Certificate must be generated before sending notification' });
    }
 
    await notifyUser({
      userId: certificate.userId._id,
      type: 'certificate',
      message: `Your certificate for ${certificate.eventId.title} is ready!`,
      data: { eventId: certificate.eventId._id, certificateId: certificate._id, certificateURL: certificate.certificateURL },
      emailOptions: {
        to: certificate.userId.email,
        subject: `Your Certificate for ${certificate.eventId.title} is Ready!`,
        html: `<h2>🎉 Your Certificate is Ready!</h2><p>Dear ${certificate.userId.name},</p><p>Your certificate for <strong>${certificate.eventId.title}</strong> has been generated and is now available.</p><p><strong>Certificate Type:</strong> ${certificate.type}</p><p>You can view and download your certificate from your CampVerse dashboard.</p><br><p>Best regards,<br>CampVerse Team</p>`
      }
    });
 
    return res.json({ 
      message: 'Certificate notification sent successfully',
      recipient: certificate.userId.email
    });
 
  } catch (error) {
    console.error('Send certificate notification error:', error);
    return res.status(500).json({ error: 'Error sending certificate notification' });
  }
}
 
/**
 * Get certificate management dashboard for hosts
 */
async function getCertificateDashboard(req, res) {
  try {
    const requesterId = req.user.id;
    const { eventId, status, certificateType, page = 1, limit = 10 } = req.query;
 
    // Build query for events hosted by the user
    const eventQuery = { hostUserId: requesterId };
    if (eventId) {
      eventQuery._id = eventId;
    }
 
    const events = await Event.find(eventQuery, '_id title schedule');
    const eventIds = events.map(event => event._id);
 
    if (eventIds.length === 0) {
      return res.json({
        certificates: [],
        pagination: {
          page: parseInt(page),
          limit: parseInt(limit),
          total: 0,
          pages: 0
        },
        summary: {
          totalCertificates: 0,
          generated: 0,
          pending: 0,
          failed: 0
        }
      });
    }
 
    // Build certificate query
    const certificateQuery = { eventId: { $in: eventIds } };
    if (status) {
      certificateQuery.status = status;
    }
    if (certificateType) {
      certificateQuery.type = certificateType;
    }
 
    // Calculate pagination
    const skip = (parseInt(page) - 1) * parseInt(limit);
    const totalCertificates = await Certificate.countDocuments(certificateQuery);
 
    // Get certificates with pagination
    const certificates = await Certificate.find(certificateQuery)
      .populate('userId', 'name email')
      .populate('eventId', 'title schedule')
      .sort({ createdAt: -1 })
      .skip(skip)
      .limit(parseInt(limit));
 
    // Get summary statistics
    const summary = await Certificate.aggregate([
      { $match: { eventId: { $in: eventIds } } },
      {
        $group: {
          _id: '$status',
          count: { $sum: 1 }
        }
      }
    ]);
 
    const summaryObj = {
      totalCertificates,
      generated: 0,
      pending: 0,
      failed: 0
    };
 
    summary.forEach(item => {
      summaryObj[item._id] = item.count;
    });
 
    return res.json({
      certificates: certificates.map(cert => ({
        id: cert._id,
        userId: cert.userId._id,
        userName: cert.userId.name,
        userEmail: cert.userId.email,
        eventId: cert.eventId._id,
        eventTitle: cert.eventId.title,
        eventDate: cert.eventId.schedule?.start,
        certificateType: cert.type,
        status: cert.status,
        certificateURL: cert.certificateURL,
        issuedAt: cert.issuedAt,
        createdAt: cert.createdAt
      })),
      pagination: {
        page: parseInt(page),
        limit: parseInt(limit),
        total: totalCertificates,
        pages: Math.ceil(totalCertificates / parseInt(limit))
      },
      summary: summaryObj,
      events: events.map(event => ({
        id: event._id,
        title: event.title,
        date: event.schedule?.start
      }))
    });
 
  } catch (error) {
    console.error('Certificate dashboard error:', error);
    return res.status(500).json({ error: 'Error fetching certificate dashboard' });
  }
}
 
/**
 * Bulk retry failed certificates for an event
 */
async function bulkRetryFailedCertificates(req, res) {
  try {
    const { eventId } = req.params;
    const requesterId = req.user.id;
 
    // Check permissions
    const event = await Event.findById(eventId);
    if (!event) {
      return res.status(404).json({ error: 'Event not found' });
    }
 
    const isHost = event.hostUserId.toString() === requesterId;
    const isCoHost = event.coHosts.includes(requesterId);
    const isAdmin = req.user.roles.includes('platformAdmin');
 
    if (!isHost && !isCoHost && !isAdmin) {
      return res.status(403).json({ error: 'Only event hosts, co-hosts, or admins can retry failed certificates' });
    }
 
    // Get all failed certificates for the event
    const failedCertificates = await Certificate.find({
      eventId,
      status: 'failed'
    });
 
    if (failedCertificates.length === 0) {
      return res.status(400).json({ error: 'No failed certificates found for this event' });
    }
 
    const results = {
      total: failedCertificates.length,
      retried: 0,
      success: 0,
      failed: 0,
      certificates: []
    };
 
    // Retry each failed certificate
    for (const certificate of failedCertificates) {
      try {
        // Retry ML API call
        const mlResponse = await sendToMLAPI(certificate.certificateData);
 
        // Update certificate
        certificate.mlApiResponse = {
          requestId: mlResponse.requestId,
          generationStatus: mlResponse.generationStatus,
          errorMessage: mlResponse.errorMessage,
          generatedAt: new Date()
        };
 
        if (mlResponse.success) {
          certificate.status = 'generated';
          certificate.certificateURL = mlResponse.certificateURL;
          results.success++;
        } else {
          certificate.status = 'failed';
          results.failed++;
        }
 
        await certificate.save();
        results.retried++;
 
        results.certificates.push({
          certificateId: certificate._id,
          status: certificate.status,
          success: mlResponse.success,
          errorMessage: mlResponse.errorMessage
        });
 
      } catch (error) {
        console.error(`Error retrying certificate ${certificate._id}:`, error);
        results.failed++;
        results.certificates.push({
          certificateId: certificate._id,
          status: 'failed',
          success: false,
          errorMessage: error.message
        });
      }
    }
 
    return res.json({
      message: 'Bulk retry completed',
      eventId,
      eventTitle: event.title,
      results
    });
 
  } catch (error) {
    console.error('Bulk retry error:', error);
    return res.status(500).json({ error: 'Error retrying failed certificates' });
  }
}
 
module.exports = {
  generateCertificate,
  getUserCertificates,
  getCertificateById,
  verifyCertificate,
  exportAttendedUsers,
  retryCertificateGeneration,
  getCertificateStats,
  generateBatchCertificates,
  getCertificateProgress,
  sendCertificateNotification,
  getCertificateDashboard,
  bulkRetryFailedCertificates
};