<?php
defined( 'ABSPATH' ) || exit;

// Icon pack used by icon-loader.
$icon_pack = 'erikflowers'; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

// Erik Flowers icon font.
wp_enqueue_style(
	'erik-icons-core',
	plugin_dir_url( __FILE__ ) . '../weather-icons/css/weather-icons.css',
	array(),
	'1.0.0'
);

// Card 41 CSS.
wp_enqueue_style(
	'mw-card41-style',
	plugin_dir_url( __FILE__ ) . '../assets/card/card41-style.css',
	array(),
	time()
);

// Helpers.
require_once plugin_dir_path( __FILE__ ) . '../inc/icon-loader.php';
require_once plugin_dir_path( __FILE__ ) . '../inc/utilities.php';

// ------------------------------------------------------------------
// Core data
// ------------------------------------------------------------------
$mwpl_current = $weather['current'] ?? array();
$mwpl_daily   = $weather['daily']  ?? array();

// Label + units.
$mwpl_label_raw   = $atts['label'] ?? "{$atts['lat']}, {$atts['lon']}";
$mwpl_unit        = $atts['units'] ?? 'metric';
$mwpl_unit_symbol = ( 'imperial' === $mwpl_unit ) ? '°F' : '°C';

$mwpl_lat = $atts['lat'] ?? '';
$mwpl_lon = $atts['lon'] ?? '';

// Timezone info for local clock (same pattern as card 36).
$mwpl_tz = function_exists( 'mwpl_get_location_time' )
	? mwpl_get_location_time( $mwpl_lat, $mwpl_lon )
	: array();

$mwpl_tz_offset = isset( $mwpl_tz['offset'] ) ? (int) $mwpl_tz['offset'] : 0;

$mwpl_date_label = mwpl_gmdate( 'l, j F' );

// Main values.
$mwpl_temp    = isset( $mwpl_current['temp'] ) ? round( $mwpl_current['temp'] ) : null;
$mwpl_feels   = isset( $mwpl_current['feels_like'] ) ? round( $mwpl_current['feels_like'] ) : null;
$mwpl_desc    = isset( $mwpl_current['weather'][0]['description'] )
	? ucwords( $mwpl_current['weather'][0]['description'] )
	: '';
$mwpl_icon    = isset( $mwpl_current['weather'][0]['icon'] )
	? mw_erikflowers_icon_map( $mwpl_current['weather'][0]['icon'] )
	: 'wi-na';

// Today hi/lo.
$mwpl_today = $mwpl_daily[0] ?? null;
$mwpl_hi    = null;
$mwpl_lo    = null;

if ( $mwpl_today && isset( $mwpl_today['temp']['max'], $mwpl_today['temp']['min'] ) ) {
	$mwpl_hi = round( $mwpl_today['temp']['max'] );
	$mwpl_lo = round( $mwpl_today['temp']['min'] );
}

// Other stats.
$mwpl_humidity  = isset( $mwpl_current['humidity'] ) ? (int) $mwpl_current['humidity'] : null;
$mwpl_pressure  = isset( $mwpl_current['pressure'] ) ? (int) $mwpl_current['pressure'] : null;
$mwpl_visibility = isset( $mwpl_current['visibility'] ) ? $mwpl_current['visibility'] / 1000 : null;

// Wind (speed only – keep this one simpler than 36).
$mwpl_wind_label = 'Wind: --';
if ( isset( $mwpl_current['wind_speed'] ) ) {
	$mwpl_speed_mps  = (float) $mwpl_current['wind_speed'];
	$mwpl_wind_speed = ( 'imperial' === $mwpl_unit )
		? round( $mwpl_speed_mps * 2.241, 1 ) . ' mph'
		: round( $mwpl_speed_mps * 3.6, 1 ) . ' km/h';

	$mwpl_wind_label = 'Wind: ' . $mwpl_wind_speed;
}

// Sunrise / sunset.
$mwpl_sunrise = $mwpl_current['sunrise'] ?? null;
$mwpl_sunset  = $mwpl_current['sunset']  ?? null;

// Last-updated timestamp.
$mwpl_last_ts     = isset( $mwpl_current['dt'] ) ? (int) $mwpl_current['dt'] : null;
$mwpl_minutes_ago = $mwpl_last_ts ? floor( ( time() - $mwpl_last_ts ) / 60 ) : null;
?>

