//go:build windows
// +build windows

package client

import (
	"fmt"
	"log"
	"os"
	"os/exec"

	"tailscale-clone/internal/types"
)



// Windows-specific WireGuard interface management
func (c *Client) createInterface() error {
	log.Printf("Creating WireGuard interface %s on Windows", c.interfaceName)
	
	// On Windows, WireGuard interfaces are managed by the WireGuard Windows client
	// We'll try to use the existing interface or create a configuration file
	
	// Check if interface already exists
	_, err := c.wgClient.Device(c.interfaceName)
	if err == nil {
		log.Printf("Interface %s already exists", c.interfaceName)
		return nil
	}
	
	// Try to create interface using WireGuard Windows client
	// This is a simplified approach - in production you'd use the WireGuard Windows API
	log.Printf("Interface %s will be created by WireGuard Windows client", c.interfaceName)
	log.Printf("Please create a WireGuard tunnel named '%s' in the WireGuard Windows client", c.interfaceName)
	
	return nil
}

func (c *Client) setupSubnetRouting() error {
	if !c.isSubnetRouter || len(c.subnets) == 0 {
		return nil
	}

	log.Printf("Setting up subnet routing on Windows for: %v", c.subnets)
	
	// On Windows, we need to use netsh commands instead of sysctl
	// Enable IP forwarding (requires admin privileges)
	cmd := exec.Command("netsh", "interface", "ipv4", "set", "global", "forwarding=enabled")
	if err := cmd.Run(); err != nil {
		log.Printf("Warning: Failed to enable IP forwarding (requires admin): %v", err)
		log.Printf("You may need to run as Administrator")
	}

	// Add routes for advertised subnets using Windows route command
	for _, subnet := range c.subnets {
		// Parse subnet to get network and mask
		// This is simplified - you'd need proper CIDR parsing
		cmd := exec.Command("route", "add", subnet, "mask", "255.255.255.0", "0.0.0.0")
		if err := cmd.Run(); err != nil {
			log.Printf("Warning: Failed to add route for %s: %v", subnet, err)
		}
	}

	log.Printf("Subnet routing setup complete for: %v", c.subnets)
	return nil
}

func (c *Client) updateSubnetRouting() error {
	// Clear existing routes
	cmd := exec.Command("route", "delete", "0.0.0.0")
	if err := cmd.Run(); err != nil {
		log.Printf("Warning: Failed to clear routes: %v", err)
	}

	// Add routes for each subnet router
	for _, route := range c.subnetRoutes {
		if !route.Active {
			continue
		}

		// Get WireGuard device info
		device, err := c.wgClient.Device(c.interfaceName)
		if err != nil {
			continue
		}

		for _, peer := range device.Peers {
			if peer.Endpoint == nil {
				continue
			}
			
			for _, subnet := range route.Subnets {
				// Use Windows route command
				cmd := exec.Command("route", "add", subnet, "mask", "255.255.255.0", peer.Endpoint.IP.String())
				if err := cmd.Run(); err != nil {
					log.Printf("Warning: Failed to add route for %s via %s: %v", subnet, peer.Endpoint.IP, err)
				}
			}
		}
	}

	return nil
}

// Windows-specific helper functions
func isElevated() bool {
	// Simplified elevation check - in production you'd use proper Windows API
	// For now, we'll assume elevated if we can access system directories
	_, err := os.Open("\\\\.\\PHYSICALDRIVE0")
	return err == nil
}

func runAsAdmin() error {
	if isElevated() {
		return nil
	}
	
	// This is a simplified version - in production you'd use the Windows API properly
	log.Println("Administrator privileges required. Please run as Administrator.")
	return fmt.Errorf("administrator privileges required")
}

// Windows-specific interface configuration
func (c *Client) configureInterfacePlatform(networkCIDR string, peers []types.Peer) error {
	log.Printf("Configuring WireGuard interface %s on Windows", c.interfaceName)
	
	// On Windows, we need to work with the WireGuard Windows client
	// This is a simplified approach that logs the configuration
	
	log.Printf("Private Key: %s", c.privateKey)
	log.Printf("IP Address: %s", c.ip)
	log.Printf("Network: %s", networkCIDR)
	log.Printf("Peers: %d", len(peers))
	
	for i, peer := range peers {
		log.Printf("Peer %d: %s (Allowed IPs: %v)", i+1, peer.PublicKey[:16]+"...", peer.AllowedIPs)
	}
	
	log.Printf("Please configure the WireGuard tunnel '%s' manually with the above settings", c.interfaceName)
	log.Printf("Or use the WireGuard Windows client to import a configuration file")
	
	return nil
}

 