<?php
// BroadcastUrSelf master site + event template
// Change these before going live.

define('ADMIN_USER', 'admin');
define('ADMIN_PASS', 'change-this-password');
define('DATA_DIR', __DIR__ . '/data');
define('SETTINGS_FILE', DATA_DIR . '/settings.json');
define('SUBMISSIONS_FILE', DATA_DIR . '/submissions.json');
define('BASE_URL', ''); // e.g. /community if installed in a subfolder

function site_url(string $path = ''): string {
    $base = rtrim(BASE_URL, '/');
    $path = ltrim($path, '/');
    return $base . ($path ? '/' . $path : '');
}

function default_settings(): array {
    return [
        'site_name' => 'BroadcastUrSelf',
        'site_tagline' => 'Radio Hosting & Community Platform',
        'site_logo' => 'assets/site-logo.png',
        'site_intro' => 'A professional home base for your radio station community, featured events, seasonal promotions, contests, announcements, and network activity.',
        'community_blurb' => 'Use this as the main BroadcastUrSelf site, then swap the featured event section whenever you launch something new.',
        'footer_note' => 'Powered by BroadcastUrSelf radio hosting and community events.',
        'primary_button_label' => 'Event Link',
        'primary_button_url' => '',
        'secondary_button_label' => 'Open Submission Page',
        'station_heading' => 'Participating Stations & DJs',

        'event_active' => true,
        'event_type' => 'Featured Event',
        'event_name' => 'Featured Community Event',
        'headline' => 'Promote your next big event with a branded landing page.',
        'event_poster' => 'assets/default-event-poster.png',
        'pot_label' => 'Current Pot',
        'pot_value' => '145',
        'date_range' => 'April 3rd – April 6th',
        'description' => 'The event link is the main destination for the feature event. Participating stations and DJs can also run clue drops, promos, or supporting content across the network.',
        'how_it_works' => 'Use the button to direct listeners to the main event, stream, station page, external landing page, or sponsor page. You can keep submissions closed until the event begins.',
        'event_notice' => 'The event link can be added later if not ready yet.',
        'cta_text' => 'Now promoting across the community!',
        'status_text' => 'Submissions are currently closed.',
        'no_event_title' => 'No featured event is live right now.',
        'no_event_text' => 'The BroadcastUrSelf community is still active across the network. Check back soon for the next event, competition, or special broadcast.',
        'show_prize_box' => true,
        'show_submission_button' => true,
        'submission_button_text' => 'Open Submission Page',
        'submission_page_open' => false,
        'submission_title' => 'Submit Your Entry',
        'submission_intro' => 'Use this form for clue answers, contest entries, registration, nominations, or other community submissions.',
        'success_message' => 'Thanks. Your submission has been received.',

        'show_entry_type' => true,
        'require_entry_type' => false,
        'entry_type_label' => 'Entry Type',
        'entry_type_options' => [
            'Clue Answer',
            'Competition Entry',
            'General Submission',
            'Request',
            'Other'
        ],

        'show_field_one' => true,
        'require_field_one' => true,
        'field_one_label' => 'Title / Artist / Main Answer',
        'field_one_placeholder' => 'Enter the main answer or subject',

        'show_field_two' => true,
        'require_field_two' => false,
        'field_two_label' => 'Song / Description / Extra Detail',
        'field_two_placeholder' => 'Enter supporting information',

        'show_field_three' => false,
        'require_field_three' => false,
        'field_three_label' => 'Third Answer / Extra Detail',
        'field_three_placeholder' => 'Enter a third answer or detail',

        'show_message_field' => true,
        'require_message_field' => false,
        'field_message_label' => 'Message / Notes',
        'field_message_placeholder' => 'Add any extra details here',

        'show_name_field' => true,
        'require_name_field' => false,
        'name_label' => 'Your Name',

        'show_email_field' => true,
        'require_email_field' => true,
        'email_label' => 'Email Address',

        'show_platform_field' => true,
        'require_platform_field' => true,
        'platform_label' => 'Where did you hear about this event?',
        'platforms' => ['Website', 'Discord', 'Facebook', 'On Air', 'Other'],

        'stations' => [
            ['name' => 'Underground FX', 'url' => 'http://radio.undergroundfxradio.com:9000/stream', 'enabled' => true],
            ['name' => 'Radio Galaxy FX', 'url' => 'http://dj.radiogalaxyfx.com:8000/live', 'enabled' => true],
            ['name' => 'Oldies And More', 'url' => 'http://cr3.broadcasturself.com:23990/stream', 'enabled' => true],
            ['name' => 'Almost Heaven Radio', 'url' => 'http://cr3.broadcasturself.com:6969/stream', 'enabled' => false],
            ['name' => 'Home time radio', 'url' => 'http://cr3.broadcasturself.com:65001/stream', 'enabled' => true],
            ['name' => 'Thunder Mountain Radio', 'url' => 'http://cr3.broadcasturself.com:6660/stream', 'enabled' => true],
            ['name' => "Smurfette's Country Delights Radio", 'url' => 'http://cr3.broadcasturself.com:1982/stream', 'enabled' => true]
        ],
        'featured_blocks' => []
    ];
}