<div class="mw-card card-41 mw41-shell"
	data-card="<?php echo esc_attr( isset( $card ) ? intval( $card ) : 41 ); ?>"
	data-lat="<?php echo esc_attr( $mwpl_lat ); ?>"
	data-lon="<?php echo esc_attr( $mwpl_lon ); ?>"
    data-tzoffset="<?php echo esc_attr( $mwpl_tz_offset ); ?>">

	<div class="mw41-circle">
		<div class="mw41-inner">
			<!-- Top: location + date + local time -->
			<div class="mw41-top">
				<div class="mw41-location">
					<div class="mw41-city">
						<?php echo esc_html( $mwpl_label_raw ); ?>
					</div>
					<div class="mw41-date">
						<?php echo esc_html( $mwpl_date_label ); ?>
					</div>
				</div>

				<div class="mw41-clock mw-local-time"
                <?php if ( ! empty( $mwpl_tz['name'] ) ) : ?>
                    data-timezone="<?php echo esc_attr( $mwpl_tz['name'] ); ?>"
                <?php endif; ?>>
                <span class="mw41-clock-label">Local time</span>
                <span class="mw41-clock-time mw-live-time">--:--</span>
            </div>

			<!-- Middle: big icon + temp -->
			<div class="mw41-middle">
				<div class="mw41-icon-wrap">
					<i class="wi <?php echo esc_attr( $mwpl_icon ); ?>"></i>
				</div>

				<div class="mw41-temp-block">
					<div class="mw41-temp-main">
						<?php if ( null !== $mwpl_temp ) : ?>
							<?php echo esc_html( $mwpl_temp ); ?><span class="mw41-unit"><?php echo esc_html( $mwpl_unit_symbol ); ?></span>
						<?php else : ?>
							<span class="mw41-temp-missing">--</span>
						<?php endif; ?>
					</div>

					<?php if ( null !== $mwpl_feels ) : ?>
						<div class="mw41-feels">
							Feels like <?php echo esc_html( $mwpl_feels . $mwpl_unit_symbol ); ?>
						</div>
					<?php endif; ?>

					<?php if ( ! empty( $mwpl_desc ) ) : ?>
						<div class="mw41-desc">
							<?php echo esc_html( $mwpl_desc ); ?>
						</div>
					<?php endif; ?>
				</div>
			</div>

			<!-- Ring hi/lo -->
			<?php if ( null !== $mwpl_hi || null !== $mwpl_lo ) : ?>
				<div class="mw41-ring-row">
					<?php if ( null !== $mwpl_hi ) : ?>
						<div class="mw41-ring-item mw41-hi">
							H: <?php echo esc_html( $mwpl_hi . $mwpl_unit_symbol ); ?>
						</div>
					<?php endif; ?>
					<?php if ( null !== $mwpl_lo ) : ?>
						<div class="mw41-ring-item mw41-lo">
							L: <?php echo esc_html( $mwpl_lo . $mwpl_unit_symbol ); ?>
						</div>
					<?php endif; ?>
				</div>
			<?php endif; ?>

			<!-- Bottom: stats -->
			<div class="mw41-bottom">
				<div class="mw41-row">
					<?php if ( null !== $mwpl_sunrise ) : ?>
						<div class="mw41-stat">
							<i class="wi wi-sunrise"></i>
							<span class="local-time"
								  data-utc="<?php echo esc_attr( $mwpl_sunrise ); ?>">
								<?php echo esc_html( mwpl_gmdate( 'g:i A', $mwpl_sunrise ) ); ?>
							</span>
						</div>
					<?php endif; ?>
					<?php if ( null !== $mwpl_sunset ) : ?>
						<div class="mw41-stat">
							<i class="wi wi-sunset"></i>
							<span class="local-time"
								  data-utc="<?php echo esc_attr( $mwpl_sunset ); ?>">
								<?php echo esc_html( mwpl_gmdate( 'g:i A', $mwpl_sunset ) ); ?>
							</span>
						</div>
					<?php endif; ?>
				</div>

				<div class="mw41-row">
					<div class="mw41-stat">
						<i class="wi wi-strong-wind"></i>
						<?php echo esc_html( $mwpl_wind_label ); ?>
					</div>

					<?php if ( null !== $mwpl_humidity ) : ?>
						<div class="mw41-stat">
							<i class="wi wi-humidity"></i>
							<?php echo esc_html( $mwpl_humidity ); ?>% RH
						</div>
					<?php endif; ?>
				</div>

				<div class="mw41-row mw41-row-small">
					<?php if ( null !== $mwpl_pressure ) : ?>
						<div class="mw41-stat">
							<i class="wi wi-barometer"></i>
							<?php echo esc_html( $mwpl_pressure ); ?> hPa
						</div>
					<?php endif; ?>

					<?php if ( null !== $mwpl_visibility ) : ?>
						<div class="mw41-stat">
							<i class="wi wi-day-haze"></i>
							Vis: <?php echo esc_html( $mwpl_visibility ); ?> km
						</div>
					<?php endif; ?>
				</div>
			</div>

			<?php if ( $mwpl_last_ts ) : ?>
				<div class="mw41-updated">
					🕒 Updated
					<span class="local-time" data-utc="<?php echo esc_attr( $mwpl_last_ts ); ?>">
						<?php echo esc_html( mwpl_gmdate( 'g:i A', $mwpl_last_ts ) ); ?>
					</span>
					<?php if ( $mwpl_minutes_ago >= 1 ) : ?>
						<span class="mw41-ago">
							(<?php echo esc_html( $mwpl_minutes_ago ); ?> min<?php echo ( $mwpl_minutes_ago > 1 ) ? 's' : ''; ?> ago)
						</span>
					<?php endif; ?>
				</div>
			<?php endif; ?>

		</div><!-- /.mw41-inner -->
	</div><!-- /.mw41-circle -->
</div><!-- /.mw-card.card-41 -->
