package admin

import (
	"context"
	"fmt"
	"strings"

	"github.com/tidbcloud/AgentTeam/internal/coredb"
)

type Options struct {
	RequireSystemAdminUser func(context.Context, string) error
}

type Store struct {
	db      coredb.DB
	options Options
}

func New(db coredb.DB, options Options) *Store {
	return &Store{db: db, options: options}
}

func (s *Store) Validate() error { return s.options.Validate() }

func (o Options) Validate() error {
	var missing []string
	if o.RequireSystemAdminUser == nil {
		missing = append(missing, "RequireSystemAdminUser")
	}
	if len(missing) > 0 {
		return fmt.Errorf("admin store: missing dependencies: %s", strings.Join(missing, ", "))
	}
	return nil
}

func (store *Store) requireSystemAdminUser(ctx context.Context, userID string) error {
	if store == nil || store.options.RequireSystemAdminUser == nil {
		return fmt.Errorf("admin store system admin authorization dependency is not configured")
	}
	return store.options.RequireSystemAdminUser(ctx, userID)
}
