package main

import (
	"context"
	"fmt"
	"os"
	"path/filepath"

	"github.com/spf13/cobra"

	"github.com/enmanuel/agents/internal/config"
	shellmatrix "github.com/enmanuel/agents/shell/matrix"
)

func avatarCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "avatar <agent-id> <image-path>",
		Short: "Upload an image and set it as the bot's Matrix avatar",
		Args:  cobra.ExactArgs(2),
		RunE: func(cmd *cobra.Command, args []string) error {
			agentID, imagePath := args[0], args[1]

			cfg, err := loadMatrixCfg(agentID)
			if err != nil {
				return err
			}

			client, err := shellmatrix.New(cfg.Matrix)
			if err != nil {
				return fmt.Errorf("matrix client: %w", err)
			}

			uri, err := client.SetAvatar(context.Background(), imagePath)
			if err != nil {
				return err
			}

			fmt.Printf("ok    %-20s  avatar → %s\n", agentID, uri)
			return nil
		},
	}
}

func uploadMediaCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "upload-media <agent-id> <image-path>",
		Short: "Upload an image to Matrix media repo (does NOT set it as avatar)",
		Args:  cobra.ExactArgs(2),
		RunE: func(cmd *cobra.Command, args []string) error {
			agentID, imagePath := args[0], args[1]

			cfg, err := loadMatrixCfg(agentID)
			if err != nil {
				return err
			}

			client, err := shellmatrix.New(cfg.Matrix)
			if err != nil {
				return fmt.Errorf("matrix client: %w", err)
			}

			uri, err := client.UploadMedia(context.Background(), imagePath)
			if err != nil {
				return err
			}

			fmt.Printf("ok    %-20s  uploaded → %s\n", agentID, uri)
			return nil
		},
	}
}

func setAvatarURLCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "set-avatar-url <agent-id> <mxc-uri>",
		Short: "Set the bot's avatar to an already-uploaded mxc:// URI",
		Args:  cobra.ExactArgs(2),
		RunE: func(cmd *cobra.Command, args []string) error {
			agentID, mxcURI := args[0], args[1]

			cfg, err := loadMatrixCfg(agentID)
			if err != nil {
				return err
			}

			client, err := shellmatrix.New(cfg.Matrix)
			if err != nil {
				return fmt.Errorf("matrix client: %w", err)
			}

			if err := client.SetAvatarURL(context.Background(), mxcURI); err != nil {
				return err
			}

			fmt.Printf("ok    %-20s  avatar-url → %s\n", agentID, mxcURI)
			return nil
		},
	}
}

func displaynameCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "displayname <agent-id> [name]",
		Short: "Set the bot's Matrix display name (defaults to agent.name in config)",
		Args:  cobra.RangeArgs(1, 2),
		RunE: func(cmd *cobra.Command, args []string) error {
			agentID := args[0]

			cfg, err := loadMatrixCfg(agentID)
			if err != nil {
				return err
			}

			name := cfg.Agent.Name
			if len(args) == 2 {
				name = args[1]
			}
			if name == "" {
				return fmt.Errorf("no name provided and agent.name is empty in config")
			}

			client, err := shellmatrix.New(cfg.Matrix)
			if err != nil {
				return fmt.Errorf("matrix client: %w", err)
			}

			if err := client.SetDisplayName(context.Background(), name); err != nil {
				return err
			}

			fmt.Printf("ok    %-20s  displayname → %q\n", agentID, name)
			return nil
		},
	}
}

// loadMatrixCfg locates the config for the given agent ID and returns it with
// Matrix-relevant env vars expanded. Does not run full LLM/tool validation so
// the command works even when OPENAI_API_KEY etc. are not set.
func loadMatrixCfg(agentID string) (*config.AgentConfig, error) {
	matches, err := filepath.Glob(agentsGlob)
	if err != nil {
		return nil, err
	}

	for _, path := range matches {
		cfg, err := config.LoadMeta(path)
		if err != nil {
			continue
		}
		if cfg.Agent.ID != agentID {
			continue
		}

		// Expand only the fields the Matrix client needs.
		cfg.Matrix.Homeserver = os.ExpandEnv(cfg.Matrix.Homeserver)
		cfg.Matrix.UserID = os.ExpandEnv(cfg.Matrix.UserID)
		return cfg, nil
	}

	return nil, fmt.Errorf("agent %q not found under agents/*/config.yaml", agentID)
}
