<?php
/**
 * API: CertificarR Factura FEL (Versión XML con Lógica Exento IVA)
 * Adaptado para RENOVADO2026 (MySQLi + sucursal_config)
 */

header('Content-Type: application/json; charset=utf-8');
date_default_timezone_set('America/Guatemala');

require_once __DIR__ . '/../../config/fel_config.php';
require_once __DIR__ . '/../../config/db_connection.php';
require_once __DIR__ . '/../../config/sucursal_config.php'; // sets $sucursal_id

try {
    $input = json_decode(file_get_contents('php://input'), true);
    if (!$input) throw new Exception('Datos inválidos');

    // 1. CARGAR CONFIGURACIÓN DE SUCURSAL ACTIVA
    // $sucursal_id ya está definido en sucursal_config.php
    $idSucursalActiva = isset($sucursal_id) ? (int)$sucursal_id : 2;

    // 2. OBTENER DATOS DE LA SUCURSAL EN LA BD
    $stmtSuc = $conn->prepare("SELECT * FROM sucursales WHERE id = ?");
    $stmtSuc->bind_param("i", $idSucursalActiva);
    $stmtSuc->execute();
    $dbSucursal = $stmtSuc->get_result()->fetch_assoc();
    $stmtSuc->close();

    if (!$dbSucursal) {
        throw new Exception("La sucursal ID $idSucursalActiva no existe en la base de datos.");
    }

    $nombreComercial = !empty($dbSucursal['nombre_fel']) ? $dbSucursal['nombre_fel'] : $dbSucursal['nombre'];
    $codigoSat = !empty($dbSucursal['codigo_establecimiento']) ? $dbSucursal['codigo_establecimiento'] : 1;
    
    $direccionEmisor = $dbSucursal['direccion'];
    $municipioEmisor = !empty($dbSucursal['municipio']) ? $dbSucursal['municipio'] : 'GUATEMALA';
    $departamentoEmisor = !empty($dbSucursal['departamento']) ? $dbSucursal['departamento'] : 'GUATEMALA';

    // Datos recibidos del Frontend
    $venta_id = $input['venta_id'] ?? null;
    $buyer = $input['buyer']; 
    $items = $input['items']; 
    $observations = $input['observations'] ?? 'Gracias por su compra';

    // 3. Obtener Token
    $token = getFELToken();
    if (!$token) throw new Exception('No se pudo obtener token de Digifact');

    // 4. Preparar Datos Generales (CON LÓGICA DE FECHA RETROACTIVA)
    if (!empty($input['fecha_emision'])) {
        $fecha_base = $input['fecha_emision']; // Viene YYYY-MM-DD
        $hora_actual = date("H:i:s");
        $fecha_completa = $fecha_base . "T" . $hora_actual;
    } else {
        $fecha_completa = date("Y-m-d\TH:i:s");
    }

    $fecha_emision = $fecha_completa . "-06:00";
    $referencia = "FAC-" . time();

    // 5. Preparar Cliente
    $buyer_tax_id = strtoupper(trim($buyer['taxID']));
    $buyer_name   = strtoupper(trim($buyer['name']));
    $buyer_address = strtoupper(trim($buyer['address']));
    $buyer_type   = $buyer['type'] ?? 'NIT'; 

    if ($buyer_type === 'CF' || $buyer_tax_id === 'CF' || $buyer_tax_id === 'C/F') {
        $buyer_tax_id = 'CF';
        $buyer_name = 'CONSUMIDOR FINAL'; 
        $buyer_address = 'CIUDAD';
    }

    // 6. Procesar Items (LOGICA EXENTO IVA)
    $invoice_items = [];
    $total_iva = 0;
    $total_invoice = 0;
    $tiene_exentos = false; 

    foreach ($items as $index => $item) {
        $cantidad    = floatval($item['cantidad']);
        $precio_unit = floatval($item['precio']);
        $total_linea = $cantidad * $precio_unit;
        
        $es_exento = isset($item['exento_iva']) && $item['exento_iva'] == 1;

        if ($es_exento) {
            $base_imponible = number_format($total_linea, 6, '.', '');
            $monto_iva = "0.000000";
            $codigo_impuesto = "2"; 
            $tiene_exentos = true;
        } else {
            $base = $total_linea / 1.12;
            $iva  = $total_linea - $base;
            
            $base_imponible = number_format($base, 6, '.', '');
            $monto_iva      = number_format($iva, 6, '.', '');
            $codigo_impuesto = "1"; 
        }

        $total_iva += (float)$monto_iva;
        $total_invoice += $total_linea;

        $invoice_items[] = [
            "Number" => (string)($index + 1),
            "Type" => "Bien",
            "Description" => substr($item['producto_nombre'] ?? $item['NombreComercial'] ?? 'Producto', 0, 490),
            "Qty" => number_format($cantidad, 6, '.', ''),
            "UnitOfMeasure" => "UNI",
            "Price" => number_format($precio_unit, 6, '.', ''),
            "Taxes" => [
                "Tax" => [[
                    "Code" => $codigo_impuesto, 
                    "Description" => "IVA",
                    "TaxableAmount" => $base_imponible,
                    "Amount" => $monto_iva
                ]]
            ],
            "Totals" => [
                "TotalItem" => number_format($total_linea, 6, '.', '')
            ]
        ];
    }

    // 7. Preparar Frases
    $frases = [
        ["Name" => "TipoFrase", "Data" => "1", "Value" => "1"],
        ["Name" => "Escenario", "Data" => "1", "Value" => "1"]
    ];

    if ($tiene_exentos) {
        $frases[] = ["Name" => "TipoFrase", "Data" => "2", "Value" => "4"];
        $frases[] = ["Name" => "Escenario", "Data" => "2", "Value" => "10"];
    }

    // 8. Datos del Comprador
    $buyerNode = [
        "TaxID" => $buyer_tax_id,
        "Name" => $buyer_name,
        "AddressInfo" => [
            "Address" => $buyer_address,
            "City" => "01001",
            "District" => "GUATEMALA",
            "State" => "GUATEMALA",
            "Country" => "GT"
        ]
    ];

    if ($buyer_type === 'CUI') {
        $buyerNode["TaxIDType"] = "CUI";
    }

    // 9. Construir JSON para Digifact
    $factura = [
        "Version" => "1.00",
        "CountryCode" => "GT",
        "Header" => [
            "DocType" => "FACT",
            "IssuedDateTime" => $fecha_emision,
            "Currency" => "GTQ"
        ],
        "Seller" => [
            "TaxID" => FEL_NIT_EMISOR,
            "Name" => FEL_RAZON_SOCIAL,
            "TaxIDAdditionalInfo" => [ ["Name" => "AfiliacionIVA", "Data" => null, "Value" => "GEN"] ],
            "BranchInfo" => [
                "Code" => (string)$codigoSat, 
                "Name" => $nombreComercial,   
                "AddressInfo" => [
                    "Address" => $direccionEmisor,
                    "City" => "01001",
                    "District" => $municipioEmisor,    
                    "State" => $departamentoEmisor,
                    "Country" => "GT"
                ]
            ],
            "AdditionlInfo" => $frases
        ],
        "Buyer" => $buyerNode,
        "Items" => $invoice_items,
        "Totals" => [
            "TotalTaxes" => [
                "TotalTax" => [[
                    "Description" => "IVA",
                    "Amount" => number_format($total_iva, 6, '.', '')
                ]]
            ],
            "GrandTotal" => [
                "InvoiceTotal" => number_format($total_invoice, 6, '.', '')
            ]
        ],
        "AdditionalDocumentInfo" => [
            "AdditionalInfo" => [[
                "Code" => $referencia,
                "Type" => "ADENDA",
                "AditionalData" => [
                    "Data" => [[
                        "Name" => "INFORMACION_ADICIONAL",
                        "Info" => [[
                            "Name" => "OBSERVACIONES",
                            "Data" => null,
                            "Value" => $observations
                        ]]
                    ]]
                ],
                "AditionalInfo" => [[
                    "Name" => "VALIDAR_REFERENCIA_INTERNA",
                    "Data" => null,
                    "Value" => "NO_VALIDAR"
                ]]
            ]]
        ]
    ];

    // 10. Enviar a Digifact
    $json_nuc = json_encode($factura);
    $nit_pad = str_pad(FEL_NIT_EMISOR, 12, "0", STR_PAD_LEFT);
    
    $url = FEL_CERT_URL . "?" . http_build_query([
        "TAXID" => $nit_pad, 
        "USERNAME" => FEL_USUARIO, 
        "FORMAT" => "XML"
    ]);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_nuc);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Authorization: " . $token,
        "Content-Type: application/json"
    ]);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $res = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode !== 200) throw new Exception('Error HTTP ' . $httpCode . ' al conectar con certificador.');

    $resultado = json_decode($res, true);

    // 11. Respuesta al Frontend
    if (isset($resultado['code']) && ($resultado['code'] == 1 || $resultado['code'] == "1")) {
        $datosParaFrontend = [
            'uuid' => $resultado['authNumber'],
            'serie' => $resultado['batch'],
            'numero' => $resultado['serial'],
            'xml' => $resultado['responseData1'],
            'fecha' => $resultado['authTime'],
            'emisor' => [
                'nombre' => $nombreComercial,
                'nit' => FEL_NIT_EMISOR,
                'direccion' => $direccionEmisor . ", " . $municipioEmisor . ", " . $departamentoEmisor
            ],
            'receptor' => $buyerNode,
            'items' => $invoice_items,
            'total' => $total_invoice
        ];

        if ($venta_id) {
            // Actualizar estado en detalle_ventas
            $stmt = $conn->prepare("UPDATE detalle_ventas SET facturado = 1, estado_factura = 'FACTURADO' WHERE venta_id = ?");
            $stmt->bind_param("i", $venta_id);
            $stmt->execute();
            $stmt->close();

            // Guardar en fel_facturas para reimpresión
            $stmtFel = $conn->prepare("INSERT INTO fel_facturas (venta_id, uuid, serie, numero, datos_json) VALUES (?, ?, ?, ?, ?)");
            $datos_json = json_encode($datosParaFrontend, JSON_UNESCAPED_UNICODE);
            $auth_number = $resultado['authNumber'] ?? '';
            $batch = $resultado['batch'] ?? '';
            $serial = $resultado['serial'] ?? '';
            $stmtFel->bind_param("issss", $venta_id, $auth_number, $batch, $serial, $datos_json);
            $stmtFel->execute();
            $stmtFel->close();
        }

        echo json_encode([
            'success' => true,
            'data' => $datosParaFrontend
        ], JSON_UNESCAPED_UNICODE);
    } else {
        $msg = $resultado['message'] ?? 'Error desconocido';
        $desc = $resultado['description'] ?? '';
        throw new Exception("Digifact Error: $msg - $desc");
    }

} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'error' => $e->getMessage()], JSON_UNESCAPED_UNICODE);
}

$conn->close();
?>
