<?php
/**
 * Maintenance schedule helpers.
 *
 * @package MacaHold
 */

defined( 'ABSPATH' ) || exit;

/**
 * Scheduler utilities.
 */
class Maca_Hold_Scheduler {

	/**
	 * Check if current time is inside the configured schedule window.
	 *
	 * @param array<string, mixed> $settings Plugin settings.
	 * @return bool
	 */
	public static function is_within_schedule( array $settings ) {
		$start = self::parse_datetime( $settings['schedule_start'] ?? '' );
		$end   = self::parse_datetime( $settings['schedule_end'] ?? '' );
		$now   = current_datetime()->getTimestamp();

		if ( null === $start && null === $end ) {
			return false;
		}

		if ( null !== $start && $now < $start ) {
			return false;
		}

		if ( null !== $end && $now > $end ) {
			return false;
		}

		return true;
	}

	/**
	 * Parse stored datetime into a timestamp.
	 *
	 * Stored values are site-local datetimes (WordPress timezone).
	 *
	 * @param string $value Datetime string.
	 * @return int|null
	 */
	public static function parse_datetime( $value ) {
		$value = trim( (string) $value );
		if ( '' === $value ) {
			return null;
		}

		$timezone = wp_timezone();
		$datetime = date_create_immutable_from_format( 'Y-m-d\TH:i', $value, $timezone );

		if ( false === $datetime ) {
			$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', $value, $timezone );
		}

		if ( false === $datetime ) {
			$datetime = date_create_immutable( $value, $timezone );
		}

		if ( false === $datetime ) {
			return null;
		}

		return $datetime->getTimestamp();
	}

	/**
	 * Sanitize a datetime-local value as site-local time.
	 *
	 * @param string $value Raw datetime.
	 * @return string
	 */
	public static function sanitize_datetime( $value ) {
		$value = trim( (string) $value );
		if ( '' === $value ) {
			return '';
		}

		$timestamp = self::parse_datetime( $value );
		if ( null === $timestamp ) {
			return '';
		}

		return wp_date( 'Y-m-d\TH:i', $timestamp );
	}

	/**
	 * Format a stored datetime for datetime-local inputs.
	 *
	 * @param string $value Stored datetime.
	 * @return string
	 */
	public static function format_for_input( $value ) {
		$timestamp = self::parse_datetime( $value );
		if ( null === $timestamp ) {
			return '';
		}

		return wp_date( 'Y-m-d\TH:i', $timestamp );
	}

	/**
	 * Human-readable site timezone label from WordPress settings.
	 *
	 * @return string
	 */
	public static function get_timezone_label() {
		$timezone_string = wp_timezone_string();

		if ( '' !== $timezone_string ) {
			return $timezone_string;
		}

		$offset = (float) get_option( 'gmt_offset' );
		$hours  = (int) $offset;
		$mins   = (int) ( abs( $offset - $hours ) * 60 );
		$sign   = $offset >= 0 ? '+' : '-';

		return sprintf( 'UTC%s%d:%02d', $sign, abs( $hours ), $mins );
	}

	/**
	 * Human-readable schedule summary.
	 *
	 * @param array<string, mixed> $settings Plugin settings.
	 * @return string
	 */
	public static function get_summary( array $settings ) {
		$start = $settings['schedule_start'] ?? '';
		$end   = $settings['schedule_end'] ?? '';

		if ( '' === $start && '' === $end ) {
			return __( 'No schedule configured.', 'maca-hold' );
		}

		if ( '' !== $start && '' !== $end ) {
			return sprintf(
				/* translators: 1: start datetime, 2: end datetime */
				__( 'Active from %1$s until %2$s.', 'maca-hold' ),
				self::format_for_display( $start ),
				self::format_for_display( $end )
			);
		}

		if ( '' !== $start ) {
			return sprintf(
				/* translators: %s: start datetime */
				__( 'Active from %s onward.', 'maca-hold' ),
				self::format_for_display( $start )
			);
		}

		return sprintf(
			/* translators: %s: end datetime */
			__( 'Active until %s.', 'maca-hold' ),
			self::format_for_display( $end )
		);
	}

	/**
	 * Format datetime for display.
	 *
	 * @param string $value Stored datetime.
	 * @return string
	 */
	private static function format_for_display( $value ) {
		$timestamp = self::parse_datetime( $value );
		if ( null === $timestamp ) {
			return $value;
		}

		return wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp );
	}
}
