<?php
/**
 * Test doubles for The Events Calendar classes and functions
 */

if (!class_exists('Tribe__Events__Main')) {
    class Tribe__Events__Main {
        const POSTTYPE = 'tribe_events';
        const VENUE_POST_TYPE = 'tribe_venue';
        const ORGANIZER_POST_TYPE = 'tribe_organizer';

        public static function registerPostType() {
            register_post_type(self::POSTTYPE, [
                'public' => true,
                'label' => 'Events'
            ]);
            register_post_type(self::VENUE_POST_TYPE, [
                'public' => true,
                'label' => 'Venues'
            ]);
            register_post_type(self::ORGANIZER_POST_TYPE, [
                'public' => true,
                'label' => 'Organizers'
            ]);
        }
    }
}

// Register post types when file is loaded
if (function_exists('register_post_type')) {
    Tribe__Events__Main::registerPostType();
}

// Mock TEC functions if they don't exist
if (!function_exists('tribe_get_start_date')) {
    function tribe_get_start_date($event_id) {
        return get_post_meta($event_id, '_EventStartDate', true);
    }
}

if (!function_exists('tribe_get_end_date')) {
    function tribe_get_end_date($event_id) {
        return get_post_meta($event_id, '_EventEndDate', true);
    }
}

if (!function_exists('tribe_get_cost')) {
    function tribe_get_cost($event_id, $with_currency_symbol = false) {
        $cost = get_post_meta($event_id, '_EventCost', true);
        if ($with_currency_symbol) {
            $symbol = get_post_meta($event_id, '_EventCurrencySymbol', true) ?: '$';
            return $symbol . $cost;
        }
        return $cost;
    }
}

if (!function_exists('tribe_event_is_all_day')) {
    function tribe_event_is_all_day($event_id) {
        return get_post_meta($event_id, '_EventAllDay', true) === 'yes';
    }
}

if (!function_exists('tribe_is_recurring_event')) {
    function tribe_is_recurring_event($event_id) {
        return false;
    }
}

if (!function_exists('tribe_get_timezone')) {
    function tribe_get_timezone($event_id = null) {
        return get_option('timezone_string') ?: 'UTC';
    }
}

if (!function_exists('tribe_get_venue_website_link')) {
    function tribe_get_venue_website_link($venue_id) {
        $url = get_post_meta($venue_id, '_VenueURL', true);
        return $url ? sprintf('<a href="%s">%s</a>', esc_url($url), esc_url($url)) : '';
    }
}

if (!function_exists('tribe_get_organizer_website_link')) {
    function tribe_get_organizer_website_link($organizer_id) {
        $url = get_post_meta($organizer_id, '_OrganizerWebsite', true);
        return $url ? sprintf('<a href="%s">%s</a>', esc_url($url), esc_url($url)) : '';
    }
}

if (!function_exists('tribe_get_venue')) {
    function tribe_get_venue($venue_id) {
        return get_post($venue_id);
    }
}

if (!function_exists('tribe_get_venue_details')) {
    function tribe_get_venue_details($venue_id) {
        return [
            'name' => get_the_title($venue_id),
            'address' => get_post_meta($venue_id, '_VenueAddress', true),
            'city' => get_post_meta($venue_id, '_VenueCity', true),
            'state' => get_post_meta($venue_id, '_VenueState', true),
            'zip' => get_post_meta($venue_id, '_VenueZip', true),
            'country' => get_post_meta($venue_id, '_VenueCountry', true),
            'phone' => get_post_meta($venue_id, '_VenuePhone', true),
            'website' => get_post_meta($venue_id, '_VenueURL', true)
        ];
    }
}

if (!function_exists('tribe_get_organizer')) {
    function tribe_get_organizer($organizer_id) {
        return get_post($organizer_id);
    }
}

if (!function_exists('tribe_get_organizer_details')) {
    function tribe_get_organizer_details($organizer_id) {
        return [
            'name' => get_the_title($organizer_id),
            'phone' => get_post_meta($organizer_id, '_OrganizerPhone', true),
            'email' => get_post_meta($organizer_id, '_OrganizerEmail', true),
            'website' => get_post_meta($organizer_id, '_OrganizerWebsite', true)
        ];
    }
}