package heroic_ops

import (
	"context"
	"time"
)

// HeroicOPDatabase defines the interface for database operations
type HeroicOPDatabase interface {
	// Starter operations
	LoadStarters(ctx context.Context) ([]HeroicOPData, error)
	LoadStarter(ctx context.Context, starterID int32) (*HeroicOPData, error)
	SaveStarter(ctx context.Context, starter *HeroicOPStarter) error
	DeleteStarter(ctx context.Context, starterID int32) error

	// Wheel operations
	LoadWheels(ctx context.Context) ([]HeroicOPData, error)
	LoadWheelsForStarter(ctx context.Context, starterID int32) ([]HeroicOPData, error)
	LoadWheel(ctx context.Context, wheelID int32) (*HeroicOPData, error)
	SaveWheel(ctx context.Context, wheel *HeroicOPWheel) error
	DeleteWheel(ctx context.Context, wheelID int32) error

	// Instance operations
	SaveHOInstance(ctx context.Context, ho *HeroicOP) error
	LoadHOInstance(ctx context.Context, instanceID int64) (*HeroicOP, error)
	DeleteHOInstance(ctx context.Context, instanceID int64) error

	// Statistics and events
	SaveHOEvent(ctx context.Context, event *HeroicOPEvent) error
	LoadHOEvents(ctx context.Context, instanceID int64) ([]HeroicOPEvent, error)
	GetHOStatistics(ctx context.Context, characterID int32) (*HeroicOPStatistics, error)

	// Utility operations
	GetNextStarterID(ctx context.Context) (int32, error)
	GetNextWheelID(ctx context.Context) (int32, error)
	GetNextInstanceID(ctx context.Context) (int64, error)
	EnsureHOTables(ctx context.Context) error
}

// HeroicOPEventHandler defines the interface for handling HO events
type HeroicOPEventHandler interface {
	// HO lifecycle events
	OnHOStarted(ho *HeroicOP, initiatorID int32)
	OnHOCompleted(ho *HeroicOP, completedBy int32, spellID int32)
	OnHOFailed(ho *HeroicOP, reason string)
	OnHOTimerExpired(ho *HeroicOP)

	// Progress events
	OnAbilityUsed(ho *HeroicOP, characterID int32, abilityIcon int16, success bool)
	OnWheelShifted(ho *HeroicOP, characterID int32, newWheelID int32)
	OnStarterMatched(ho *HeroicOP, starterID int32, characterID int32)
	OnStarterEliminated(ho *HeroicOP, starterID int32, characterID int32)

	// Phase transitions
	OnWheelPhaseStarted(ho *HeroicOP, wheelID int32, timeRemaining int32)
	OnProgressMade(ho *HeroicOP, characterID int32, progressPercent float32)
}

// SpellManager defines the interface for spell system integration
type SpellManager interface {
	// Get spell information
	GetSpellInfo(spellID int32) (*SpellInfo, error)
	GetSpellName(spellID int32) string
	GetSpellDescription(spellID int32) string

	// Cast spells
	CastSpell(casterID int32, spellID int32, targets []int32) error
	IsSpellValid(spellID int32) bool
}

// ClientManager defines the interface for client communication
type ClientManager interface {
	// Send HO packets to clients
	SendHOUpdate(characterID int32, data *PacketData) error
	SendHOStart(characterID int32, ho *HeroicOP) error
	SendHOComplete(characterID int32, ho *HeroicOP, success bool) error
	SendHOTimer(characterID int32, timeRemaining int32, totalTime int32) error

	// Broadcast to multiple clients
	BroadcastHOUpdate(characterIDs []int32, data *PacketData) error
	BroadcastHOEvent(characterIDs []int32, eventType int, data string) error

	// Client validation
	IsClientConnected(characterID int32) bool
	GetClientVersion(characterID int32) int
}

// EncounterManager defines the interface for encounter system integration
type EncounterManager interface {
	// Get encounter information
	GetEncounterParticipants(encounterID int32) ([]int32, error)
	IsEncounterActive(encounterID int32) bool
	GetEncounterInfo(encounterID int32) (*EncounterInfo, error)

	// HO integration
	CanStartHO(encounterID int32, initiatorID int32) bool
	NotifyHOStarted(encounterID int32, instanceID int64)
	NotifyHOCompleted(encounterID int32, instanceID int64, success bool)
}

