<?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 ensure_data_files(): void {
    if (!is_dir(DATA_DIR)) {
        mkdir(DATA_DIR, 0775, true);
    }

    if (!file_exists(SETTINGS_FILE)) {
        $defaults = [
            // Branding / main site
            'site_name' => 'BroadcastUrSelf',
            'site_tagline' => 'Radio Hosting & Community Platform',
            'site_logo' => 'assets/site-logo.png',
            'site_intro' => 'A professional radio station community site for competitions, seasonal specials, live events, station promotions, and community announcements.',
            'community_blurb' => 'Use this template as the main station community site and switch the featured event section whenever you launch something new.',
            'footer_note' => 'Powered by BroadcastUrSelf radio hosting and community events.',
            'primary_button_label' => 'Visit Event Link',
            'primary_button_url' => '',
            'secondary_button_label' => 'Open Submission Page',
            'station_heading' => 'Participating Stations & DJs',

            // Featured event
            'event_active' => true,
            'event_type' => 'Competition',
            '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.',
            '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.',
            'entry_type_label' => 'Entry Type',
            'entry_type_options' => [
                'Clue Answer',
                'Competition Entry',
                'General Submission',
                'Request',
                'Other'
            ],

            // Flexible form labels
            'field_one_label' => 'Title / Artist / Main Answer',
            'field_one_placeholder' => 'Enter the main answer or subject',
            'field_two_label' => 'Song / Description / Extra Detail',
            'field_two_placeholder' => 'Enter supporting information',
            'field_message_label' => 'Message / Notes',
            'field_message_placeholder' => 'Add any extra details here',
            'name_label' => 'Your Name',
            'email_label' => 'Email Address',
            'platform_label' => 'Where did you hear about this event?',
            'platforms' => ['Website', 'Discord', 'Facebook', 'On Air', 'Other'],

            // Lists
            'stations' => [
                'BroadcastUrSelf Community',
                'Underground FX',
                'Hometime Radio',
                'Oldies & More Radio',
                'Smurfettes Country Delights'
            ],
            'featured_blocks' => [
                ['title' => 'Seasonal Events', 'text' => 'Halloween, Christmas, Easter, Summer, countdown shows, and holiday specials.'],
                ['title' => 'Competitions', 'text' => 'Run clue hunts, song guesses, giveaways, raffles, and sponsor-backed promotions.'],
                ['title' => 'Community Hub', 'text' => 'Use this as the main home page for station updates, DJ spotlights, and network announcements.']
            ]
        ];
        file_put_contents(SETTINGS_FILE, json_encode($defaults, 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);
    return is_array($settings) ? $settings : [];
}

function save_settings(array $settings): void {
    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;
}
?>