package services

import (
	"fmt"

	"git.linuxforward.com/byop/byop-engine/dbstore"
	"git.linuxforward.com/byop/byop-engine/models"
)

type ClientService struct {
	store *dbstore.ClientStore
}

// NewClientService creates a new ClientService
func NewClientService(store *dbstore.ClientStore) *ClientService {
	return &ClientService{
		store: store,
	}
}

// CreateClient creates a new client
func (s *ClientService) CreateClient(client *models.Client) error {
	if client.Name == "" {
		return fmt.Errorf("client name is required")
	}
	return s.store.Create(client)
}

// GetClient retrieves a client by ID
func (s *ClientService) GetClient(id string) (*models.Client, error) {
	client, err := s.store.GetByID(id)
	if err != nil {
		return nil, fmt.Errorf("failed to retrieve client: %w", err)
	}
	return client, nil
}

// UpdateClient updates an existing client
func (s *ClientService) UpdateClient(client *models.Client) error {
	if client.ID == "" {
		return fmt.Errorf("client ID is required for update")
	}
	return s.store.Update(client)
}

// DeleteClient deletes a client by ID
func (s *ClientService) DeleteClient(id string) error {
	return s.store.Delete(id)
}

// ListClients retrieves all clients with optional filtering
func (s *ClientService) ListClients(filter map[string]interface{}) ([]*models.Client, error) {
	return s.store.List(filter)
}
