<?php
/**
 * ------------------------------------------------------------------------
 * WT Kunena Birthdays Module
 * ------------------------------------------------------------------------
 * Copyright (C) 2009-2025 Wave Theme. All Rights Reserved.
 * @license - GNU/GPL, http://www.gnu.org/licenses/gpl.html
 * Author: Wave Theme Template Club
 * Websites: http://www.wavetheme.com - http://www.9themestore.com
 * ------------------------------------------------------------------------
 */

use Joomla\CMS\Factory;
use Joomla\CMS\Component\ComponentHelper;
use Kunena\Forum\Libraries\Route\KunenaRoute;

defined('_JEXEC') or die;

class ModWTKunenaBirthdaysHelper
{
  public static function getBirthdayUsers($params)
  {
    if (!ComponentHelper::isInstalled('com_kunena') || !ComponentHelper::getComponent('com_kunena', true)->enabled) {
      return 'error_nokunena';
    }

    $db = Factory::getDbo();
    $today = Factory::getDate()->format('m-d');
    $limit = (int) $params->get('limit', 10);

    $query = $db->getQuery(true)
      ->select('u.id, u.username, u.name, ku.birthdate, ku.avatar, s.userid AS online')
      ->from('#__users AS u')
      ->join('LEFT', '#__kunena_users AS ku ON ku.userid = u.id')
      ->join('LEFT', '#__session AS s ON s.userid = u.id AND s.client_id = 0')
      ->where("DATE_FORMAT(ku.birthdate, '%m-%d') = " . $db->quote($today))
      ->order('u.name ASC')
      ->setLimit($limit);

    $db->setQuery($query);
    $users = $db->loadObjectList();

    return $users ?: 'error_nousers';
  }

  public static function getProfileLink($userId)
  {


    return KunenaRoute::_("index.php?option=com_kunena&view=user&userid=$userId");
  }

  public static function getInitial($name)
  {
    return strtoupper(mb_substr(trim($name), 0, 1));
  }

  public static function getColorFromUser($userKey)
  {
    $colors = ['#FF6F61', '#6B5B95', '#88B04B', '#F7CAC9', '#92A8D1', '#955251', '#009B77'];

    $index = crc32($userKey) % count($colors);
    return $colors[$index];
  }
}
