<?php

defined( 'ABSPATH' ) || exit;

// Fallback tide helpers used by the free build (no premium gating).
// inc/tide-loader-fallback.php.

if ( ! function_exists( 'mwpl_format_tide_time_local' ) ) {
	function mwpl_format_tide_time_local( $utc_time, $location_tz = 'UTC' ) {
		try {
			$tz = new DateTimeZone( $location_tz );
			$dt = new DateTime( $utc_time, new DateTimeZone( 'UTC' ) );
			$dt->setTimezone( $tz );
			return $dt->format( 'g:i A' );
		} catch ( Exception $e ) {
			return $utc_time;
		}
	}
}

// Default percent_between before we calculate anything.
$mwpl_percent_between = 0.5;

// Global tide data array used by the cards.
$mwpl_tide_data = array(
	'current_tide'    => null,
	'next_high'       => null,
	'next_low'        => null,
	'trend'           => null,
	'percent_between' => max( 0, min( 1, $mwpl_percent_between ) ),
	'timezone'        => isset( $marine_data['timezone'] ) ? $marine_data['timezone'] : 'UTC',
);

if (
	! empty( $marine_data['hourly']['time'] )
	&& ! empty( $marine_data['hourly']['sea_level_height_msl'] )
) {
	$mwpl_hours = $marine_data['hourly']['time'];
	$mwpl_tides = $marine_data['hourly']['sea_level_height_msl'];

	$mwpl_now           = time();
	$mwpl_current_index = 0;

	// Find the index of the last known tide before "now".
	foreach ( $mwpl_hours as $mwpl_i => $mwpl_ts ) {
		if ( strtotime( $mwpl_ts ) > $mwpl_now ) {
			$mwpl_current_index = max( 0, $mwpl_i - 1 );
			break;
		}
	}

	$mwpl_current_tide = $mwpl_tides[ $mwpl_current_index ];
	$mwpl_trend        = ( $mwpl_tides[ $mwpl_current_index + 1 ] > $mwpl_current_tide ) ? 'Rising' : 'Falling';

	$mwpl_next_high = array(
		'value' => $mwpl_current_tide,
		'index' => $mwpl_current_index,
	);
	$mwpl_next_low  = array(
		'value' => $mwpl_current_tide,
		'index' => $mwpl_current_index,
	);

	// Look ahead up to 48 hours for next high/low.
	for ( $mwpl_i = $mwpl_current_index; $mwpl_i < min( count( $mwpl_tides ) - 1, $mwpl_current_index + 48 ); $mwpl_i++ ) {
		if ( $mwpl_tides[ $mwpl_i ] > $mwpl_next_high['value'] ) {
			$mwpl_next_high = array(
				'value' => $mwpl_tides[ $mwpl_i ],
				'index' => $mwpl_i,
			);
		}
		if ( $mwpl_tides[ $mwpl_i ] < $mwpl_next_low['value'] ) {
			$mwpl_next_low = array(
				'value' => $mwpl_tides[ $mwpl_i ],
				'index' => $mwpl_i,
			);
		}
	}

	$mwpl_from = ( 'Rising' === $mwpl_trend ) ? $mwpl_next_low : $mwpl_next_high;
	$mwpl_to   = ( 'Rising' === $mwpl_trend ) ? $mwpl_next_high : $mwpl_next_low;

	$mwpl_percent_between = ( $mwpl_to['index'] !== $mwpl_from['index'] )
		? ( $mwpl_current_index - $mwpl_from['index'] ) / max( 1, ( $mwpl_to['index'] - $mwpl_from['index'] ) )
		: 0.5;

	$mwpl_tide_data = array(
		'current_tide'    => round( $mwpl_current_tide, 2 ),
		'next_high'       => array(
			'time'  => $mwpl_hours[ $mwpl_next_high['index'] ],
			'local' => mwpl_format_tide_time_local(
				$mwpl_hours[ $mwpl_next_high['index'] ],
				$marine_data['timezone']
			),
			'value' => round( $mwpl_next_high['value'], 2 ),
		),
		'next_low'        => array(
			'time'  => $mwpl_hours[ $mwpl_next_low['index'] ],
			'local' => mwpl_format_tide_time_local(
				$mwpl_hours[ $mwpl_next_low['index'] ],
				$marine_data['timezone']
			),
			'value' => round( $mwpl_next_low['value'], 2 ),
		),
		'trend'           => $mwpl_trend,
		'percent_between' => max( 0, min( 1, $mwpl_percent_between ) ),
		'timezone'        => $marine_data['timezone'],
		'tide_source'     => 'Open-Meteo',
	);
}