<?php
/**
 * Template Builder Management - Admin Interface
 * Allows users to manage their custom templates
 */

// Include core
require_once dirname(__FILE__) . '/../../f_core/config.core.php';

// Check if user is logged in
if (!isset($_SESSION['USER_ID']) || $_SESSION['USER_ID'] <= 0) {
    header('Location: /signin.php');
    exit;
}

// Initialize template builder
require_once $cfg['classes_dir'] . '/class.templatebuilder.php';
$templateBuilder = new VTemplateBuilder();

// Handle actions
$action = isset($_GET['action']) ? $_GET['action'] : 'list';
$message = '';
$messageType = '';

switch ($action) {
    case 'new':
        // Show builder for new template
        showBuilder(0, $templateBuilder);
        exit;

    case 'edit':
        // Show builder for existing template
        $templateId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
        showBuilder($templateId, $templateBuilder);
        exit;

    case 'delete':
        $templateId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
        $result = $templateBuilder->deleteTemplate($templateId);
        if ($result['success']) {
            $message = 'Template deleted successfully';
            $messageType = 'success';
        } else {
            $message = $result['error'];
            $messageType = 'error';
        }
        break;

    case 'duplicate':
        $templateId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
        $result = $templateBuilder->duplicateTemplate($templateId);
        if ($result['success']) {
            $message = 'Template duplicated successfully';
            $messageType = 'success';
        } else {
            $message = $result['error'];
            $messageType = 'error';
        }
        break;

    case 'toggle_active':
        $templateId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
        $isActive = isset($_GET['is_active']) ? (int)$_GET['is_active'] : 0;
        $result = $templateBuilder->updateTemplate($templateId, ['is_active' => $isActive]);
        if ($result['success']) {
            $message = 'Template ' . ($isActive ? 'activated' : 'deactivated') . ' successfully';
            $messageType = 'success';
        } else {
            $message = $result['error'];
            $messageType = 'error';
        }
        break;
}

// Get all templates
$templates = $templateBuilder->getUserTemplates();
$userPrefs = $templateBuilder->getUserPreferences();

// Assign to Smarty
$smarty->assign('templates', $templates);
$smarty->assign('user_prefs', $userPrefs);
$smarty->assign('message', $message);
$smarty->assign('message_type', $messageType);

// Display page
echo $smarty->fetch('tpl_backend/tpl_template_manager.tpl');

/**
 * Show template builder interface
 */
function showBuilder($templateId, $templateBuilder)
{
    global $smarty, $cfg;

    $template = null;
    $templateJson = '{}';

    if ($templateId > 0) {
        $template = $templateBuilder->getTemplate($templateId);
        if (!$template) {
            header('Location: /account.php?s=templates&error=not_found');
            exit;
        }
        $templateJson = json_encode($template);
    }

    $smarty->assign('template', $template);
    $smarty->assign('template_json', $templateJson);
    $smarty->assign('page_title', $templateId > 0 ? 'Edit Template' : 'New Template');

    echo $smarty->fetch('tpl_frontend/tpl_builder/tpl_builder_main.tpl');
}
