// Mephisto farming routine for D2R.
//
// Classic MF run: Create game → WP to Durance 2 → Teleport to Durance 3 →
// Moat trick Mephisto → Loot → TP to town → Stash → Exit → Repeat
package mephisto

import (
	"context"
	"fmt"
	"sync"

	"git.cloonar.com/openclawd/iso-bot/pkg/plugin"
)

// Phase represents the current phase of a Mephisto run.
type Phase string

const (
	PhaseCreateGame  Phase = "create_game"
	PhaseTeleport    Phase = "teleport_to_durance"
	PhaseFindBoss    Phase = "find_mephisto"
	PhaseKill        Phase = "kill"
	PhaseLoot        Phase = "loot"
	PhaseTownPortal  Phase = "town_portal"
	PhaseStash       Phase = "stash"
	PhaseExitGame    Phase = "exit_game"
)

// Routine implements plugin.Routine for Mephisto runs.
type Routine struct {
	mu       sync.RWMutex
	services plugin.EngineServices
	phase    Phase
	runCount int
}

// New creates a Mephisto routine.
func New(services plugin.EngineServices) *Routine {
	return &Routine{services: services}
}

// Name returns the routine name.
func (r *Routine) Name() string { return "Mephisto" }

// Phase returns current phase for status display.
func (r *Routine) Phase() string {
	r.mu.RLock()
	defer r.mu.RUnlock()
	return string(r.phase)
}

// Run executes one Mephisto run.
func (r *Routine) Run(ctx context.Context) error {
	phases := []struct {
		phase   Phase
		handler func(ctx context.Context) error
	}{
		{PhaseCreateGame, r.createGame},
		{PhaseTeleport, r.teleportToDurance},
		{PhaseFindBoss, r.findMephisto},
		{PhaseKill, r.killMephisto},
		{PhaseLoot, r.lootItems},
		{PhaseTownPortal, r.townPortal},
		{PhaseStash, r.stashItems},
		{PhaseExitGame, r.exitGame},
	}

	for _, p := range phases {
		select {
		case <-ctx.Done():
			return ctx.Err()
		default:
		}

		r.mu.Lock()
		r.phase = p.phase
		r.mu.Unlock()

		r.services.Log("info", fmt.Sprintf("Phase: %s", p.phase))

		if err := p.handler(ctx); err != nil {
			return fmt.Errorf("phase %s failed: %w", p.phase, err)
		}

		r.services.Wait()
	}

	r.mu.Lock()
	r.runCount++
	r.mu.Unlock()

	return nil
}

func (r *Routine) createGame(ctx context.Context) error {
	// TODO: Navigate lobby → create game with randomized name
	return nil
}

func (r *Routine) teleportToDurance(ctx context.Context) error {
	// TODO: Open WP → Durance of Hate 2 → teleport to level 3 entrance
	return nil
}

func (r *Routine) findMephisto(ctx context.Context) error {
	// TODO: Teleport around Durance 3 to find Mephisto
	// Position for moat trick (safe spot across the moat)
	return nil
}

func (r *Routine) killMephisto(ctx context.Context) error {
	// TODO: Cast offensive spells from moat trick position
	// Monitor health, use potions if needed
	return nil
}

func (r *Routine) lootItems(ctx context.Context) error {
	// TODO: Teleport to body, detect items, pick up per loot filter
	return nil
}

func (r *Routine) townPortal(ctx context.Context) error {
	// TODO: Cast TP, click portal to go to town
	return nil
}

func (r *Routine) stashItems(ctx context.Context) error {
	// TODO: If inventory has items worth stashing, open stash and transfer
	return nil
}

func (r *Routine) exitGame(ctx context.Context) error {
	// TODO: ESC → Save & Exit
	return nil
}
