<?php
require_once __DIR__ . '/../../includes/db.php';
require_once __DIR__ . '/../../includes/session_check.php';
require_once __DIR__ . '/../../includes/activity_logger.php';

requireAdmin();

$pageTitle = 'Configuración del Bot de Telegram';

try {
    $pdo = getDbConnection();
    $stmt = $pdo->query("SELECT * FROM telegram_bot_messages WHERE id = 1");
    $config = $stmt->fetch();
} catch (Exception $e) {
    $error = $e->getMessage();
}

$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $messageText = $_POST['message_text'] ?? '';
    $buttonText = $_POST['button_text'] ?? '';
    $groupInviteLink = $_POST['group_invite_link'] ?? '';
    $isActive = isset($_POST['is_active']) ? 1 : 0;
    $registerUsers = isset($_POST['register_users']) ? 1 : 0;
    
    $stmt = $pdo->prepare("
        UPDATE telegram_bot_messages 
        SET message_text = ?, button_text = ?, group_invite_link = ?, is_active = ?, register_users = ?
        WHERE id = 1
    ");
    $stmt->execute([$messageText, $buttonText, $groupInviteLink, $isActive, $registerUsers]);
    
    logActivity(getCurrentUserId(), 'update_telegram_config', 'Configuración del bot de Telegram actualizada');
    $success = 'Configuración guardada correctamente';
    
    $stmt = $pdo->query("SELECT * FROM telegram_bot_messages WHERE id = 1");
    $config = $stmt->fetch();
}

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

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-telegram"></i> Configuración del Bot de Telegram</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 mb-4">
        <div class="card-header bg-white border-0">
            <h5 class="mb-0">Mensaje de Bienvenida</h5>
        </div>
        <div class="card-body">
            <div class="mb-3">
                <label class="form-label">Mensaje de bienvenida</label>
                <textarea name="message_text" class="form-control" rows="5"><?= htmlspecialchars($config['message_text'] ?? '') ?></textarea>
                <small class="text-muted">Usa <code>{user_name}</code> para el nombre del usuario</small>
            </div>
            
            <div class="mb-3">
                <label class="form-label">Texto del botón</label>
                <input type="text" name="button_text" class="form-control" value="<?= htmlspecialchars($config['button_text'] ?? '') ?>" placeholder="Unirse al grupo">
            </div>
            
            <div class="mb-3">
                <label class="form-label">Enlace de invitación al grupo</label>
                <input type="text" name="group_invite_link" class="form-control" value="<?= htmlspecialchars($config['group_invite_link'] ?? '') ?>" placeholder="https://t.me/...">
            </div>
        </div>
    </div>
    
    <div class="card border-0 shadow-sm mb-4">
        <div class="card-header bg-white border-0">
            <h5 class="mb-0">Opciones</h5>
        </div>
        <div class="card-body">
            <div class="form-check form-switch mb-3">
                <input class="form-check-input" type="checkbox" name="is_active" id="isActive" <?= ($config['is_active'] ?? true) ? 'checked' : '' ?>>
                <label class="form-check-label" for="isActive">Mensaje de bienvenida activo</label>
            </div>
            
            <div class="form-check form-switch">
                <input class="form-check-input" type="checkbox" name="register_users" id="registerUsers" <?= ($config['register_users'] ?? true) ? 'checked' : '' ?>>
                <label class="form-check-label" for="registerUsers">Registrar usuarios automáticamente</label>
            </div>
        </div>
    </div>
    
    <button type="submit" class="btn btn-primary">
        <i class="bi bi-save"></i> Guardar Configuración
    </button>
</form>

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