package handler

import (
	"errors"
	"net/http"
	"os"
	"strings"

	"galaxy/model/order"
	"galaxy/model/report"
	"galaxy/model/rest"

	e "galaxy/error"

	"galaxy/game/internal/controller"
	"galaxy/game/internal/model/game"

	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"github.com/google/uuid"
)

type CommandExecutor interface {
	GenerateGame([]string) (rest.StateResponse, error)
	GenerateTurn() (rest.StateResponse, error)
	GameState() (rest.StateResponse, error)
	BanishRace(string) error
	LoadReport(actor string, turn uint) (*report.Report, error)
	// Execute is reserved for future use; any API request for orders should use ValidateOrder
	Execute(cmd ...Command) error
	ValidateOrder(actor string, cmd ...order.DecodableCommand) (*order.UserGamesOrder, error)
	FetchOrder(actor string, turn uint) (*order.UserGamesOrder, bool, error)
	FetchBattle(turn uint, ID uuid.UUID) (*report.BattleReport, bool, error)
}

type Command func(controller.Ctrl) error

type executor struct {
	cfg controller.Configurer
}

// ResolveStoragePath returns the engine storage path resolved from
// STORAGE_PATH (preferred, historical name) or GAME_STATE_PATH (canonical
// name written by Runtime Manager). It returns an error when neither
// variable is set; callers are expected to fail fast at startup.
func ResolveStoragePath() (string, error) {
	if v := strings.TrimSpace(os.Getenv("STORAGE_PATH")); v != "" {
		return v, nil
	}
	if v := strings.TrimSpace(os.Getenv("GAME_STATE_PATH")); v != "" {
		return v, nil
	}
	return "", errors.New("storage path is not set: provide STORAGE_PATH or GAME_STATE_PATH")
}

func initConfig() controller.Configurer {
	return func(p *controller.Param) {
		// Validated once at startup by ResolveStoragePath; the error
		// is dropped here to keep the Configurer signature simple.
		p.StoragePath, _ = ResolveStoragePath()
	}
}

func NewDefaultExecutor() CommandExecutor {
	return NewDefaultConfigExecutor(initConfig())
}

func NewDefaultConfigExecutor(configurer controller.Configurer) CommandExecutor {
	return &executor{cfg: configurer}
}

func (e *executor) Execute(cmd ...Command) error {
	return controller.ExecuteCommand(e.cfg, func(c controller.Ctrl) error {
		for i := range cmd {
			if err := cmd[i](c); err != nil {
				return err
			}
		}
		return nil
	})
}

func (e *executor) ValidateOrder(actor string, cmd ...order.DecodableCommand) (*order.UserGamesOrder, error) {
	return controller.ValidateOrder(e.cfg, actor, cmd...)
}

func (e *executor) FetchOrder(actor string, turn uint) (*order.UserGamesOrder, bool, error) {
	return controller.FetchOrder(e.cfg, actor, turn)
}

func (e *executor) FetchBattle(turn uint, ID uuid.UUID) (*report.BattleReport, bool, error) {
	return controller.FetchBattle(e.cfg, turn, ID)
}

func (e *executor) GenerateGame(races []string) (rest.StateResponse, error) {
	s, err := controller.GenerateGame(e.cfg, races)
	if err != nil {
		return rest.StateResponse{}, err
	}
	return stateResponse(s), nil
}

func (e *executor) GenerateTurn() (rest.StateResponse, error) {
	err := controller.GenerateTurn(e.cfg)
	if err != nil {
		return rest.StateResponse{}, err
	}
	return e.GameState()
}

func (e *executor) GameState() (rest.StateResponse, error) {
	s, err := controller.GameState(e.cfg)
	if err != nil {
		return rest.StateResponse{}, err
	}
	return stateResponse(s), nil
}

func (e *executor) BanishRace(raceName string) error {
	return controller.BanishRace(e.cfg, raceName)
}

func (e *executor) LoadReport(actor string, turn uint) (*report.Report, error) {
	return controller.LoadReport(e.cfg, actor, turn)
}

func stateResponse(s game.State) rest.StateResponse {
	result := &rest.StateResponse{
		ID:       s.ID,
		Turn:     s.Turn,
		Stage:    s.Stage,
		Finished: s.Finished,
		Players:  make([]rest.PlayerState, len(s.Players)),
	}
	for i := range s.Players {
		result.Players[i].ID = s.Players[i].ID
		result.Players[i].RaceName = s.Players[i].RaceName
		result.Players[i].Planets = s.Players[i].Planets
		result.Players[i].Population = s.Players[i].Population.F()
		result.Players[i].Extinct = s.Players[i].Extinct
	}
	return *result
}

func errorResponse(c *gin.Context, err error) bool {
	if err == nil {
		return false
	}

	if v, ok := err.(validator.ValidationErrors); ok {
		c.JSON(http.StatusBadRequest, gin.H{"error": v.Error()})
		return true
	}

	if ge, ok := errors.AsType[*e.GenericError](err); ok {
		switch ge.Code {
		case e.ErrGameNotInitialized:
			c.Status(http.StatusNotImplemented)
		default:
			c.JSON(http.StatusInternalServerError, gin.H{"generic_error": ge.Error(), "code": ge.Code})
		}
	} else {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
	}

	return true
}
