package domain

import (
	"encoding/json"
	"time"
)

type Template struct {
	ID                 int64           `json:"id"`
	Name               string          `json:"name"`
	Description        string          `json:"description"`
	Locale             string          `json:"locale"`
	ThumbnailURL       string          `json:"thumbnailUrl"`
	TemplatePreviewURL string          `json:"templatePreviewUrl"`
	Type               string          `json:"type"`
	PaletteReady       bool            `json:"paletteReady"`
	RawJSON            json.RawMessage `json:"rawTemplateJson"`
	IsAITemplate       bool            `json:"isAiTemplate"`
	IsOnboarded        bool            `json:"isOnboarded"`
	ManifestStatus     string          `json:"manifestStatus"`
	LastDiscoveredAt   *time.Time      `json:"lastDiscoveredAt,omitempty"`
}

type TemplateManifest struct {
	ID                    string          `json:"id"`
	TemplateID            int64           `json:"templateId"`
	Version               int             `json:"version"`
	Source                string          `json:"source"`
	LanguageUsedDiscovery string          `json:"languageUsedForDiscovery"`
	DiscoveryPayloadJSON  json.RawMessage `json:"discoveryPayloadJson"`
	DiscoveryResponseJSON json.RawMessage `json:"discoveryResponseJson"`
	FlattenedManifestJSON json.RawMessage `json:"flattenedManifestJson"`
	IsActive              bool            `json:"isActive"`
	CreatedAt             time.Time       `json:"createdAt"`
	UpdatedAt             time.Time       `json:"updatedAt"`
}

type TemplateField struct {
	ID             string `json:"id"`
	TemplateID     int64  `json:"templateId"`
	ManifestID     string `json:"manifestId"`
	Section        string `json:"section"`
	WebsiteSection string `json:"websiteSection"`
	KeyName        string `json:"keyName"`
	Path           string `json:"path"`
	FieldKind      string `json:"fieldKind"`
	SampleValue    string `json:"sampleValue"`
	IsEnabled      bool   `json:"isEnabled"`
	IsRequiredByUs bool   `json:"isRequiredByUs"`
	DisplayLabel   string `json:"displayLabel"`
	DisplayOrder   int    `json:"displayOrder"`
	Notes          string `json:"notes"`
}

type SiteBuild struct {
	ID                string          `json:"id"`
	TemplateID        int64           `json:"templateId"`
	ManifestID        string          `json:"manifestId"`
	RequestName       string          `json:"requestName"`
	GlobalDataJSON    json.RawMessage `json:"globalDataJson"`
	AIDataJSON        json.RawMessage `json:"aiDataJson"`
	FinalSitesPayload json.RawMessage `json:"finalSitesPayloadJson"`
	QCJobID           *int64          `json:"qcJobId,omitempty"`
	QCSiteID          *int64          `json:"qcSiteId,omitempty"`
	QCStatus          string          `json:"qcStatus"`
	QCPreviewURL      string          `json:"qcPreviewUrl"`
	QCEditorURL       string          `json:"qcEditorUrl"`
	QCResultJSON      json.RawMessage `json:"qcResultJson"`
	QCErrorJSON       json.RawMessage `json:"qcErrorJson"`
	StartedAt         *time.Time      `json:"startedAt,omitempty"`
	FinishedAt        *time.Time      `json:"finishedAt,omitempty"`
}

type BuildDraft struct {
	ID                  string          `json:"id"`
	TemplateID          int64           `json:"templateId"`
	ManifestID          string          `json:"manifestId"`
	Source              string          `json:"source"`
	RequestName         string          `json:"requestName"`
	GlobalDataJSON      json.RawMessage `json:"globalDataJson"`
	FieldValuesJSON     json.RawMessage `json:"fieldValuesJson"`
	DraftContextJSON    json.RawMessage `json:"draftContextJson"`
	SuggestionStateJSON json.RawMessage `json:"suggestionStateJson"`
	Status              string          `json:"status"`
	Notes               string          `json:"notes"`
	CreatedAt           time.Time       `json:"createdAt"`
	UpdatedAt           time.Time       `json:"updatedAt"`
}

