<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * SmallPict_Quota_Manager
 *
 * Tracks local usage statistics for informational display only.
 * Actual quota enforcement is handled server-side by the SmallPict API.
 * This class does NOT block any functionality based on usage counters.
 */
class SmallPict_Quota_Manager {

	/**
	 * Get the monthly quota limit in bytes (for display purposes only).
	 * The authoritative limit is enforced by the backend API.
	 */
	public static function get_quota_limit() {
		$limits = get_option( 'smallpict_account_limits', array() );
		if ( isset( $limits['monthly_quota_mb'] ) ) {
			return (int) $limits['monthly_quota_mb'] * 1024 * 1024;
		}

		// Default display fallback (Free Plan)
		return 20 * 1024 * 1024;
	}

	/**
	 * Record usage after successful optimization.
	 * Used only for local display in the Usage & Quota section.
	 */
	public static function record_usage( $image_size_bytes ) {
		$tracking = self::get_tracking_data();

		// Auto-rotation check
		if ( time() > $tracking['next_reset'] ) {
			$tracking = self::rotate_period( $tracking );
		}

		$tracking['bytes'] += $image_size_bytes;
		update_option( 'smallpict_usage_tracking', $tracking );
	}

	/**
	 * Get usage percentage for UI display only.
	 */
	public static function get_usage_percent() {
		$limit_bytes = self::get_quota_limit();
		if ( $limit_bytes <= 0 ) {
			return 0;
		}

		$tracking = self::get_tracking_data();

		if ( time() > $tracking['next_reset'] ) {
			$tracking = self::rotate_period( $tracking );
		}

		$percent = ( $tracking['bytes'] / $limit_bytes ) * 100;
		return min( 100, round( $percent, 1 ) );
	}

	// --- Tracking Internals ---

	public static function get_tracking_data() {
		$tracking = get_option( 'smallpict_usage_tracking', array() );

		if ( empty( $tracking ) || ! isset( $tracking['next_reset'] ) ) {
			// Try migration from old format
			$old_usage   = get_option( 'smallpict_monthly_usage' );
			$start_bytes = 0;
			if ( $old_usage && isset( $old_usage['month'] ) && $old_usage['month'] === gmdate( 'Y-m' ) ) {
				$start_bytes = (int) $old_usage['bytes'];
			}

			return self::init_tracking( $start_bytes );
		}

		return $tracking;
	}

	public static function init_tracking( $start_bytes = 0, $anchor_day = null ) {
		if ( ! $anchor_day ) {
			$anchor_day = (int) gmdate( 'j' );
		}

		$next_reset = self::calculate_next_reset( $anchor_day );

		$tracking = array(
			'bytes'              => $start_bytes,
			'billing_anchor_day' => $anchor_day,
			'next_reset'         => $next_reset,
		);

		update_option( 'smallpict_usage_tracking', $tracking );
		return $tracking;
	}

	public static function rotate_period( $tracking ) {
		$tracking['bytes']      = 0;
		$tracking['next_reset'] = self::calculate_next_reset( $tracking['billing_anchor_day'] );
		update_option( 'smallpict_usage_tracking', $tracking );
		return $tracking;
	}

	public static function calculate_next_reset( $anchor_day ) {
		$current_month = (int) gmdate( 'n' );
		$current_year  = (int) gmdate( 'Y' );

		$target_month = $current_month + 1;
		$target_year  = $current_year;
		if ( $target_month > 12 ) {
			$target_month = 1;
			++$target_year;
		}

		$days_in_target  = (int) gmdate( 't', mktime( 0, 0, 0, $target_month, 1, $target_year ) );
		$target_day      = min( $anchor_day, $days_in_target );
		$target_date_str = "$target_year-$target_month-$target_day 00:00:00";
		return strtotime( $target_date_str );
	}

	public static function get_quota_display() {
		return round( self::get_quota_limit() / ( 1024 * 1024 ) );
	}
}