function merge_settings(array $defaults, array $settings): array {
    foreach ($defaults as $key => $value) {
        if (!array_key_exists($key, $settings)) {
            $settings[$key] = $value;
        } elseif (is_array($value) && is_array($settings[$key])) {
            $settings[$key] = merge_settings($value, $settings[$key]);
        }
    }
    return $settings;
}

function normalize_stations($stations): array {
    $normalized = [];
    if (!is_array($stations)) {
        return $normalized;
    }

    foreach ($stations as $station) {
        if (is_string($station)) {
            $name = trim($station);
            if ($name !== '') {
                $normalized[] = ['name' => $name, 'url' => '', 'enabled' => true];
            }
            continue;
        }

        if (!is_array($station)) {
            continue;
        }

        $name = trim((string)($station['name'] ?? ''));
        $url = trim((string)($station['url'] ?? ''));
        $enabled = !empty($station['enabled']);

        if ($name === '' && $url === '') {
            continue;
        }

        $normalized[] = [
            'name' => $name,
            'url' => $url,
            'enabled' => $enabled,
        ];
    }

    return $normalized;
}

function ensure_data_files(): void {
    if (!is_dir(DATA_DIR)) {
        mkdir(DATA_DIR, 0775, true);
    }

    if (!file_exists(SETTINGS_FILE)) {
        file_put_contents(SETTINGS_FILE, json_encode(default_settings(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
    }

    if (!file_exists(SUBMISSIONS_FILE)) {
        file_put_contents(SUBMISSIONS_FILE, json_encode([], JSON_PRETTY_PRINT));
    }
}

function get_settings(): array {
    ensure_data_files();
    $settings = json_decode((string) file_get_contents(SETTINGS_FILE), true);
    $settings = is_array($settings) ? $settings : [];
    $settings = merge_settings(default_settings(), $settings);
    $settings['stations'] = normalize_stations($settings['stations'] ?? []);
    return $settings;
}

function save_settings(array $settings): void {
    if (isset($settings['stations'])) {
        $settings['stations'] = normalize_stations($settings['stations']);
    }
    file_put_contents(SETTINGS_FILE, json_encode($settings, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

function get_submissions(): array {
    ensure_data_files();
    $data = json_decode((string) file_get_contents(SUBMISSIONS_FILE), true);
    return is_array($data) ? $data : [];
}

function save_submissions(array $submissions): void {
    file_put_contents(SUBMISSIONS_FILE, json_encode(array_values($submissions), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}

function e($value): string {
    return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
}

function bool_setting(array $settings, string $key, bool $default = false): bool {
    return isset($settings[$key]) ? (bool) $settings[$key] : $default;
}
?>