<?php
// paneladm.jatpluss.com/config/config.php

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

/* ---------- 1) IMAP ---------- */
define('IMAP_SERVER', 'link_hosting');
define('IMAP_PORT', 993);
define('IMAP_USER', 'correo_principal');
define('IMAP_PASSWORD', 'pass_correo');

/* ---------- 2) DB ---------- */
require_once __DIR__ . '/../admin/conexion.php';

/* ---------- 3) Cargar asuntos desde DB ---------- */
function getSubjectsByPlatform(PDO $pdo): array
{
    $stmt = $pdo->prepare(
        "SELECT platform, subject_line
         FROM email_subjects
         WHERE active = 1
         ORDER BY platform, subject_line"
    );
    $stmt->execute();

    $map = [];
    foreach ($stmt as $row) {
        $map[$row['platform']][] = $row['subject_line'];
    }
    return $map;
}

$ASUNTOS_POR_PLATAFORMA = getSubjectsByPlatform($pdo);

/* ---------- 4) IMAP helpers ---------- */
$inbox = null;

function open_imap_connection()
{
    global $inbox;
    $inbox = imap_open(
        '{' . IMAP_SERVER . ':' . IMAP_PORT . '/imap/ssl}INBOX',
        IMAP_USER,
        IMAP_PASSWORD
    ) or die("Error al conectar con IMAP: " . imap_last_error());
}

function close_imap_connection()
{
    global $inbox;
    if ($inbox) {
        imap_close($inbox);
    }
}

open_imap_connection();
register_shutdown_function('close_imap_connection');
