<?php
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/session_check.php';
checkSession();
require_once __DIR__ . '/includes/env_loader.php';
require_once __DIR__ . '/src/Translate.php';

$pageTitle = 'Traducir Mensaje';

$sourceText = $_GET['text'] ?? '';
$sourceLang = $_GET['source_lang'] ?? 'es';
$targetLang = $_GET['target_lang'] ?? 'en';
$translatedText = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $sourceText = $_POST['source_text'] ?? '';
    $sourceLang = $_POST['source_lang'] ?? 'es';
    $targetLang = $_POST['target_lang'] ?? 'en';
    
    if ($sourceText) {
        try {
            $translator = new src\Translate();
            $translatedText = $translator->translate($sourceText, $sourceLang, $targetLang);
            
            if (!$translatedText) {
                $error = 'Error al traducir. Verifica la conexión con LibreTranslate.';
            }
        } catch (Exception $e) {
            $error = $e->getMessage();
        }
    }
}

try {
    $pdo = getDbConnection();
    $stmt = $pdo->query("SELECT * FROM supported_languages WHERE is_active = 1 ORDER BY language_name");
    $languages = $stmt->fetchAll();
} catch (Exception $e) {
    $languages = [];
}

require_once __DIR__ . '/templates/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-translate"></i> Traducir Mensaje</h2>
</div>

<?php if ($error): ?>
    <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>

<div class="row">
    <div class="col-md-6">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white border-0">
                <h5 class="mb-0">Texto Original</h5>
            </div>
            <div class="card-body">
                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label">Idioma de origen</label>
                        <select name="source_lang" class="form-select">
                            <?php foreach ($languages as $lang): ?>
                            <option value="<?= $lang['language_code'] ?>" <?= $sourceLang === $lang['language_code'] ? 'selected' : '' ?>>
                                <?= htmlspecialchars($lang['flag_emoji'] . ' ' . $lang['language_name']) ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Texto a traducir</label>
                        <textarea name="source_text" class="form-control" rows="8" required><?= htmlspecialchars($sourceText) ?></textarea>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Idioma de destino</label>
                        <select name="target_lang" class="form-select">
                            <?php foreach ($languages as $lang): ?>
                            <option value="<?= $lang['language_code'] ?>" <?= $targetLang === $lang['language_code'] ? 'selected' : '' ?>>
                                <?= htmlspecialchars($lang['flag_emoji'] . ' ' . $lang['language_name']) ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-translate"></i> Traducir
                    </button>
                </form>
            </div>
        </div>
    </div>
    
    <div class="col-md-6">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white border-0">
                <h5 class="mb-0">Traducción</h5>
            </div>
            <div class="card-body">
                <?php if ($translatedText): ?>
                    <textarea class="form-control" rows="12" readonly><?= htmlspecialchars($translatedText) ?></textarea>
                    <button class="btn btn-outline-secondary mt-2" onclick="navigator.clipboard.writeText('<?= htmlspecialchars(addslashes($translatedText)) ?>')">
                        <i class="bi bi-clipboard"></i> Copiar
                    </button>
                <?php else: ?>
                    <p class="text-muted text-center py-5">Traducción aparecerá aquí</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/templates/footer.php'; ?>
