<?php

declare(strict_types=1);

namespace PpPhp\Core;

/**
 * Label API – manage multilingual labels for tables, fields, pages, and custom strings.
 *
 * All labels are stored in project.json under misc.labels.
 * If a label is missing for the requested language, the default (first) language is used as fallback.
 *
 * Usage:
 *   Label::SetProjectSlug('my-project');
 *   echo Label::getFieldLabel('customers', 'name', 'np');
 *   Label::setFieldTooltip('orders', 'total', 'Enter the total amount', 'en');
 */
class Label
{
    private static ?string $slug = null;

    /**
     * Set the current project slug (required before any get/set).
     */
    public static function SetProjectSlug(string $slug): void
    {
        self::$slug = $slug;
    }

    // ── Generic getter / setter ────────────────────────────────────────

    /**
     * Get a label for a language and section, with fallback.
     *
     * @param string $key       e.g., "customers.name" or "save"
     * @param string $langCode  e.g., "en" or "np"
     * @param string $section   tables, custom, tooltips, titles, placeholders, logo, cookie
     * @param string $fallback  Returned if no label is found
     * @return string
     */
    public static function Get(string $key, string $langCode = 'en', string $section = 'tables', string $fallback = ''): string
    {
        $state = ProjectState::load(self::$slug);
        $labels = $state['misc']['labels'][$langCode][$section] ?? [];

        if (isset($labels[$key]) && $labels[$key] !== '') {
            return $labels[$key];
        }

        // Fallback to default language
        $defaultLang = self::getDefaultLanguage();
        if ($langCode !== $defaultLang) {
            $defaultLabels = $state['misc']['labels'][$defaultLang][$section] ?? [];
            if (isset($defaultLabels[$key]) && $defaultLabels[$key] !== '') {
                return $defaultLabels[$key];
            }
        }

        return $fallback !== '' ? $fallback : $key;
    }

    /**
     * Set a label for a language and section.
     */
    public static function Set(string $key, string $value, string $langCode = 'en', string $section = 'tables'): void
    {
        $state = ProjectState::load(self::$slug);
        $state['misc']['labels'][$langCode][$section][$key] = $value;
        ProjectState::save(self::$slug, $state);
    }

    // ── Field labels ────────────────────────────────────────────────────

    public static function getFieldLabel(string $table, string $field, string $langCode = 'en'): string
    {
        return self::Get("{$table}.{$field}", $langCode, 'tables', $field);
    }

    public static function setFieldLabel(string $table, string $field, string $value, string $langCode = 'en'): void
    {
        self::Set("{$table}.{$field}", $value, $langCode, 'tables');
    }

    // ── Table captions ──────────────────────────────────────────────────

    public static function getTableCaption(string $table, string $langCode = 'en'): string
    {
        return self::Get("{$table}.caption", $langCode, 'tables', $table);
    }

    public static function setTableCaption(string $table, string $value, string $langCode = 'en'): void
    {
        self::Set("{$table}.caption", $value, $langCode, 'tables');
    }

    // ── Page titles ─────────────────────────────────────────────────────

    public static function getPageTitle(string $table, string $page, string $langCode = 'en'): string
    {
        return self::Get("{$table}.{$page}", $langCode, 'titles', "{$table} - {$page}");
    }

    public static function setPageTitle(string $table, string $page, string $value, string $langCode = 'en'): void
    {
        self::Set("{$table}.{$page}", $value, $langCode, 'titles');
    }

    // ── Field tooltips ──────────────────────────────────────────────────

    public static function getFieldTooltip(string $table, string $field, string $langCode = 'en'): string
    {
        return self::Get("{$table}.{$field}", $langCode, 'tooltips', '');
    }

    public static function setFieldTooltip(string $table, string $field, string $value, string $langCode = 'en'): void
    {
        self::Set("{$table}.{$field}", $value, $langCode, 'tooltips');
    }

    // ── Field placeholders ──────────────────────────────────────────────

    public static function getFieldPlaceholder(string $table, string $field, string $langCode = 'en'): string
    {
        return self::Get("{$table}.{$field}", $langCode, 'placeholders', '');
    }

    public static function setFieldPlaceholder(string $table, string $field, string $value, string $langCode = 'en'): void
    {
        self::Set("{$table}.{$field}", $value, $langCode, 'placeholders');
    }

    // ── Custom labels ───────────────────────────────────────────────────

    public static function getCustomLabel(string $key, string $langCode = 'en'): string
    {
        return self::Get($key, $langCode, 'custom', $key);
    }

    public static function setCustomLabel(string $key, string $value, string $langCode = 'en'): void
    {
        self::Set($key, $value, $langCode, 'custom');
    }

    // ── Project logo ────────────────────────────────────────────────────

    public static function getProjectLogo(string $langCode = 'en'): string
    {
        return self::Get('projectLogo', $langCode, 'logo', '');
    }

    public static function setProjectLogo(string $value, string $langCode = 'en'): void
    {
        self::Set('projectLogo', $value, $langCode, 'logo');
    }

    // ── Cookie consent banner ───────────────────────────────────────────

    public static function getCookieBanner(string $langCode = 'en'): string
    {
        return self::Get('cookieBannerText', $langCode, 'cookie', '');
    }

    public static function setCookieBanner(string $value, string $langCode = 'en'): void
    {
        self::Set('cookieBannerText', $value, $langCode, 'cookie');
    }

    // ── Internal helpers ────────────────────────────────────────────────

    /**
     * Return the first language code from the project settings (default language).
     */
    private static function getDefaultLanguage(): string
    {
        $state = ProjectState::load(self::$slug);
        $langs = $state['misc']['project']['languages'] ?? [['code' => 'en']];
        return $langs[0]['code'] ?? 'en';
    }
}