<?php 
function dacdb_calendar($atts)
{
    global $fDebug, $data_path, $options;
    $calendar = "";
    $views = "dayGridMonth,listMonth";

    // Determine the club ID to render in the calendar (if any)
    $club_id = isset($_GET['club_id']) ? $_GET['club_id'] : 
              (isset($atts['club_id']) ? $atts['club_id'] : 
              (isset($options['dacdb_club']) ? $options['dacdb_club'] : '0'));

    // Set the layout of the rendered calendar
    $view = (isset($_GET['view']) && in_array($_GET['view'], ['list', 'listMonth', '1'])) ? 'listMonth' : 
           (isset($atts['view']) && in_array($atts['view'], ['list', 'listMonth', '1']) ? 'listMonth' : 
           (isset($_GET['view']) && in_array($_GET['view'], ['month', '0', 'dayGridMonth']) || 
           (isset($atts['view']) && in_array($atts['view'], ['month', '0', 'dayGridMonth'])) ? 'dayGridMonth' : 
           (!empty($options['cal_display']) && in_array($options['cal_display'], ['listMonth', 'dayGridMonth']) ? 
           $options['cal_display'] : 'dayGridMonth')));

    // Set the default calendar limit
    $callimit = (isset($_GET['cal_limit']) && $_GET['cal_limit'] >= 1 && $_GET['cal_limit'] <= 9) ? $_GET['cal_limit'] : 
               (isset($atts['cal_limit']) && $atts['cal_limit'] >= 1 && $atts['cal_limit'] <= 9 ? $atts['cal_limit'] : 
               (!empty($options['cal_limit']) && $options['cal_limit'] >= 1 && $options['cal_limit'] <= 9 ? 
               $options['cal_limit'] : 5));

    // Get sector parameter
    $sector = isset($_GET['sector']) ? $_GET['sector'] : 
             (isset($atts['sector']) ? $atts['sector'] : '');

    // First determine if we're looking at club or district level
    $club_id = isset($options['dacdb_club']) ? $options['dacdb_club'] : '0';

    // Get all available event category IDs for fallback
    $eventID_json = $data_path . "wp_eventcategory.cfm?D=" . $options['dacdb_district'];
    $valid_categories = array();
    
    $json_data = @file_get_contents($eventID_json);
    if ($json_data !== false) {
        $categories = json_decode($json_data, true);
        if (is_array($categories)) {
            $valid_categories = array_column($categories, 'EVENTCATEGORYID');
        }
    }

    // Priority 1: URL Query String
    if (isset($_GET['catids'])) {
        $eids = $_GET['catids'];
    } elseif (isset($_GET['catid'])) {
        $eids = $_GET['catid'];
    }
    // Priority 2: Shortcode Attributes
    elseif (isset($atts['catids'])) {
        $eids = $atts['catids'];
    } elseif (isset($atts['catid'])) {
        $eids = $atts['catid'];
    }
    // Priority 3: Plugin Options based on club/district setting
    elseif ($club_id > 0 && !empty($options['club_events_types_page'])) {
        $eids = $options['club_events_types_page'];
    } elseif ($club_id == 0 && !empty($options['district_events_types_page'])) {
        $eids = $options['district_events_types_page'];
    }
    // Priority 4: All Available Categories
    else {
        $eids = implode(',', $valid_categories);
    }

    if ($fDebug) {
        $calendar .= '<div class="debug-info">';
        $calendar .= '<p>URL catids: ' . (isset($_GET['catids']) ? $_GET['catids'] : 'Not set') . '</p>';
        $calendar .= '<p>URL catid: ' . (isset($_GET['catid']) ? $_GET['catid'] : 'Not set') . '</p>';
        $calendar .= '<p>Shortcode catids: ' . (isset($atts['catids']) ? $atts['catids'] : 'Not set') . '</p>';
        $calendar .= '<p>Shortcode catid: ' . (isset($atts['catid']) ? $atts['catid'] : 'Not set') . '</p>';
        $calendar .= '<p>Club ID: ' . $club_id . '</p>';
        $calendar .= '<p>Options club_events_types_page: ' . (isset($options['club_events_types_page']) ? $options['club_events_types_page'] : 'Not set') . '</p>';
        $calendar .= '<p>Options district_events_types_page: ' . (isset($options['district_events_types_page']) ? $options['district_events_types_page'] : 'Not set') . '</p>';
        $calendar .= '<p>All available categories: ' . implode(',', $valid_categories) . '</p>';
        $calendar .= '<p>Final eids used: ' . $eids . '</p>';
        $calendar .= '</div>';
    }

    // Build URLs for XML and json sources
    $event_url = $data_path . "R_GetEventsV2.cfm?AccountID=" . $options['dacdb_district'] . 
                 "&ClubID=" . $club_id . 
                 "&EventCategoryIDs=" . $eids;
    
    if (!empty($sector)) {
        $event_url .= "&Sector=" . $sector;
    }

    // Localize the script with our data
    wp_localize_script('dacdb-calendar', 'dacdbCalendarData', array(
        'dataPath' => $data_path,
        'accountId' => $options['dacdb_district'],
        'clubId' => $club_id,
        'eids' => $eids,
        'sector' => $sector,
        'view' => $view,
        'views' => $views,
        'eventIdJson' => $eventID_json,
        'calLimit' => $callimit
    ));

    // Add calendar HTML with styles
    $calendar .= "<style>
                    .entry-content tr td {padding: 4px 4px !important;}        // override a style to reduce the cell padding
                    .fc-timegrid-event {
                        line-height: 1.1em;
                    }
                    </style>";
    $calendar .= "<div id='Calendar'></div>";
    $calendar .= "<div id='colorKey'></div>";

    return $calendar;
}