package engine

import (
	"golang.org/x/net/html"
)

// ProcessMode defines how the engine should process content
type ProcessMode int

const (
	// Enhancement mode: Parse + Generate IDs + Inject content + Add editor assets
	Enhancement ProcessMode = iota
	// IDGeneration mode: Parse + Generate IDs only (for API)
	IDGeneration
	// ContentInjection mode: Parse + Generate IDs + Inject content only
	ContentInjection
)

// ContentInput represents input to the content engine
type ContentInput struct {
	HTML     []byte      // Raw HTML or markup
	FilePath string      // File context (e.g., "index.html")
	SiteID   string      // Site identifier
	Mode     ProcessMode // Processing mode
}

// ContentResult represents the result of content processing
type ContentResult struct {
	Document     *html.Node         // Processed HTML document
	Elements     []ProcessedElement // All processed elements
	GeneratedIDs map[string]string  // Map of element positions to generated IDs
}

// ProcessedElement represents an element that has been processed
type ProcessedElement struct {
	Node      *html.Node // HTML node
	ID        string     // Generated content ID
	Content   string     // Injected content (if any)
	Generated bool       // Whether ID was generated (vs existing)
	Tag       string     // Element tag name
	Classes   []string   // Element CSS classes
}

// ContentClient interface for accessing content data
// This will be implemented by database clients, HTTP clients, and mock clients
type ContentClient interface {
	GetContent(siteID, contentID string) (*ContentItem, error)
	GetBulkContent(siteID string, contentIDs []string) (map[string]ContentItem, error)
	GetAllContent(siteID string) (map[string]ContentItem, error)
	CreateContent(siteID, contentID, htmlContent, originalTemplate, lastEditedBy string) (*ContentItem, error)

	// Collection operations
	GetCollection(siteID, collectionID string) (*CollectionItem, error)
	CreateCollection(siteID, collectionID, containerHTML, lastEditedBy string) (*CollectionItem, error)
	GetCollectionItems(siteID, collectionID string) ([]CollectionItemWithTemplate, error)
	CreateCollectionTemplate(siteID, collectionID, name, htmlTemplate string, isDefault bool) (*CollectionTemplateItem, error)
}

// ContentItem represents a piece of content from the database
type ContentItem struct {
	ID               string `json:"id"`
	SiteID           string `json:"site_id"`
	HTMLContent      string `json:"html_content"`
	OriginalTemplate string `json:"original_template"`
	UpdatedAt        string `json:"updated_at"`
	LastEditedBy     string `json:"last_edited_by,omitempty"`
}

// ContentResponse represents the API response structure
type ContentResponse struct {
	Content []ContentItem `json:"content"`
	Error   string        `json:"error,omitempty"`
}

// CollectionItem represents a collection container from the database
type CollectionItem struct {
	ID            string `json:"id"`
	SiteID        string `json:"site_id"`
	ContainerHTML string `json:"container_html"`
	UpdatedAt     string `json:"updated_at"`
	LastEditedBy  string `json:"last_edited_by,omitempty"`
}

// CollectionTemplateItem represents a collection template from the database
type CollectionTemplateItem struct {
	TemplateID   int    `json:"template_id"`
	CollectionID string `json:"collection_id"`
	SiteID       string `json:"site_id"`
	Name         string `json:"name"`
	HTMLTemplate string `json:"html_template"`
	IsDefault    bool   `json:"is_default"`
}

// CollectionItemWithTemplate represents a collection item with its template information
type CollectionItemWithTemplate struct {
	ItemID       string `json:"item_id"`
	CollectionID string `json:"collection_id"`
	SiteID       string `json:"site_id"`
	TemplateID   int    `json:"template_id"`
	HTMLContent  string `json:"html_content"`
	Position     int    `json:"position"`
	UpdatedAt    string `json:"updated_at"`
	LastEditedBy string `json:"last_edited_by"`

	// Template information
	TemplateName string `json:"template_name"`
	HTMLTemplate string `json:"html_template"`
	IsDefault    bool   `json:"is_default"`
}