// PlayerManager defines the interface for player system integration
type PlayerManager interface {
	// Get player information
	GetPlayerInfo(characterID int32) (*PlayerInfo, error)
	GetPlayerClass(characterID int32) (int8, error)
	GetPlayerLevel(characterID int32) (int16, error)
	IsPlayerOnline(characterID int32) bool

	// Player abilities
	CanPlayerUseAbility(characterID int32, abilityIcon int16) bool
	GetPlayerAbilities(characterID int32) ([]int16, error)

	// Player state
	IsPlayerInCombat(characterID int32) bool
	GetPlayerEncounter(characterID int32) (int32, error)
}

// LogHandler defines the interface for logging operations
type LogHandler interface {
	LogDebug(system, format string, args ...interface{})
	LogInfo(system, format string, args ...interface{})
	LogWarning(system, format string, args ...interface{})
	LogError(system, format string, args ...interface{})
}

// TimerManager defines the interface for timer management
type TimerManager interface {
	// Timer operations
	StartTimer(instanceID int64, duration time.Duration, callback func()) error
	StopTimer(instanceID int64) error
	UpdateTimer(instanceID int64, newDuration time.Duration) error
	GetTimeRemaining(instanceID int64) (time.Duration, error)

	// Timer queries
	IsTimerActive(instanceID int64) bool
	GetActiveTimers() []int64
}

// CacheManager defines the interface for caching operations
type CacheManager interface {
	// Cache operations
	Set(key string, value interface{}, expiration time.Duration) error
	Get(key string) (interface{}, bool)
	Delete(key string) error
	Clear() error

	// Cache statistics
	GetHitRate() float64
	GetSize() int
	GetCapacity() int
}

// Additional integration interfaces

// EncounterInfo contains encounter details
type EncounterInfo struct {
	ID           int32     `json:"id"`
	Name         string    `json:"name"`
	Participants []int32   `json:"participants"`
	IsActive     bool      `json:"is_active"`
	StartTime    time.Time `json:"start_time"`
	Level        int16     `json:"level"`
}

// PlayerInfo contains player details needed for HO system
type PlayerInfo struct {
	CharacterID    int32  `json:"character_id"`
	CharacterName  string `json:"character_name"`
	AccountID      int32  `json:"account_id"`
	AdventureClass int8   `json:"adventure_class"`
	AdventureLevel int16  `json:"adventure_level"`
	Zone           string `json:"zone"`
	IsOnline       bool   `json:"is_online"`
	InCombat       bool   `json:"in_combat"`
	EncounterID    int32  `json:"encounter_id"`
}

// Adapter interfaces for integration with existing systems

// HeroicOPAware defines interface for entities that can participate in HOs
type HeroicOPAware interface {
	GetCharacterID() int32
	GetClass() int8
	GetLevel() int16
	CanParticipateInHO() bool
	GetCurrentEncounter() int32
}

// EntityHOAdapter adapts entity system for HO integration
type EntityHOAdapter struct {
	entity HeroicOPAware
}

// PacketBuilder defines interface for building HO packets
type PacketBuilder interface {
	BuildHOStartPacket(ho *HeroicOP) ([]byte, error)
	BuildHOUpdatePacket(ho *HeroicOP) ([]byte, error)
	BuildHOCompletePacket(ho *HeroicOP, success bool) ([]byte, error)
	BuildHOTimerPacket(timeRemaining, totalTime int32) ([]byte, error)
}

// StatisticsCollector defines interface for collecting HO statistics
type StatisticsCollector interface {
	RecordHOStarted(instanceID int64, starterID int32, characterID int32)
	RecordHOCompleted(instanceID int64, success bool, completionTime time.Duration)
	RecordAbilityUsed(instanceID int64, characterID int32, abilityIcon int16)
	RecordShiftUsed(instanceID int64, characterID int32)
	GetStatistics() *HeroicOPStatistics
	Reset()
}

// ConfigManager defines interface for configuration management
type ConfigManager interface {
	GetHOConfig() *HeroicOPConfig
	UpdateHOConfig(config *HeroicOPConfig) error
	GetConfigValue(key string) interface{}
	SetConfigValue(key string, value interface{}) error
}
