<?php

defined('ABSPATH') || exit;

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

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

// Load this card's style
wp_enqueue_style(
    'mw-card5-style',
    plugin_dir_url(__FILE__) . '../assets/card/card5-style.css',
    array(),
    time()
);

// JS to convert data-utc → local time for sunrise/sunset
if ( ! wp_script_is('mw-localtime-js', 'enqueued') ) {
    wp_enqueue_script(
        'mw-localtime-js',
        plugin_dir_url(__FILE__) . '../js/localtime.js',
        array(),
        time(),
        true
    );
}

// Load icon logic + utilities
require_once plugin_dir_path(__FILE__) . '../inc/icon-loader.php';
require_once plugin_dir_path(__FILE__) . '../inc/utilities.php'; // moon class generator

// Pull data (prefixed for PHPCS)
$mwpl_current = $weather['current'];
$mwpl_tz_offset = isset( $weather['timezone_offset'] ) ? (int) $weather['timezone_offset'] : 0;
$mwpl_daily   = $weather['daily'];

$mwpl_label       = isset($atts['label']) && '' !== $atts['label']
    ? $atts['label']
    : "{$atts['lat']}, {$atts['lon']}";

$mwpl_units       = isset($atts['units']) ? $atts['units'] : 'metric';
$mwpl_unit_symbol = ('imperial' === $mwpl_units) ? '°F' : '°C';

$mwpl_icon_code   = $mwpl_current['weather'][0]['icon'] ?? '01d';
$mwpl_description = $mwpl_current['weather'][0]['description'] ?? 'Clear';
$mwpl_moon_phase  = $mwpl_daily[0]['moon_phase'] ?? null;
$mwpl_sunrise     = $mwpl_current['sunrise'] ?? null;
$mwpl_sunset      = $mwpl_current['sunset'] ?? null;

// For Erik Flowers we want the icon CLASS, not an image URL
$mwpl_icon_class = function_exists('mw_erikflowers_icon_map')
    ? mw_erikflowers_icon_map($mwpl_icon_code)
    : '';
?>

<div class="mw-card card-5"
     data-card="<?php echo esc_attr(isset($card) ? intval($card) : 5); ?>"
     data-lat="<?php echo esc_attr($atts['lat']); ?>"
     data-lon="<?php echo esc_attr($atts['lon']); ?>" data-tzoffset="<?php echo esc_attr( $mwpl_tz_offset ); ?>">

    <div class="mw-loc"><?php echo esc_html($mwpl_label); ?></div>

    <div class="mw-now">
        <div class="mw-icon-large">
            <?php if ($mwpl_icon_class) : ?>
                <i class="wi <?php echo esc_attr($mwpl_icon_class); ?>"></i>
            <?php else : ?>
                <i class="wi wi-na"></i>
            <?php endif; ?>
        </div>

        <div class="mw-temp">
            <?php echo esc_html( round($mwpl_current['temp']) . $mwpl_unit_symbol ); ?>
        </div>

        <div class="mw-cond">
            <?php echo esc_html( ucwords($mwpl_description) ); ?>
        </div>

        <?php if (null !== $mwpl_moon_phase) : ?>
            <?php
            $mwpl_moon_icon_class = function_exists('multiweather_get_moon_icon_class')
                ? multiweather_get_moon_icon_class($mwpl_moon_phase)
                : '';
            $mwpl_moon_label = $mwpl_moon_icon_class
                ? ucwords(str_replace(array('wi-moon-', '-'), array('', ' '), $mwpl_moon_icon_class))
                : '';
            ?>
            <?php if ($mwpl_moon_icon_class) : ?>
                <div class="mw-icon-moon">
                    <i class="wi <?php echo esc_attr($mwpl_moon_icon_class); ?>"></i>
                    <?php if ($mwpl_moon_label) : ?>
                        <div class="moon-label">
                            <?php echo esc_html($mwpl_moon_label); ?>
                        </div>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
        <?php endif; ?>

        <div class="mw-sun">
            <span>
                <i class="wi wi-sunrise"></i>
                <span class="local-time"
                      data-utc="<?php echo esc_attr( $mwpl_current['sunrise'] ); ?>">
                    <?php echo esc_html( mwpl_gmdate( 'g:i A', $mwpl_current['sunrise'] ) ); ?>
                </span>
            </span>
            <span>
                <i class="wi wi-sunset"></i>
                <span class="local-time"
                      data-utc="<?php echo esc_attr( $mwpl_current['sunset'] ); ?>">
                    <?php echo esc_html( mwpl_gmdate( 'g:i A', $mwpl_current['sunset'] ) ); ?>
                </span>
            </span>
        </div>

    </div>

    <div class="mw-forecast-compact">
        <?php for ($mwpl_i = 1; $mwpl_i <= 3; $mwpl_i++) : ?>
            <?php
            $mwpl_day = $mwpl_daily[$mwpl_i] ?? null;
            if ( ! $mwpl_day ) {
                continue;
            }

            $mwpl_day_name = gmdate('D', $mwpl_day['dt']);
            $mwpl_icon     = $mwpl_day['weather'][0]['icon'] ?? '01d';
            $mwpl_desc     = $mwpl_day['weather'][0]['description'] ?? '';

            $mwpl_min = round($mwpl_day['temp']['min']);
            $mwpl_max = round($mwpl_day['temp']['max']);

            $mwpl_day_icon_class = function_exists('mw_erikflowers_icon_map')
                ? mw_erikflowers_icon_map($mwpl_icon)
                : '';
            ?>
            <div class="mw-day-block">
                <div class="mw-day-name">
                    <?php echo esc_html($mwpl_day_name); ?>
                </div>

                <?php if ($mwpl_day_icon_class) : ?>
                    <i class="wi <?php echo esc_attr($mwpl_day_icon_class); ?>"></i>
                <?php else : ?>
                    <i class="wi wi-na"></i>
                <?php endif; ?>

                <div class="mw-day-temp">
                    <?php echo esc_html($mwpl_min . '° / ' . $mwpl_max . '°'); ?>
                </div>
            </div>
        <?php endfor; ?>
    </div>
</div>
