<?php
namespace BLR\Backline\Core;

if (!defined('ABSPATH')) {
    exit;
}

final class Format {
    public static function date(string $value): string {
        $value = trim($value);
        if ($value === '') {
            return '';
        }

        $timestamp = strtotime($value);
        if (!$timestamp) {
            return $value;
        }

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

    public static function datetime(string $value): string {
        $value = trim($value);
        if ($value === '') {
            return '';
        }

        $timestamp = strtotime($value);
        if (!$timestamp) {
            return $value;
        }

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

    public static function user_label(int $user_id): string {
        if ($user_id <= 0) {
            return '';
        }

        $user = get_userdata($user_id);
        return $user ? (string) $user->display_name : '';
    }

    public static function event_location(int $event_id): string {
        if ($event_id <= 0) {
            return '';
        }

        $venue = trim((string) get_post_meta($event_id, 'blr_venue_name', true));
        $city  = trim((string) get_post_meta($event_id, 'blr_venue_city', true));
        $state = trim((string) get_post_meta($event_id, 'blr_venue_state', true));

        $place = trim($city . ($state !== '' ? ', ' . $state : ''));
        if ($venue !== '' && $place !== '') {
            return $venue . ' · ' . $place;
        }

        return $venue !== '' ? $venue : $place;
    }

    public static function city_state(string $city, string $state): string {
        $city = trim($city);
        $state = trim($state);
        return trim($city . ($state !== '' ? ', ' . $state : ''));
    }
}
