<?php

namespace App\Core;

/**
 * Accessibility Helper
 * دوال مساعدة لتحسين Accessibility (A11y)
 */
class Accessibility
{
    /**
     * إضافة ARIA label
     */
    public static function ariaLabel(string $label): string
    {
        return 'aria-label="' . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . '"';
    }

    /**
     * إضافة ARIA describedby
     */
    public static function ariaDescribedBy(string $id): string
    {
        return 'aria-describedby="' . htmlspecialchars($id, ENT_QUOTES, 'UTF-8') . '"';
    }

    /**
     * إضافة ARIA required
     */
    public static function ariaRequired(bool $required = true): string
    {
        return $required ? 'aria-required="true"' : '';
    }

    /**
     * إضافة ARIA invalid
     */
    public static function ariaInvalid(bool $invalid = false): string
    {
        return $invalid ? 'aria-invalid="true"' : '';
    }

    /**
     * إضافة role
     */
    public static function role(string $role): string
    {
        return 'role="' . htmlspecialchars($role, ENT_QUOTES, 'UTF-8') . '"';
    }

    /**
     * إضافة tabindex
     */
    public static function tabIndex(int $index): string
    {
        return 'tabindex="' . $index . '"';
    }

    /**
     * Generate accessible form field
     */
    public static function formField(string $id, string $label, string $type = 'text', array $attributes = []): string
    {
        $labelId = $id . '_label';
        $errorId = $id . '_error';
        
        $html = '<div class="form-group">';
        $html .= '<label for="' . htmlspecialchars($id, ENT_QUOTES, 'UTF-8') . '" id="' . $labelId . '">';
        $html .= htmlspecialchars($label, ENT_QUOTES, 'UTF-8');
        $html .= '</label>';
        
        $attrs = [
            'id' => $id,
            'type' => $type,
            'aria-labelledby' => $labelId,
            'aria-describedby' => $errorId
        ];
        
        $attrs = array_merge($attrs, $attributes);
        
        $html .= '<input';
        foreach ($attrs as $key => $value) {
            $html .= ' ' . htmlspecialchars($key, ENT_QUOTES, 'UTF-8') . '="' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '"';
        }
        $html .= '>';
        
        $html .= '<span id="' . $errorId . '" class="error-message" role="alert" aria-live="polite"></span>';
        $html .= '</div>';
        
        return $html;
    }

    /**
     * Skip to main content link
     */
    public static function skipToMainContent(): string
    {
        return '<a href="#main-content" class="skip-link" ' . self::tabIndex(0) . '>تخطي إلى المحتوى الرئيسي</a>';
    }
}
