package ports

import (
	"context"

	"github.com/nexuscrm/backend/internal/features/composite"
)

// CompositeService defines the interface for composite/batch API operations.
// This allows handlers to depend on an abstraction rather than concrete implementation.
type CompositeService interface {
	// ExecuteBatch processes multiple subrequests in sequence or parallel
	// Each subrequest is executed and results are aggregated
	ExecuteBatch(ctx context.Context, req composite.BatchRequest) (*composite.BatchResponse, error)

	// ExecuteCollection performs bulk CRUD on multiple records of the same type
	// Supports insert, update, and delete operations
	ExecuteCollection(ctx context.Context, method string, objectType string, records []map[string]interface{}, allOrNone bool) (*CollectionResult, error)

	// CreateTree creates a parent record with nested child records in a single request
	// Resolves foreign key references automatically
	CreateTree(ctx context.Context, objectType string, root map[string]interface{}, children map[string][]map[string]interface{}) (*TreeResult, error)
}

// CollectionResult represents the result of a bulk collection operation
type CollectionResult struct {
	Records   []CollectionRecord `json:"records"`
	HasErrors bool               `json:"hasErrors"`
}

// CollectionRecord represents a single record result in a collection operation
type CollectionRecord struct {
	ID      string   `json:"id,omitempty"`
	Success bool     `json:"success"`
	Errors  []string `json:"errors,omitempty"`
	Created bool     `json:"created,omitempty"`
}

// TreeResult represents the result of a tree creation operation
type TreeResult struct {
	ID       string                  `json:"id"`
	Success  bool                    `json:"success"`
	Errors   []string                `json:"errors,omitempty"`
	Children map[string][]TreeResult `json:"children,omitempty"`
}
