// Slash-command execution: builds SlashCommandEnv and runs commands
// through input.NewSlashCommandController.
package app

import (
	"context"

	tea "github.com/charmbracelet/bubbletea"

	"github.com/genai-io/san/internal/app/input"
)

func (m *model) slashCommandEnv() input.SlashCommandEnv {
	return input.SlashCommandEnv{
		// UI state
		Input:        &m.userInput,
		Conversation: &m.conv.ConversationModel,
		Tool:         &m.conv.Tool,
		Width:        m.env.Width,
		Height:       m.env.Height,
		Cwd:          m.env.CWD,
		InputTokens:  m.env.InputTokens,

		// Services
		Setting: m.services.Setting,
		LLM:     m.services.LLM,
		Session: m.services.Session,
		Command: m.services.Command,
		Skill:   m.services.Skill,
		Plugin:  m.services.Plugin,
		MCP:     m.services.MCP,
		Tracker: m.services.Tracker,
		Cron:    m.services.Cron,
		ToolSvc: m.services.Tool,

		// Env callbacks
		GetThinkingEffort: func() string { return m.env.EffectiveThinkingEffort() },
		SetThinkingEffort: m.env.SetThinkingEffort,
		ResetTokens:       m.env.ResetTokens,

		// Model actions
		CommitMessages:          m.CommitMessages,
		SubmitToAgent:           m.SubmitToAgent,
		HandleSkillInvocation:   m.HandleSkillInvocation,
		StartExternalEditor:     m.StartExternalEditor,
		ReloadPluginBackedState: m.ReloadPluginBackedState,
		PersistSession:          m.PersistSession,
		InitTaskStorage:         m.InitTaskStorage,
		ReconfigureAgentTool:    m.ReconfigureAgentTool,
		StopAgentSession:        m.StopAgentSession,
		FireSessionEnd:          m.FireSessionEnd,
		BuildCompactRequest:     m.BuildCompactRequest,
		SpinnerTickCmd:          m.SpinnerTickCmd,
		ResetCronQueue:          m.ResetCronQueue,
		ForkSession:             m.forkSession,
		RunSelfLearnDemo:        m.runSelfLearnDemo,
	}
}

func (m *model) executeCommand(ctx context.Context, inputText string) (string, tea.Cmd, bool) {
	return input.NewSlashCommandController(m.slashCommandEnv()).Execute(ctx, inputText)
}
