// SPDX-FileCopyrightText: Copyright 2025 Stacklok, Inc.
// SPDX-License-Identifier: Apache-2.0

// Package groups provides functionality for managing logical groupings of MCP servers.
// It includes types and interfaces for creating, retrieving, listing, and deleting groups.
package groups

import (
	"context"
	"encoding/json"
	"os"
)

// DefaultGroup is the name of the default group for workloads
const DefaultGroup = "default"

// Group represents a logical grouping of MCP servers.
type Group struct {
	Name              string   `json:"name"`
	RegisteredClients []string `json:"registered_clients"`
	Skills            []string `json:"skills,omitempty"`
}

// WriteJSON serializes the Group to JSON and writes it to the provided writer
func (g *Group) WriteJSON(w *os.File) error {
	encoder := json.NewEncoder(w)
	encoder.SetIndent("", "  ")
	return encoder.Encode(g)
}

// Manager defines the interface for managing groups of MCP servers.
// It provides methods for creating, retrieving, listing, and deleting groups.
//
//go:generate mockgen -destination=mocks/mock_manager.go -package=mocks -source=group.go Manager
type Manager interface {
	// Create creates a new group with the specified name.
	// Returns an error if a group with the same name already exists.
	Create(ctx context.Context, name string) error

	// Get retrieves a group by name.
	// Returns an error if the group does not exist.
	Get(ctx context.Context, name string) (*Group, error)

	// List returns all groups.
	List(ctx context.Context) ([]*Group, error)

	// Delete removes a group by name.
	// Returns an error if the group does not exist.
	Delete(ctx context.Context, name string) error

	// Exists checks if a group with the specified name exists.
	Exists(ctx context.Context, name string) (bool, error)

	// RegisterClients registers multiple clients with multiple groups.
	RegisterClients(ctx context.Context, groupNames []string, clientNames []string) error

	// UnregisterClients removes multiple clients from multiple groups.
	UnregisterClients(ctx context.Context, groupNames []string, clientNames []string) error

	// Update persists changes to an existing group.
	// The group must already exist; returns an error if it does not.
	Update(ctx context.Context, group *Group) error
}
