package cmd

import (
	"fmt"

	gogitea "code.gitea.io/sdk/gitea"
	"github.com/spf13/cobra"
)

var listCmd = &cobra.Command{
	Use:   "list",
	Short: "List your Gitea repos",
	RunE: func(cmd *cobra.Command, args []string) error {
		cfg, err := loadConfig()
		if err != nil {
			return err
		}

		client, err := gogitea.NewClient(cfg.URL, gogitea.SetToken(cfg.Token))
		if err != nil {
			return fmt.Errorf("connecting to Gitea: %w", err)
		}

		repos, _, err := client.ListMyRepos(gogitea.ListReposOptions{
			ListOptions: gogitea.ListOptions{PageSize: 50},
		})
		if err != nil {
			return fmt.Errorf("listing repos: %w", err)
		}

		if len(repos) == 0 {
			fmt.Println("No repos found.")
			return nil
		}

		fmt.Printf("%-30s  %-8s  %s\n", "NAME", "PRIVATE", "URL")
		fmt.Printf("%-30s  %-8s  %s\n", "----", "-------", "---")
		for _, r := range repos {
			priv := "public"
			if r.Private {
				priv = "private"
			}
			fmt.Printf("%-30s  %-8s  %s\n", r.Name, priv, r.HTMLURL)
		}
		return nil
	},
}

func init() {
	rootCmd.AddCommand(listCmd)
}
