/**
 * @file lwipopts.h
 * @brief lwIP Configuration for NexusOS Membrane
 */

#ifndef LWIP_LWIPOPTS_H
#define LWIP_LWIPOPTS_H

// --- LwIP Debug Constants (Needed before opt.h defines them) ---
#define LWIP_DBG_ON            0x80U
#define LWIP_DBG_OFF           0x00U
#define LWIP_DBG_TRACE         0x40U
#define LWIP_DBG_STATE         0x20U
#define LWIP_DBG_FRESH         0x10U
#define LWIP_DBG_HALT          0x08U

#define NO_SYS                  1
#define LWIP_SOCKET             0
#define LWIP_NETCONN            0

// DHCP Support
#define LWIP_DHCP               1
#define LWIP_ACD                0
#define LWIP_DHCP_DOES_ACD_CHECK 0
#define LWIP_AUTOIP             0
#define LWIP_UDP                1
#define LWIP_NETIF_HOSTNAME     1
#define LWIP_RAW                1

// DNS & TCP
#define LWIP_DNS                1
#define DNS_TABLE_SIZE          4
#define DNS_MAX_NAME_LENGTH     256
#define LWIP_TCP                1
#define TCP_MSS                 1460
#define TCP_WND                 (4 * TCP_MSS)
#define TCP_SND_BUF             (4 * TCP_MSS)

// Performance & Memory: Tank Mode (Unified Heap)
#define MEM_LIBC_MALLOC         1
#define MEMP_MEM_MALLOC         1
#define MEM_ALIGNMENT           8
#define SYS_LIGHTWEIGHT_PROT    0  // Hephaestus: Disable in NO_SYS mode
#define MEM_SIZE                (2 * 1024 * 1024)
#define MEMP_NUM_PBUF           128
#define MEMP_NUM_UDP_PCB        32  
#define MEMP_NUM_TCP_PCB        16
#define PBUF_POOL_SIZE          128
#define MEMP_NUM_SYS_TIMEOUT    64

// DECISION(DNS): Disable DNS Secure Randomization (random source ports/XID)
// This forces dns_enqueue() to use dns_pcbs[0] directly instead of calling
// dns_alloc_pcb() which was failing with ERR_MEM due to dynamic allocation.
// Our net_glue.nim injects dns_pcbs[0] explicitly - this ensures it's used.
#define LWIP_DNS_SECURE         0

// Network Interface
#define LWIP_ETHERNET           1
#define LWIP_ARP                1
#define LWIP_TIMERS             1
#define ETHARP_SUPPORT_VLAN     0

// Checksum Configuration
// CHECK disabled (don't validate incoming - helps debug)
// GEN enabled (QEMU user-mode networking requires valid checksums)
#define CHECKSUM_CHECK_UDP      0
#define CHECKSUM_CHECK_TCP      0
#define CHECKSUM_CHECK_IP       0
#define CHECKSUM_CHECK_ICMP     0
#define CHECKSUM_GEN_UDP        1
#define CHECKSUM_GEN_TCP        1
#define CHECKSUM_GEN_IP         1
#define CHECKSUM_GEN_ICMP       1

// Loopback Support
#define LWIP_HAVE_LOOPIF        1
#define LWIP_NETIF_LOOPBACK     1
#define LWIP_LOOPBACK_MAX_PBUFS 8

// Debugging (Loud Mode)
#define LWIP_DEBUG              1
#define LWIP_PLATFORM_DIAG(x)   lwip_platform_diag x

extern int printf(const char *format, ...);
#define LWIP_ASSERT(message, assertion) do { if(!(assertion)) { \
  printf("\n[LwIP ASSERT] %s\n", message); \
  while(1); \
}} while(0)

#define DHCP_DEBUG              (LWIP_DBG_ON | LWIP_DBG_TRACE | LWIP_DBG_STATE)
#define UDP_DEBUG               (LWIP_DBG_ON | LWIP_DBG_TRACE)
#define NETIF_DEBUG             (LWIP_DBG_ON | LWIP_DBG_TRACE | LWIP_DBG_STATE)
#define IP_DEBUG                (LWIP_DBG_ON | LWIP_DBG_TRACE)
#define ICMP_DEBUG              (LWIP_DBG_ON | LWIP_DBG_TRACE)
#define LWIP_STATS              0
#define MEMP_STATS              0
#define SYS_STATS               0
#define MEM_STATS               0
#define MEMP_DEBUG              (LWIP_DBG_ON | LWIP_DBG_TRACE)
#define ETHERNET_DEBUG          (LWIP_DBG_ON | LWIP_DBG_TRACE)
#define ETHARP_DEBUG            (LWIP_DBG_ON | LWIP_DBG_TRACE)
#define DNS_DEBUG               (LWIP_DBG_ON | LWIP_DBG_TRACE | LWIP_DBG_STATE)

#define LWIP_DBG_MIN_LEVEL      0
#define LWIP_DBG_TYPES_ON       0xFFU

// Endianness
#undef BYTE_ORDER
#define BYTE_ORDER 1234

#endif
