package ports

import (
	"context"

	"github.com/nexuscrm/shared/pkg/models"
)

// UIAPIService defines the interface for Salesforce UI API operations.
// Provides aggregated metadata, layouts, and record data for rich UI rendering.
type UIAPIService interface {
	// GetRecordUI retrieves aggregated data for rendering records in the UI
	// Combines record data, object info, and layouts in a single response
	GetRecordUI(ctx context.Context, recordIDs []string, typeHint string, user *models.UserSession) (*RecordUIResult, error)

	// GetObjectInfo retrieves metadata for a specific object type
	GetObjectInfo(ctx context.Context, objectType string, user *models.UserSession) (*ObjectInfoData, error)

	// GetLayout retrieves the page layout for an object and user profile
	GetLayout(ctx context.Context, objectType string, user *models.UserSession) (*LayoutData, error)

	// ResolveObjectType determines the object type from a record ID
	ResolveObjectType(ctx context.Context, id string, hintType string, user *models.UserSession) (string, error)

	// Close gracefully shuts down caches and background workers
	Close()
}

// RecordUIResult represents the aggregated UI response for record rendering
type RecordUIResult struct {
	Records     map[string]RecordData            `json:"records"`
	Layouts     map[string]map[string]LayoutData `json:"layouts"`
	ObjectInfos map[string]ObjectInfoData        `json:"objectInfos"`
}

// RecordData represents a single record's data for UI display
type RecordData struct {
	ApiName string                    `json:"apiName"`
	Id      string                    `json:"id"`
	Fields  map[string]FieldValueData `json:"fields"`
}

// FieldValueData represents a field's value with display formatting
type FieldValueData struct {
	DisplayValue string      `json:"displayValue"`
	Value        interface{} `json:"value"`
}

// ObjectInfoData represents object metadata for UI display
type ObjectInfoData struct {
	ApiName     string                   `json:"apiName"`
	Label       string                   `json:"label"`
	LabelPlural string                   `json:"labelPlural"`
	KeyPrefix   string                   `json:"keyPrefix"`
	Queryable   bool                     `json:"queryable"`
	Createable  bool                     `json:"createable"`
	Updateable  bool                     `json:"updateable"`
	Deletable   bool                     `json:"deletable"`
	Fields      map[string]FieldInfoData `json:"fields"`
}

// FieldInfoData represents field metadata for UI display
type FieldInfoData struct {
	ApiName    string `json:"apiName"`
	Label      string `json:"label"`
	DataType   string `json:"dataType"`
	Required   bool   `json:"required"`
	Createable bool   `json:"createable"`
	Updateable bool   `json:"updateable"`
}

// LayoutData represents a page layout for UI rendering
type LayoutData struct {
	Id       string              `json:"id"`
	Mode     string              `json:"mode"`
	Sections []LayoutSectionData `json:"sections"`
}

// LayoutSectionData represents a section within a layout
type LayoutSectionData struct {
	Id         string          `json:"id"`
	Heading    string          `json:"heading"`
	Columns    int             `json:"columns"`
	UseHeading bool            `json:"useHeading"`
	LayoutRows []LayoutRowData `json:"layoutRows"`
}

// LayoutRowData represents a row within a layout section
type LayoutRowData struct {
	LayoutItems []LayoutItemData `json:"layoutItems"`
}

// LayoutItemData represents an item within a layout row
type LayoutItemData struct {
	Label            string                `json:"label"`
	Required         bool                  `json:"required"`
	LayoutComponents []LayoutComponentData `json:"layoutComponents"`
}

// LayoutComponentData represents a component within a layout item
type LayoutComponentData struct {
	ComponentType string `json:"componentType"`
	ApiName       string `json:"apiName"`
}