const (
	DraftSuggestionSourceLLM               = "llm"
	DraftSuggestionSourceQCLLM             = "qc-llm"
	DraftSuggestionSourceFallbackRuleBased = "fallback-rule-based"
	DraftSuggestionSourceRuleBased         = DraftSuggestionSourceFallbackRuleBased

	DraftSuggestionStatusSuggested = "suggested"
	DraftSuggestionStatusApplied   = "applied"
	DraftSuggestionStatusDismissed = "dismissed"
)

type DraftSuggestion struct {
	FieldPath   string    `json:"fieldPath"`
	Slot        string    `json:"slot,omitempty"`
	Value       string    `json:"value"`
	Reason      string    `json:"reason,omitempty"`
	Source      string    `json:"source,omitempty"`
	Status      string    `json:"status,omitempty"`
	GeneratedAt time.Time `json:"generatedAt,omitempty"`
	UpdatedAt   time.Time `json:"updatedAt,omitempty"`
}

type DraftSuggestionState struct {
	ByFieldPath map[string]DraftSuggestion `json:"byFieldPath,omitempty"`
	UpdatedAt   time.Time                  `json:"updatedAt,omitempty"`
}

type DraftStyleProfile struct {
	LocaleStyle        string `json:"localeStyle,omitempty"`
	MarketStyle        string `json:"marketStyle,omitempty"`
	AddressMode        string `json:"addressMode,omitempty"`
	ContentTone        string `json:"contentTone,omitempty"`
	PromptInstructions string `json:"promptInstructions,omitempty"`
}

type PromptBlockConfig struct {
	ID          string `json:"id"`
	Label       string `json:"label,omitempty"`
	Instruction string `json:"instruction,omitempty"`
	Enabled     bool   `json:"enabled"`
}

type DraftPromptConfig struct {
	Blocks []PromptBlockConfig `json:"blocks,omitempty"`
}

type DraftLLMContext struct {
	BusinessType   string            `json:"businessType,omitempty"`
	WebsiteURL     string            `json:"websiteUrl,omitempty"`
	WebsiteSummary string            `json:"websiteSummary,omitempty"`
	StyleProfile   DraftStyleProfile `json:"styleProfile,omitempty"`
	Prompt         DraftPromptConfig `json:"prompt,omitempty"`
}

type DraftContext struct {
	IntakeSource string          `json:"intakeSource,omitempty"`
	LLM          DraftLLMContext `json:"llm,omitempty"`
}

type AppSettings struct {
	QCBaseURL                string              `json:"qcBaseUrl"`
	QCBearerTokenEncrypted   string              `json:"qcBearerTokenEncrypted"`
	LanguageOutputMode       string              `json:"languageOutputMode"`
	JobPollIntervalSeconds   int                 `json:"jobPollIntervalSeconds"`
	JobPollTimeoutSeconds    int                 `json:"jobPollTimeoutSeconds"`
	LLMActiveProvider        string              `json:"llmActiveProvider,omitempty"`
	LLMActiveModel           string              `json:"llmActiveModel,omitempty"`
	LLMBaseURL               string              `json:"llmBaseUrl,omitempty"`
	LLMTemperature           float64             `json:"llmTemperature,omitempty"`
	LLMMaxTokens             int                 `json:"llmMaxTokens,omitempty"`
	OpenAIAPIKeyEncrypted    string              `json:"openAiApiKeyEncrypted,omitempty"`
	AnthropicAPIKeyEncrypted string              `json:"anthropicApiKeyEncrypted,omitempty"`
	GoogleAPIKeyEncrypted    string              `json:"googleApiKeyEncrypted,omitempty"`
	XAIAPIKeyEncrypted       string              `json:"xaiApiKeyEncrypted,omitempty"`
	OllamaAPIKeyEncrypted    string              `json:"ollamaApiKeyEncrypted,omitempty"`
	MasterPrompt             string              `json:"masterPrompt,omitempty"`
	PromptBlocks             []PromptBlockConfig `json:"promptBlocks,omitempty"`
}
