<?php
namespace SPAATG\Model\AdminNotices;

if ( ! defined( 'ABSPATH' ) ) {
 exit; // Exit if accessed directly.
}

use SPAATG\Controller\StatsController as StatsController;
use SPAATG\Controller\QuotaController as QuotaController;

class QuotaNoticeMonth extends \SPAATG\Model\AdminNoticeModel
{
	protected $key = 'MSG_UPGRADE_MONTH';

	public function load()
	{
    parent::load();
	}

	protected function checkTrigger()
	{
			$quotaController = QuotaController::getInstance();

			if ($quotaController->hasQuota() === false)
				return false;

			$quotaData = $quotaController->getQuota();

			if ($this->monthlyUpgradeNeeded($quotaData) === false)
				return false;

			$this->addData('average', $this->getMonthAverage());
			$this->addData('month_total', $quotaData->monthly->total);
			$this->addData('onetime_remaining', $quotaData->onetime->remaining);

	}

	protected function getMessage()
	{
		$quotaController = QuotaController::getInstance();

		$quotaData = $quotaController->getQuota();
		$average = $this->getMonthAverage(); // $this->getData('average');
		$month_total = $quotaData->monthly->total;// $this->getData('month_total');
		$onetime_remaining = $quotaData->onetime->remaining; //$this->getData('onetime_remaining'); */

		/* translators: 1, 3: HTML strong tags, 2: average image count, 4: monthly plan quota, 5: one-time images count, 6: HTML line break. */
		$message = '<p>' . sprintf(__('You add an average of %1$s %2$d images and thumbnails %3$s to your Media Library every month and you have <strong>a plan of %4$d images/month (and %5$d one-time images)</strong>.%6$s You may need to upgrade your plan to have all your images optimized.', 'shortpixel-ai-alt-text-generator'), '<strong>', $average, '</strong>', $month_total, $onetime_remaining, '<br>') . '</p>';

		return $message;
	}

	protected function getMonthAverage() {
			$stats = StatsController::getInstance();

			// Count how many months have some optimized images.
			for($i = 4, $count = 0; $i>=1; $i--) {
					if($count == 0 && $stats->find('period', 'months', $i) == 0)
					{
						continue;
					}
					$count++;

			}
			// Sum last 4 months, and divide by number of active months to get number of avg per active month.
			return ($stats->find('period', 'months', 1) + $stats->find('period', 'months', 2) + $stats->find('period', 'months', 3) + $stats->find('period', 'months', 4) / max(1,$count));
	}

	protected function monthlyUpgradeNeeded($quotaData)
	{
			if  (isset($quotaData->monthly->total))
			{
					$monthAvg = $this->getMonthAverage($quotaData);
					// +20 I suspect to not trigger on very low values of monthly use(?)
					$threshold = $quotaData->monthly->total + ($quotaData->onetime->remaining / 6 ) +20;

					if ($monthAvg > $threshold)
					{
							return true;
					}
			}
			return false;
	}
} // class
