<?php

// load reward details
$rewardsConfig   = pluginHelper::pluginSpecificConfiguration('rewards');
$rewardsSettings = json_decode($rewardsConfig['data']['plugin_settings'], true);

$Auth = $params['Auth'];
$file = $params['file'];

// make sure the file is associated with a user
if ((int) $file->userId > 0)
{
    // log download
    if (($Auth->loggedIn == true) && (($Auth->id == $file->userId) || ($Auth->level == 'admin')))
    {
        // ignore - this was triggered by an admin user or is owned by the current logged in user
    }
    else
    {
        // check filesize
        $count         = false;
        $countFilesize = (int) $rewardsSettings['ppd_min_file_size'];
        if ($countFilesize == 0)
        {
            $count = true;
        }
        else
        {
            // check download size counts
            if(($countFilesize*1024*1024) <= $file->fileSize)
            {
                $count = true;
            }
        }

        // log download
        if ($count == true)
        {
            // log
            $db = Database::getDatabase();

            // check whether the user has already downloaded today
            $usersIp = Stats::getIP();
            $sql     = "SELECT * FROM plugin_reward_ppd_detail WHERE download_ip = " . $db->quote($usersIp) . " AND file_id = " . $file->id . " AND DATE(download_date) = " . $db->quote(date('Y-m-d'));
            $row     = $db->getRows($sql);
            if (COUNT($row) == 0)
            {
                // lookup country group
                $countryGroupId = null;
                $rewardAmount   = false;
                $country        = Stats::getCountry($usersIp);
                $groupCountry   = $db->getRow("SELECT id, group_id FROM plugin_reward_ppd_group_country WHERE country_code = " . $db->quote($country) . " LIMIT 1");
                if ($groupCountry)
                {
                    $countryGroupId = $groupCountry['group_id'];

                    // lookup reward amount
                    $rewardAmount = $db->getValue('SELECT payout_rate FROM plugin_reward_ppd_group WHERE id=' . (int) $countryGroupId . ' LIMIT 1');
                }

                if ($rewardAmount === false)
                {
                    // load the lowest outpayment group and use this
                    $rewardDefault = $db->getRow('SELECT id, payout_rate FROM plugin_reward_ppd_group ORDER BY payout_rate ASC LIMIT 1');
                    if ($rewardDefault)
                    {
                        $countryGroupId = $rewardDefault['id'];
                        $rewardAmount   = $rewardDefault['payout_rate'];
                    }
                }

                // break download $rewardAmount into thousands
                $rewardAmount = $rewardAmount / 1000;

                // add entry
                $sql  = "INSERT INTO plugin_reward_ppd_detail (reward_user_id, download_ip, file_id, download_country_group_id, download_date, reward_amount, status)
                    VALUES (:reward_user_id, :download_ip, :file_id, :download_country_group_id, :download_date, :reward_amount, 'pending')";
                $vals = array('reward_user_id'            => $file->userId,
                    'download_ip'               => $usersIp,
                    'file_id'                   => $file->id,
                    'download_country_group_id' => $countryGroupId,
                    'download_date'             => sqlDateTime(),
                    'reward_amount'             => $rewardAmount);
                $db->query($sql, $vals);
            }
        }
    }
}