package oxcmd

import (
	"context"
	"fmt"

	"github.com/opensvc/om3/v3/core/client"
	"github.com/opensvc/om3/v3/core/commoncmd"
	"github.com/opensvc/om3/v3/core/objectselector"
	"github.com/opensvc/om3/v3/daemon/api"
)

type (
	CmdObjectDisable struct {
		OptsGlobal
		commoncmd.OptsLock
		commoncmd.OptsResourceSelector
	}
)

func (t *CmdObjectDisable) Run(kind string) error {
	mergedSelector := commoncmd.MergeSelector("", t.ObjectSelector, kind, "")
	c, err := client.New()
	if err != nil {
		return err
	}
	sel := objectselector.New(mergedSelector, objectselector.WithClient(c))
	paths, err := sel.MustExpand()
	if err != nil {
		return err
	}
	for _, p := range paths {
		params := api.PostSvcDisableParams{}
		params.Rid = &t.RID
		params.Subset = &t.Subset
		params.Tag = &t.Tag
		response, err := c.PostSvcDisableWithResponse(context.Background(), p.Namespace, p.Name, &params)
		if err != nil {
			return err
		}
		switch response.StatusCode() {
		case 204:
		case 400:
			return fmt.Errorf("%s: %s", p, *response.JSON400)
		case 401:
			return fmt.Errorf("%s: %s", p, *response.JSON401)
		case 403:
			return fmt.Errorf("%s: %s", p, *response.JSON403)
		case 500:
			return fmt.Errorf("%s: %s", p, *response.JSON500)
		default:
			return fmt.Errorf("%s: unexpected response: %s", p, response.Status())
		}
	}
	return nil
}
