package internal

import (
	"context"

	"github.com/gotd/td/tg"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

var (
	channelID    int64
	channelAlias string
)

func ExportCmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "export",
		Short: "export channels",
		RunE: wrapE(func(ctx context.Context) error {
			if channelID == 0 && channelAlias == "" {
				return errors.New("channel id or alias is required")
			}

			var channel *tg.Channel
			var err error
			if channelAlias != "" {
				channel, err = client.ChannelInfoByAlias(ctx, channelAlias)
				if err != nil {
					return err
				}
			} else {
				channel, err = client.ChannelInfoByID(ctx, channelID)
				if err != nil {
					return err
				}
			}
			return client.Channel(ctx, channel, 0)
		}),
	}

	cmd.Flags().Int64Var(&channelID, "channel", 0, "channel id")
	cmd.Flags().StringVar(&channelAlias, "alias", "", "channel alias")

	return cmd
}
