<?php
/**
 * Settings defaults and admin labels.
 *
 * @package BatoCookieConsent
 */

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

final class Bato_Cookie_Consent_Settings_Defaults
{
    public static function defaults(): array
    {
        return array(
            'version' => 1,
            'updated_at' => gmdate('c'),
            'general' => array(
                'enabled' => true,
                'mode' => 'gdpr',
                'bannerType' => 'full',
                'language' => Bato_Cookie_Consent_Settings_Languages::default_language(),
                'layout' => 'floating',
                'providedBy' => '',
                'logoAttachmentId' => 0,
                'logoDisabled' => false,
                'useDefaultLogo' => false,
            ),
            'content' => self::content(),
            'policyLinks' => array(),
            'styling' => array(
                'className' => '',
            ),
            'advanced' => array(
                'expiresDays' => 180,
                'floatingButton' => array(
                    'enabled' => false,
                    'showAfterConsent' => false,
                ),
                'googleConsentMode' => false,
            ),
        );
    }

    public static function content(): array
    {
        $content = array();

        foreach (Bato_Cookie_Consent_Settings_Languages::supported() as $language) {
            $content[$language] = self::locale_content($language);
        }

        return $content;
    }

    public static function admin_labels(): array
    {
        return array(
            'title' => __('Cookie Consent', 'bato-cookie-consent'),
            'save' => __('Save settings', 'bato-cookie-consent'),
            'saving' => __('Saving...', 'bato-cookie-consent'),
            'saved' => __('Settings saved.', 'bato-cookie-consent'),
            'general' => __('General', 'bato-cookie-consent'),
            'content' => __('Content', 'bato-cookie-consent'),
            'policyLinks' => __('Policy Links', 'bato-cookie-consent'),
            'styling' => __('Styling', 'bato-cookie-consent'),
            'scriptsEmbeds' => __('Scripts / Embeds', 'bato-cookie-consent'),
            'apiReference' => __('API Reference', 'bato-cookie-consent'),
            'settings' => __('Settings', 'bato-cookie-consent'),
            'selectLogo' => __('Select logo', 'bato-cookie-consent'),
            'removeLogo' => __('Remove logo', 'bato-cookie-consent'),
            'restoreDefaultLogo' => __('Restore Default', 'bato-cookie-consent'),
            'addLink' => __('Add link', 'bato-cookie-consent'),
            'selectPage' => __('Select page', 'bato-cookie-consent'),
        );
    }

    private static function locale_content(string $language): array
    {
        $locale = self::library_locale($language);

        if (! $locale && 'en' !== $language) {
            $locale = self::library_locale('en');
        }

        $locale = is_array($locale) ? $locale : self::fallback_locale();

        return array(
            'banner' => self::node($locale, 'banner', self::fallback_locale()['banner']),
            'buttons' => self::node($locale, 'buttons', self::fallback_locale()['buttons']),
            'modal' => self::node($locale, 'modal', self::fallback_locale()['modal']),
            'embeds' => array(
                'placeholder' => self::node($locale['embeds'] ?? array(), 'placeholder', self::fallback_locale()['embeds']['placeholder']),
            ),
            'categories' => self::node($locale, 'categories', self::fallback_locale()['categories']),
        );
    }

    private static function library_locale(string $language): array
    {
        $path = BATO_COOKIE_CONSENT_PATH . 'i18n/locales/' . $language . '.json';

        if (! is_readable($path)) {
            return array();
        }

        $decoded = json_decode((string) file_get_contents($path), true);

        return is_array($decoded) ? $decoded : array();
    }

    private static function node(array $source, string $key, array $fallback): array
    {
        return array_replace_recursive($fallback, isset($source[$key]) && is_array($source[$key]) ? $source[$key] : array());
    }

    private static function fallback_locale(): array
    {
        return array(
            'banner' => array(
                'title' => 'We use cookies',
                'description' => 'We use cookies to improve your browsing experience, personalize content, and analyze traffic.',
                'footer' => 'You can change your preferences at any time.',
            ),
            'buttons' => array(
                'acceptAll' => 'Accept all',
                'rejectAll' => 'Reject all',
                'customize' => 'Customize',
                'save' => 'Save choices',
                'close' => 'Close',
                'preferences' => 'Cookie settings',
                'details' => 'Details',
                'hideDetails' => 'Hide details',
            ),
            'modal' => array(
                'title' => 'Cookie preferences',
                'description' => 'Choose which categories of cookies you want to allow.',
            ),
            'embeds' => array(
                'placeholder' => array(
                    'title' => 'External video',
                    'description' => 'This video may set third-party cookies. Allow marketing cookies to load and play it.',
                    'buttonText' => 'Allow marketing cookies and play video',
                ),
            ),
            'categories' => array(
                'necessary' => array(
                    'title' => 'Necessary',
                    'description' => 'Required for core site functionality and cannot be disabled.',
                    'details' => 'These cookies keep the site secure and usable. They support actions such as page navigation, consent storage, form protection, authentication, and load balancing.',
                ),
                'analytics' => array(
                    'title' => 'Analytics',
                    'description' => 'Help us understand how visitors interact with the site.',
                    'details' => 'Analytics cookies collect aggregated usage data such as visited pages, traffic sources, session duration, and error events so we can improve the site.',
                ),
                'marketing' => array(
                    'title' => 'Marketing',
                    'description' => 'Used to personalize ads and measure campaign performance.',
                    'details' => 'Marketing cookies can be used to build audience segments, measure advertising performance, limit repeated ads, and personalise campaigns across channels.',
                ),
                'preferences' => array(
                    'title' => 'Preferences',
                    'description' => 'Remember choices such as language, region, and display settings.',
                    'details' => 'Preference cookies remember choices that make the site more comfortable to use, including language, region, layout, and other display options.',
                ),
            ),
        );
    }

}
