<?php

if (!function_exists('ec_t')) {
    function ec_t(string $key, ?string $fallback = null): string
    {
        return function_exists('t') ? t($key, $fallback ?? $key) : ($fallback ?? $key);
    }
}

if (!function_exists('ec_h')) {
    function ec_h($v): string
    {
        return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('ec_status_t')) {
    function ec_status_t(?string $status): string
    {
        $status = trim((string)$status);
        if ($status === '') {
            return '';
        }

        $normalized = strtolower($status);
        $normalized = str_replace([' ', '-'], '_', $normalized);

        $map = [
            'draft' => 'stage.draft',
            'submitted' => 'stage.submitted',
            'screening' => 'stage.screening',
            'reviewing' => 'stage.reviewing',
            'under_review' => 'stage.under_review',
            'revision_required' => 'stage.revision_required',
            'accepted' => 'stage.accepted',
            'rejected' => 'stage.rejected',
            'camera_ready' => 'stage.camera_ready',
            'production' => 'stage.production',
            'published' => 'stage.published',
        ];

        $key = $map[$normalized] ?? ('stage.' . $normalized);
        return ec_t($key, $status);
    }
}

if (!function_exists('ec_decision_t')) {
    function ec_decision_t(?string $decision): string
    {
        $decision = trim((string)$decision);
        if ($decision === '') {
            return ec_t('decision.pending', 'Pending');
        }

        $normalized = strtolower($decision);
        $normalized = str_replace([' ', '-'], '_', $normalized);

        $map = [
            'accepted' => 'decision.accepted',
            'accept' => 'decision.accepted',
            'rejected' => 'decision.rejected',
            'reject' => 'decision.rejected',
            'revision_required' => 'decision.revision_required',
            'pending' => 'decision.pending',
        ];

        return ec_t($map[$normalized] ?? ('decision.' . $normalized), $decision);
    }
}

if (!function_exists('ec_file_category_t')) {
    function ec_file_category_t(?string $category): string
    {
        $category = trim((string)$category);
        if ($category === '') {
            return '';
        }

        $normalized = strtolower($category);
        $normalized = str_replace([' ', '-', '/'], '_', $normalized);

        $aliases = [
            'abstract' => 'abstract',
            'manuscript' => 'manuscript',
            'submission' => 'manuscript',
            'full_article' => 'full_article',
            'full_paper' => 'full_article',
            'revised' => 'revised_version',
            'revised_version' => 'revised_version',
            'revision' => 'revised_version',
            'payment' => 'payment_invoice',
            'payment_invoice' => 'payment_invoice',
            'invoice' => 'payment_invoice',
            'figures' => 'figures',
            'figure' => 'figures',
            'tables' => 'tables',
            'table' => 'tables',
            'comments' => 'comments',
            'comment' => 'comments',
            'galley' => 'galley',
            'camera_ready' => 'camera_ready',
            'other' => 'other',
        ];

        $keyPart = $aliases[$normalized] ?? $normalized;
        return ec_t('file_category.' . $keyPart, $category);
    }
}

if (!function_exists('ec_required_badge')) {
    function ec_required_badge(): string
    {
        return '<span class="badge bg-danger">' . ec_h(ec_t('field.required', 'Required')) . '</span>';
    }
}

if (!function_exists('ec_optional_badge')) {
    function ec_optional_badge(): string
    {
        return '<span class="badge bg-secondary">' . ec_h(ec_t('field.optional', 'Optional')) . '</span>';
    }
}



if (!function_exists('ec_production_status_t')) {
    function ec_production_status_t(?string $status): string
    {
        $status = trim((string)$status);
        if ($status === '') return '';

        $n = strtolower(str_replace([' ', '-'], '_', $status));
        $map = [
            'pending' => 'admin.production.pending',
            'ready' => 'admin.production.ready',
            'camera_ready' => 'admin.production.camera_ready',
            'galley' => 'admin.production.galley',
            'published' => 'status.published',
        ];

        return ec_t($map[$n] ?? ('status.' . $n), $status);
    }
}

if (!function_exists('ec_yes_no_t')) {
    function ec_yes_no_t($value): string
    {
        return !empty($value) ? ec_t('common.yes', 'Yes') : ec_t('common.no', 'No');
    }
}

if (!function_exists('ec_visible_t')) {
    function ec_visible_t($value): string
    {
        return !empty($value) ? ec_t('common.visible', 'Visible') : ec_t('common.hidden', 'Hidden');
    }
}

if (!function_exists('ec_enabled_t')) {
    function ec_enabled_t($value): string
    {
        return !empty($value) ? ec_t('common.enabled', 'Enabled') : ec_t('common.disabled', 'Disabled');
    }
}

