package pathfinder

import (
	"sync"
)

// PathingPolyFlags defines different polygon flags for pathfinding
type PathingPolyFlags int32

const (
	PathingNormal      PathingPolyFlags = 1
	PathingWater       PathingPolyFlags = 2
	PathingLava        PathingPolyFlags = 4
	PathingZoneLine    PathingPolyFlags = 8
	PathingPvP         PathingPolyFlags = 16
	PathingSlime       PathingPolyFlags = 32
	PathingIce         PathingPolyFlags = 64
	PathingVWater      PathingPolyFlags = 128
	PathingGeneralArea PathingPolyFlags = 256
	PathingPortal      PathingPolyFlags = 512
	PathingPrefer      PathingPolyFlags = 1024
	PathingDisabled    PathingPolyFlags = 2048
	PathingAll         PathingPolyFlags = 65535
	PathingNotDisabled PathingPolyFlags = PathingAll ^ PathingDisabled
)

// PathfinderOptions configures pathfinding behavior
type PathfinderOptions struct {
	Flags      PathingPolyFlags // Allowed polygon flags
	SmoothPath bool             // Whether to smooth the final path
	StepSize   float32          // Step size for path smoothing
	FlagCost   [10]float32      // Cost multipliers for each flag type
	Offset     float32          // Offset from polygon edges
}

// PathNode represents a single node in a path
type PathNode struct {
	Position [3]float32 // World position (x, y, z)
	Teleport bool       // Whether this is a teleport node
}

// Path represents a complete path from start to end
type Path struct {
	Nodes    []*PathNode // Ordered list of path nodes
	Partial  bool        // Whether this is a partial path (couldn't reach destination)
	Distance float32     // Total path distance
}

// PathfindingResult contains the results of a pathfinding operation
type PathfindingResult struct {
	Path     *Path       // The generated path (nil if no path found)
	Partial  bool        // Whether the path is partial
	Stuck    bool        // Whether the pathfinder got stuck
	Distance float32     // Total path distance
	NodeCount int        // Number of nodes in the path
}

// PathfindingBackend defines the interface for pathfinding implementations
type PathfindingBackend interface {
	// FindRoute finds a route between two points with basic options
	FindRoute(start, end [3]float32, flags PathingPolyFlags) *PathfindingResult
	
	// FindPath finds a path between two points with advanced options
	FindPath(start, end [3]float32, options *PathfinderOptions) *PathfindingResult
	
	// GetRandomLocation returns a random walkable location near the start point
	GetRandomLocation(start [3]float32) [3]float32
	
	// IsLoaded returns whether the pathfinding data is loaded
	IsLoaded() bool
	
	// GetName returns the name of this pathfinding backend
	GetName() string
}

// PathfinderManager manages pathfinding for a zone
type PathfinderManager struct {
	zoneName    string              // Zone name for this pathfinder
	backend     PathfindingBackend  // Active pathfinding backend
	fallback    PathfindingBackend  // Fallback backend (usually null pathfinder)
	enabled     bool                // Whether pathfinding is enabled
	mutex       sync.RWMutex        // Thread safety
	
	// Statistics
	pathRequests     int64   // Total path requests
	successfulPaths  int64   // Successful path generations
	partialPaths     int64   // Partial path generations
	failedPaths      int64   // Failed path generations
	averagePathTime  float64 // Average pathfinding time in milliseconds
}

// PathfindingStats contains pathfinding statistics
type PathfindingStats struct {
	TotalRequests    int64   `json:"total_requests"`
	SuccessfulPaths  int64   `json:"successful_paths"`
	PartialPaths     int64   `json:"partial_paths"`
	FailedPaths      int64   `json:"failed_paths"`
	SuccessRate      float64 `json:"success_rate"`
	AveragePathTime  float64 `json:"average_path_time_ms"`
	BackendName      string  `json:"backend_name"`
	IsEnabled        bool    `json:"is_enabled"`
}