<?php

/*
 * @component UserPoints
 * @copyright Copyright (C) 2015-2022 Bernard Gilly - Adrien Roussel - Martin Brampton
 * @license : GNU/GPL
 * @Website : https://remository.com
 */
namespace BlackSheepResearch\Component\UserPoints\Administrator\Models;

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

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

class Archive extends Model {

    function __construct() {
        parent::__construct();
    }

    function _archive() {
        $app = Factory::getApplication();

        $db = SQL::getInstance();

        $testDate = 1;

        $dateCombine = Factory::getApplication()->input->get('datestart', '', 'cmd');

        if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $dateCombine)) {
            $testDate = 1;
        } else {
            $testDate = 0;
        }

        if ($dateCombine != '' && $testDate == 1) {
            $dateCombine = $dateCombine . ' 00:00:00';

            // create a combined activity
            $query = "SELECT SUM(points) AS sumAllPoints, referreid FROM #__alpha_userpoints_details WHERE insert_date < {$db->quote($dateCombine)} AND `enabled`='1' GROUP BY referreid";
            $db->setQuery($query);
            $resultCombined = $db->loadObjectList();
            // archive
            $query = "INSERT INTO #__alpha_userpoints_details_archive SELECT * FROM #__alpha_userpoints_details AS a WHERE a.insert_date < {$db->quote($dateCombine)}";
            $db->setQuery($query);
            $db->execute();
            // delete			
            $query = "DELETE FROM #__alpha_userpoints_details WHERE insert_date < {$db->quote($dateCombine)}";
            $db->setQuery($query);
            $db->execute();
            // optimize
            $query = "OPTIMIZE TABLE #__alpha_userpoints_details";
            $db->setQuery($query);
            $db->execute();

            foreach ($resultCombined as $combined) {
                if ($combined->referreid != '') {
                    $detail = new PointsDetails();
                    // save new points into alpha_userpointsdetails table
                    $detail->referreid = $combined->referreid;
                    $detail->setPointsWithMax($combined->referreid, $combined->sumAllPoints);
                    $detail->insert_date = $dateCombine;
                    $detail->expire_date = '';
                    $detail->rule_id = 'sysplgup_archive';
                    $detail->approved = 1;
                    $detail->status = 1;
                    $detail->keyreference = '';
                    $date = date(Text::_('DATE_FORMAT_LC2'), strtotime(SQL::dateSQL()));  // HTMLHelper::_('date', $dateCombine, Text::_('DATE_FORMAT_LC2'))
                    $detail->datareference = sprintf(Text::_('AUP_COMBINED_ACTIVITIES_BEFORE_DATE'), $date);
                    $detail->enabled = 1;
                    $detail->save();
                }
            }
            $app->enqueueMessage(Text::_('AUP_ACTIVITIES_COMBINED_SUCCESSFULLY'));
        } else {
            $app->enqueueMessage(Text::_('AUP_MISSING_DATE'));
        }

        return;
    }

}
