<?php

/**
 * @package		UserPoints for Joomla 4.x
 * @copyright	Copyright (C) 2015-2016. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */
// no direct access
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;
use BlackSheepResearch\Component\UserPoints\Site\Problems\SQL;

class modUserPointsCouponCodeHelper {

    public static function checkcoupon($params, $coupon) {

        $app = Factory::getApplication();

        // check if user is logged in		
        $user = UserPoints::getUser();
        if (!$user->id) {
            echo "<script> alert('" . Text::_('MODAUP_CP_YOU_MUST_BE_LOGGED') . "'); </script>";
            return;
        }

        // insert API AlphaUserPoint
        $db = SQL::getInstance();

        $now = SQL::datetimeSQL();

        $query = "SELECT * FROM #__alpha_userpoints_coupons WHERE `couponcode` LIKE {$db->quote($coupon)}"
                . " AND (`expires` >= '$now' OR `expires`='1000-01-01 00:00:00')";

        $db->setQuery($query);
        $result = $db->loadObjectList();
        if ($result) {
            $resultCouponExist = 0;
            // active user		
            $referrerid = UserPoints::getReferreid();

            // check if public or private coupon
            if (!$result[0]->public) {
                // private -> usable once per one user
                $query = "SELECT count(*) FROM #__alpha_userpoints_details WHERE `keyreference` LIKE {$db->quote($coupon)} AND `enabled` = 1";
                $db->setQuery($query);
                $resultCouponExist = $db->loadResult();
                if (!$resultCouponExist) {
                    // insert points
                    UserPoints::newpoints('sysplgup_couponpointscodes', $referrerid, $result[0]->couponcode, $result[0]->description, $result[0]->points);
                    $app->enqueueMessage(Text::_('MODAUP_CP_COUPON_REDEEMED'));
                } else {
                    $app->enqueueMessage(Text::_('MODAUP_CP_THIS_COUPON_WAS_ALREADY_USED'));
                }
            } elseif ($result[0]->public) {
                // public -> usable once per all users
                $keyreference = $coupon . "##" . $user->id;
                $query = "SELECT count(*) FROM #__alpha_userpoints_details WHERE `keyreference` LIKE {$db->quote($keyreference)} AND `enabled` = 1";
                $db->setQuery($query);
                $resultCouponExist = $db->loadResult();
                if (!$resultCouponExist) {
                    // insert points
                    UserPoints::newpoints('sysplgup_couponpointscodes', $referrerid, $keyreference, $result[0]->description, $result[0]->points);
                } else {
                    $app->enqueueMessage(Text::_('MODAUP_CP_THIS_COUPON_WAS_ALREADY_USED'));
                }
            }
        } else {
            $app->enqueueMessage(Text::_('MODAUP_CP_PLEASE_CHECK_YOUR_COUPON'));
            return;
        }
    }
}
