#!/usr/bin/env python3
"""
NimPacket Python Bindings - Basic Usage

Run from the python/ directory:
    cd python
    python examples/basic_usage.py
"""

import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

import nimpacket as np

print("NimPacket", np.version())
print("Protocols:", np.supported_protocols())
print()

# --- DNS ---
print("=== DNS ===")
query = np.create_dns_query("google.com", np.DNS_TYPE_A, 0x1234)
print(f"DNS query for google.com: {len(query)} bytes")
parsed = np.parse_dns_packet(query)
print(f"  Transaction ID: {parsed['transaction_id']}")
print(f"  Questions: {parsed['questions']}")

encoded = np.encode_domain_name("www.example.com")
print(f"  Encoded 'www.example.com': {list(encoded)}")
print()

# --- DHCP ---
print("=== DHCP ===")
discover = np.create_dhcp_discover("AA:BB:CC:DD:EE:FF", 0xDEADBEEF)
print(f"DHCP DISCOVER: {len(discover)} bytes")
parsed = np.parse_dhcp_packet(discover)
print(f"  Message type: {parsed['message_type']}")
print(f"  Transaction ID: {parsed['transaction_id']}")
print(f"  Client MAC: {parsed['client_mac']}")
print(f"  Options: {parsed['options']}")

request = np.create_dhcp_request("AA:BB:CC:DD:EE:FF", "192.168.1.100", "192.168.1.1", 0xCAFEBABE)
parsed = np.parse_dhcp_packet(request)
print(f"DHCP REQUEST: type={parsed['message_type']}, options={len(parsed['options'])}")

release = np.create_dhcp_release("AA:BB:CC:DD:EE:FF", "192.168.1.100", "192.168.1.1")
parsed = np.parse_dhcp_packet(release)
print(f"DHCP RELEASE: type={parsed['message_type']}")
print()

# --- TCP SYN Packet ---
print("=== TCP SYN ===")
syn = np.build_tcp_syn_packet("192.168.1.100", "10.0.0.1", 54321, 80)
print(f"TCP SYN packet: {len(syn)} bytes")
parsed = np.parse_packet(syn)
print(f"  {parsed['ipv4']['src_ip']}:{parsed['tcp']['src_port']} -> {parsed['ipv4']['dst_ip']}:{parsed['tcp']['dst_port']}")
print(f"  TTL={parsed['ipv4']['ttl']}, Flags={parsed['tcp']['flags']}")
print()

# --- ICMP Ping ---
print("=== ICMP Ping ===")
ping = np.build_icmp_echo_packet("192.168.1.100", "8.8.8.8", 1234, 1)
print(f"ICMP Echo Request: {len(ping)} bytes")
parsed = np.parse_packet(ping)
print(f"  {parsed['ipv4']['src_ip']} -> {parsed['ipv4']['dst_ip']}")
print(f"  Type={parsed['icmp']['type']}, ID={parsed['icmp']['identifier']}, Seq={parsed['icmp']['sequence']}")
print()

# --- UDP Packet ---
print("=== UDP ===")
payload = b"Hello NimPacket!"
udp = np.build_udp_packet("10.0.0.1", "10.0.0.2", 12345, 53, payload)
print(f"UDP packet: {len(udp)} bytes")
parsed = np.parse_packet(udp)
print(f"  {parsed['ipv4']['src_ip']}:{parsed['udp']['src_port']} -> {parsed['ipv4']['dst_ip']}:{parsed['udp']['dst_port']}")
print(f"  Payload length: {parsed['payload_length']}")
print()

# --- ARP ---
print("=== ARP ===")
arp = np.create_arp_request("AA:BB:CC:DD:EE:FF", "192.168.1.100", "192.168.1.1")
print(f"ARP request: {len(arp)} bytes")
parsed = np.parse_arp_packet(arp)
print(f"  {parsed['sender_mac']} ({parsed['sender_ip']}) -> who has {parsed['target_ip']}?")
print()

# --- Ethernet ---
print("=== Ethernet ===")
eth = np.create_ethernet_header("AA:BB:CC:DD:EE:FF", "FF:FF:FF:FF:FF:FF", np.ETHERTYPE_IPV4)
print(f"Ethernet header: {len(eth)} bytes")
parsed = np.parse_ethernet_header(eth)
print(f"  {parsed['src_mac']} -> {parsed['dst_mac']}, type=0x{parsed['ether_type']:04X}")
print()

# --- Utilities ---
print("=== Utilities ===")
print(f"  parse_ipv4('192.168.1.1') = {np.parse_ipv4('192.168.1.1')}")
print(f"  ip_to_string(3232235777) = {np.ip_to_string(3232235777)}")
print(f"  parse_mac('AA:BB:CC:DD:EE:FF') = {np.parse_mac('AA:BB:CC:DD:EE:FF').hex(':')}")
print(f"  htons(80) = {np.htons(80)}")
print(f"  is_admin() = {np.is_admin()}")
print()

print("All examples passed!")
