<?php
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/session_check.php';
require_once __DIR__ . '/includes/message_handler.php';
require_once __DIR__ . '/common/helpers/sender_factory.php';
require_once __DIR__ . '/common/helpers/converter_factory.php';

requireAdmin();

function getTranslationButtons(PDO $pdo, string $text): array
{
    $stmt = $pdo->query("SELECT language_code, flag_emoji FROM supported_languages WHERE is_active = 1");
    $languages = $stmt->fetchAll();
    
    if (count($languages) <= 1) {
        return [];
    }
    
    return [
        'telegram' => buildTelegramTranslationButtons($pdo, $languages, $text),
        'discord' => buildDiscordTranslationButtons($languages, $text)
    ];
}

function buildTelegramTranslationButtons(PDO $pdo, array $languages, string $text): array
{
    if (count($languages) <= 1) {
        return [];
    }
    
    // Guardar texto en la base de datos con hash consistente
    $textHash = md5($text);
    $stmt = $pdo->prepare("INSERT INTO translation_cache (text_hash, original_text) VALUES (?, ?) ON DUPLICATE KEY UPDATE original_text = VALUES(original_text)");
    $stmt->execute([$textHash, $text]);
    
    $buttons = [];
    
    foreach ($languages as $lang) {
        $buttons[] = [
            'text' => $lang['flag_emoji'] . ' ' . strtoupper($lang['language_code']),
            'callback_data' => 'translate:' . $lang['language_code'] . ':' . $textHash
        ];
    }
    
    return [
        'inline_keyboard' => array_chunk($buttons, 3)
    ];
}

function buildDiscordTranslationButtons(array $languages, string $text): array
{
    $buttons = [];
    
    $textHash = md5($text);
    
    foreach ($languages as $lang) {
        $buttons[] = [
            'label' => $lang['flag_emoji'] . ' ' . strtoupper($lang['language_code']),
            'custom_id' => 'translate_' . $lang['language_code'] . ':' . $textHash,
            'style' => 1
        ];
    }
    
    return [
        [
            'type' => 1,
            'components' => array_slice($buttons, 0, 5)
        ]
    ];
}

$pageTitle = 'Enviar Mensaje Directo';

$recipients = [];
$galleryImages = [];

try {
    $pdo = getDbConnection();
    $stmt = $pdo->query("SELECT * FROM recipients ORDER BY platform, name");
    $recipients = $stmt->fetchAll();
    
    // Cargar imágenes de la galería
    $galleryPath = __DIR__ . '/galeria';
    if (is_dir($galleryPath)) {
        $files = scandir($galleryPath);
        foreach ($files as $file) {
            if (in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), ['jpg', 'jpeg', 'png', 'gif', 'webp'])) {
                $galleryImages[] = $file;
            }
        }
    }
} catch (Exception $e) {
    $error = $e->getMessage();
}

