<?php
// update_caracteristica.php
// Proxy a Node para actualizar tb_caracteristicas_productos (sin MySQL en PHP)

include '../includes/session_control.php';
require_once '../cotizaciones/api/_node.php';
require_once __DIR__ . '/../includes/user_activity_log.php';

header('Content-Type: application/json; charset=UTF-8');

$action = isset($_POST['action']) ? (string)$_POST['action'] : '';

$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$idProducto = isset($_POST['idProducto']) ? intval($_POST['idProducto']) : 0;
$idCaracteristica = isset($_POST['idCaracteristica']) ? intval($_POST['idCaracteristica']) : 0;
$valor = isset($_POST['valor']) ? (string)$_POST['valor'] : '';

$token = cpn_node_token_or_fail();
$base = cpn_node_api_base();

// DELETE
if ($action === 'delete') {
    if (!$id) {
        echo json_encode(['ok' => false, 'error' => 'Parámetros inválidos']);
        exit;
    }

    $r = cpn_node_request('DELETE', $base . '/api/productos/caracteristicas/' . urlencode((string)$id), $token, null);
    if (!empty($r['ok'])) {
        cpn_activity_log_event(
            'Productos',
            'productos.caracteristica_delete',
            'Eliminó característica del producto (registro #' . $id . ')',
            'producto_caracteristica',
            $id
        );
        echo json_encode(['ok' => true]);
        exit;
    }

    echo json_encode(['ok' => false, 'error' => ($r['error']['message'] ?? 'Error')]);
    exit;
}

// ADD
if ($action === 'add') {
    if (!$idProducto || !$idCaracteristica) {
        echo json_encode(['ok' => false, 'error' => 'Parámetros inválidos']);
        exit;
    }

    $r = cpn_node_request('POST', $base . '/api/productos/' . urlencode((string)$idProducto) . '/caracteristicas/add', $token, [
        'idCaracteristica' => $idCaracteristica,
        'valor' => $valor,
    ]);

    if (!empty($r['ok'])) {
        cpn_activity_log_event(
            'Productos',
            'productos.caracteristica_add',
            'Agregó característica al producto #' . $idProducto,
            'producto',
            $idProducto,
            ['idCaracteristica' => $idCaracteristica]
        );
        echo json_encode(['ok' => true, 'data' => $r['data'] ?? null]);
        exit;
    }

    echo json_encode(['ok' => false, 'error' => ($r['error']['message'] ?? 'Error')]);
    exit;
}

// UPDATE
if (!$id || !$idCaracteristica) {
    echo json_encode(['ok' => false, 'error' => 'Parámetros inválidos']);
    exit;
}

$r = cpn_node_request('PATCH', $base . '/api/productos/caracteristicas/' . urlencode((string)$id), $token, [
    'idCaracteristica' => $idCaracteristica,
    'valor' => $valor,
]);

if (!empty($r['ok'])) {
    cpn_activity_log_event(
        'Productos',
        'productos.caracteristica_update',
        'Actualizó característica del producto (registro #' . $id . ')',
        'producto_caracteristica',
        $id,
        ['idCaracteristica' => $idCaracteristica]
    );
    echo json_encode(['ok' => true]);
    exit;
}

echo json_encode(['ok' => false, 'error' => ($r['error']['message'] ?? 'Error')]);
