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

/**
 * General utility helpers for MultiWeather.
 *
 * Prefixed wrapper around gmdate().
 *
 * @param string      $format    Date format.
 * @param int|float|null $timestamp Unix timestamp; defaults to current time if null.
 * @return string
 */
if ( ! function_exists( 'mwpl_gmdate' ) ) {
	function mwpl_gmdate( $format, $timestamp = null ) {
		if ( null === $timestamp ) {
			$timestamp = time();
		}

		return gmdate( $format, (int) $timestamp );
	}
}

// ... your existing utilities code ...

if ( ! function_exists( 'mwpl_get_weather_offset' ) ) {
    /**
     * Get timezone offset (in seconds) for a weather payload.
     *
     * @param array $weather Full weather array from OpenWeather.
     * @return int Offset in seconds from UTC.
     */
    function mwpl_get_weather_offset( $weather ) {
        return isset( $weather['timezone_offset'] )
            ? (int) $weather['timezone_offset']
            : 0;
    }
}

if ( ! function_exists( 'mwpl_local_time' ) ) {
    /**
     * Format a UNIX timestamp as LOCAL time for the location (never UTC).
     *
     * @param int    $timestamp UNIX timestamp (seconds).
     * @param array  $weather   Full weather array (for timezone_offset).
     * @param string $format    gmdate() format.
     * @return string
     */
    function mwpl_local_time( $timestamp, $weather, $format = 'g:i A' ) {
        $timestamp = (int) $timestamp;
        $offset    = mwpl_get_weather_offset( $weather );

        return gmdate( $format, $timestamp + $offset );
    }
}

if ( ! function_exists( 'mwpl_local_date' ) ) {
    /**
     * Format a UNIX timestamp as LOCAL date for the location.
     *
     * @param int    $timestamp UNIX timestamp (seconds).
     * @param array  $weather   Full weather array (for timezone_offset).
     * @param string $format    gmdate() format.
     * @return string
     */
    function mwpl_local_date( $timestamp, $weather, $format = 'l, j F' ) {
        $timestamp = (int) $timestamp;
        $offset    = mwpl_get_weather_offset( $weather );

        return gmdate( $format, $timestamp + $offset );
    }
}
function mwpl_is_pro() {
    return function_exists( 'mul_fs' ) && mul_fs()->can_use_premium_code();
}
