// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package cmd

import (
	stdctx "context"

	"gitea.dev/tea/cmd/organizations"
	"gitea.dev/tea/modules/context"
	"gitea.dev/tea/modules/print"

	"github.com/urfave/cli/v3"
)

// CmdOrgs represents handle organization
var CmdOrgs = cli.Command{
	Name:        "organizations",
	Aliases:     []string{"organization", "org"},
	Category:    catEntities,
	Usage:       "List, create, delete organizations",
	Description: "Show organization details",
	ArgsUsage:   "[<organization>]",
	Action:      runOrganizations,
	Commands: []*cli.Command{
		&organizations.CmdOrganizationList,
		&organizations.CmdOrganizationCreate,
		&organizations.CmdOrganizationDelete,
	},
	Flags: organizations.CmdOrganizationList.Flags,
}

func runOrganizations(ctx stdctx.Context, cmd *cli.Command) error {
	teaCtx, err := context.InitCommand(cmd)
	if err != nil {
		return err
	}
	if teaCtx.Args().Len() == 1 {
		org, _, err := teaCtx.Login.Client().Organizations.GetOrg(ctx, teaCtx.Args().First())
		if err != nil {
			return err
		}

		print.OrganizationDetails(org)
		return nil
	}
	return organizations.RunOrganizationList(ctx, cmd)
}
