<?php
require_once __DIR__ . '/db_connection.php';
header('Content-Type: application/json');

$response = ['success' => false, 'message' => 'Método No Permitido'];

try {
    $primary = $_POST['branding_color_primary'] ?? null;
    $secondary = $_POST['branding_color_secondary'] ?? null;
    $accent = $_POST['branding_color_accent'] ?? null;

    if ($primary && $secondary && $accent) {
        if (!preg_match('/^#[a-f0-9]{6}$/i', $primary)) { $primary = '#000000'; }
        if (!preg_match('/^#[a-f0-9]{6}$/i', $secondary)) { $secondary = '#000000'; }
        if (!preg_match('/^#[a-f0-9]{6}$/i', $accent)) { $accent = '#000000'; }
        
        $sql = "UPDATE business_info SET 
                    branding_color_primary = :primary_color,
                    branding_color_secondary = :secondary_color,
                    branding_color_accent = :accent_color
                WHERE id = 1";
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':primary_color', $primary);
        $stmt->bindParam(':secondary_color', $secondary);
        $stmt->bindParam(':accent_color', $accent);

        if ($stmt->execute()) {
            $response['success'] = true;
            $response['message'] = 'SiteFlow Actualizado Exitosamente';
        } else {
            $response['message'] = 'Método No Permitido.';
        }
    }
} catch (Exception $e) {
    error_log('Error en colors-update.php: ' . $e->getMessage());
    $response['message'] = 'Excepción del servidor.';
}

echo json_encode($response);
?>