$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $content = $_POST['content'];
    $recipientId = $_POST['recipient_id'];
    
    if ($content && $recipientId) {
        $messageId = createMessage([
            'user_id' => getCurrentUserId(),
            'content' => $content
        ]);
        
        // Obtener hora actual de MySQL para sincronización
        $pdo = getDbConnection();
        $stmt = $pdo->query("SELECT NOW() as now");
        $now = $stmt->fetch()['now'];
        
        $scheduleId = createSchedule([
            'message_id' => $messageId,
            'recipient_id' => $recipientId,
            'send_time' => $now,
            'status' => 'pending'
        ]);
        
        // Procesar el mensaje inmediatamente
        // Obtener datos del schedule recién creado
        $stmt = $pdo->prepare("
            SELECT s.*, m.content, r.platform_id, r.platform, r.name as recipient_name
            FROM schedules s
            JOIN messages m ON s.message_id = m.id
            JOIN recipients r ON s.recipient_id = r.id
            WHERE s.id = ?
        ");
        $stmt->execute([$scheduleId]);
        $schedule = $stmt->fetch();
        
        $results = ['processed' => 0, 'sent' => 0, 'failed' => 0];
        
        if ($schedule) {
            $stmt = $pdo->prepare("UPDATE schedules SET status = 'processing' WHERE id = ?");
            $stmt->execute([$schedule['id']]);
            
            try {
                $sender = \Common\Helpers\SenderFactory::create($schedule['platform']);
                
                // Obtener botones de traducción (convertir HTML a texto plano)
                $plainText = html_entity_decode(strip_tags($schedule['content']), ENT_QUOTES | ENT_HTML5, 'UTF-8');
                $plainText = preg_replace('/\s+/', ' ', $plainText); // Normalizar espacios
                $translationButtons = getTranslationButtons($pdo, $plainText);
                
                // Parsear el contenido HTML en segmentos manteniendo el orden
                $segments = $sender->parseContent($schedule['content']);
                
                $messageCount = 0;
                
                // Enviar cada segmento en el orden correcto
                foreach ($segments as $segment) {
                    if ($segment['type'] === 'text') {
                        // Convertir el texto al formato de la plataforma
                        $textContent = \Common\Helpers\ConverterFactory::convert($schedule['platform'], $segment['content']);
                        
                        if (!empty(trim($textContent))) {
                            // Agregar botones de traducción al último segmento de texto
                            $buttons = null;
                            if ($segment === end($segments)) {
                                $buttons = $schedule['platform'] === 'telegram' 
                                    ? $translationButtons['telegram'] 
                                    : $translationButtons['discord'];
                            }
                            
                            if ($schedule['platform'] === 'telegram') {
                                $sender->sendMessage($schedule['platform_id'], $textContent, $buttons);
                            } else {
                                $sender->sendMessage($schedule['platform_id'], $textContent, null, $buttons);
                            }
                            $messageCount++;
                        }
                    } elseif ($segment['type'] === 'image') {
                        $imagePath = $segment['src'];
                        
                        // Quitar parámetros de URL si los hay
                        $imgPath = parse_url($imagePath, PHP_URL_PATH) ?: $imagePath;
                        
                        if (file_exists($imgPath)) {
                            // Es un archivo local
                            $sender->sendMessageWithAttachments($schedule['platform_id'], '', [$imgPath]);
                            $messageCount++;
                        } elseif (strpos($imagePath, 'http') === 0) {
                            // Es una URL remota
                            $embed = ['image' => ['url' => $imagePath]];
                            $sender->sendMessage($schedule['platform_id'], '', $embed);
                            $messageCount++;
                        }
                    }
                }
                
                $stmt = $pdo->prepare("
                    INSERT INTO sent_messages (schedule_id, recipient_id, platform_message_id, message_count, sent_at)
                    VALUES (?, ?, '', ?, NOW())
                ");
                $stmt->execute([$schedule['id'], $schedule['recipient_id'], $messageCount]);
                
                $stmt = $pdo->prepare("UPDATE schedules SET status = 'sent', sent_at = NOW() WHERE id = ?");
                $stmt->execute([$schedule['id']]);
                
                $results['sent']++;
                
            } catch (Exception $e) {
                $stmt = $pdo->prepare("UPDATE schedules SET status = 'failed', error_message = ? WHERE id = ?");
                $stmt->execute([$e->getMessage(), $schedule['id']]);
                $results['failed']++;
            }
            
            $results['processed']++;
        }
        
        $success = "Mensaje enviado. Procesados: {$results['processed']}, Enviados: {$results['sent']}, Fallidos: {$results['failed']}";
    }
}

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

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

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

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

<form method="POST">
    <div class="card border-0 shadow-sm">
        <div class="card-body">
            <div class="mb-3">
                <label class="form-label">Plataforma</label>
                <select name="platform" id="platformSelect" class="form-select" required>
                    <option value="">-- Seleccionar --</option>
                    <option value="discord">Discord</option>
                    <option value="telegram">Telegram</option>
                </select>
            </div>
            
            <div class="mb-3">
                <label class="form-label">Destinatario</label>
                <select name="recipient_id" id="recipientSelect" class="form-select" required disabled>
                    <option value="">Selecciona una plataforma primero</option>
                </select>
            </div>
            
            <div class="mb-3">
                <label class="form-label">Mensaje</label>
                <textarea name="content" id="messageContent" class="form-control" rows="10" required></textarea>
            </div>
            
            <button type="submit" class="btn btn-primary">
                <i class="bi bi-send"></i> Enviar Ahora
            </button>
        </div>
    </div>
</form>

<!-- Modal de Galería -->
<div class="modal fade" id="galleryModal" tabindex="-1">
    <div class="modal-dialog modal-lg modal-dialog-scrollable">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="bi bi-images"></i> Galería de Imágenes</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="row g-3">
                    <?php if (empty($galleryImages)): ?>
                        <div class="col-12 text-center text-muted py-5">
                            <i class="bi bi-images" style="font-size: 3rem;"></i>
                            <p class="mt-3">No hay imágenes en la galería</p>
                        </div>
                    <?php else: ?>
                        <?php foreach ($galleryImages as $image): ?>
                        <div class="col-md-3 col-sm-4 col-6">
                            <div class="card h-100 cursor-pointer" onclick="insertImage('galeria/<?= urlencode($image) ?>')" style="cursor: pointer;">
                                <img src="galeria/<?= urlencode($image) ?>" class="card-img-top" alt="<?= htmlspecialchars($image) ?>" style="height: 120px; object-fit: cover;">
                                <div class="card-body p-2">
                                    <small class="text-muted text-truncate d-block"><?= htmlspecialchars($image) ?></small>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-lite.min.js"></script>

<script>
const recipients = <?= json_encode($recipients) ?>;

$(document).ready(function() {
    // Inicializar Summernote con botón de galería personalizado
    $('#messageContent').summernote({
        height: 300,
        toolbar: [
            ['style', ['bold', 'italic', 'underline', 'clear']],
            ['font', ['strikethrough', 'superscript', 'subscript']],
            ['fontsize', ['fontsize']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['height', ['height']],
            ['insert', ['link', 'video', 'gallery']],
            ['view', ['fullscreen', 'codeview']]
        ],
        buttons: {
            gallery: function() {
                return $.summernote.ui.button({
                    contents: '<i class="bi bi-images"></i> Galería',
                    tooltip: 'Insertar imagen desde galería',
                    click: function() {
                        $('#galleryModal').modal('show');
                    }
                }).render();
            }
        }
    });
});

function insertImage(url) {
    const imgTag = '<img src="' + url + '" style="max-width: 100%; height: auto;">';
    $('#messageContent').summernote('editor.pasteHTML', imgTag);
    $('#galleryModal').modal('hide');
}

document.getElementById('platformSelect').addEventListener('change', function() {
    const platform = this.value;
    const select = document.getElementById('recipientSelect');
    
    select.innerHTML = '<option value="">-- Seleccionar --</option>';
    
    if (platform) {
        const filtered = recipients.filter(r => r.platform === platform);
        filtered.forEach(r => {
            const option = document.createElement('option');
            option.value = r.id;
            option.textContent = r.name + ' (' + r.type + ')';
            select.appendChild(option);
        });
        select.disabled = false;
    } else {
        select.disabled = true;
        select.innerHTML = '<option value="">Selecciona una plataforma primero</option>';
    }
});
</script>

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