<?php

namespace AIEO\Claude\Abilities;

defined( 'ABSPATH' ) || exit;

class PeriodHelper {

	const ALLOWED = array( 'today', 'yesterday', 'last_7_days', 'last_30_days', 'last_90_days', 'this_month', 'last_month' );

	
	public static function resolve( $period ) {
		$period = is_string( $period ) ? sanitize_key( $period ) : 'last_7_days';
		if ( ! in_array( $period, self::ALLOWED, true ) ) {
			$period = 'last_7_days';
		}
		$now    = current_time( 'timestamp', true ); 
		$today  = strtotime( gmdate( 'Y-m-d 00:00:00', $now ) . ' UTC' );
		switch ( $period ) {
			case 'today':
				$since = $today;
				$until = $now;
				break;
			case 'yesterday':
				$since = $today - DAY_IN_SECONDS;
				$until = $today - 1;
				break;
			case 'last_30_days':
				$since = $today - 30 * DAY_IN_SECONDS;
				$until = $now;
				break;
			case 'last_90_days':
				$since = $today - 90 * DAY_IN_SECONDS;
				$until = $now;
				break;
			case 'this_month':
				$since = strtotime( gmdate( 'Y-m-01 00:00:00', $now ) . ' UTC' );
				$until = $now;
				break;
			case 'last_month':
				$first_this = strtotime( gmdate( 'Y-m-01 00:00:00', $now ) . ' UTC' );
				$since      = strtotime( '-1 month', $first_this );
				$until      = $first_this - 1;
				break;
			case 'last_7_days':
			default:
				$since = $today - 7 * DAY_IN_SECONDS;
				$until = $now;
				break;
		}
		return array(
			'since' => gmdate( 'Y-m-d H:i:s', $since ),
			'until' => gmdate( 'Y-m-d H:i:s', $until ),
			'label' => $period,
		);
	}
}
