package constants

import "strings"

// SchemaFieldType represents the type of a field
// These use Salesforce-compatible lowercase naming for maximum SDK compatibility
type SchemaFieldType string

// Salesforce-compatible field types (lowercase as per Salesforce API conventions)
const (
	// Core types
	FieldTypeString    SchemaFieldType = "string"    // Text field
	FieldTypeDouble    SchemaFieldType = "double"    // Numeric field
	FieldTypeCurrency  SchemaFieldType = "currency"  // Money field
	FieldTypeDate      SchemaFieldType = "date"      // Date only
	FieldTypeDatetime  SchemaFieldType = "datetime"  // Date + time
	FieldTypeBoolean   SchemaFieldType = "boolean"   // Checkbox
	FieldTypePicklist  SchemaFieldType = "picklist"  // Dropdown
	FieldTypeEmail     SchemaFieldType = "email"     // Email address
	FieldTypePhone     SchemaFieldType = "phone"     // Phone number
	FieldTypeTextarea  SchemaFieldType = "textarea"  // Multi-line text
	FieldTypeUrl       SchemaFieldType = "url"       // Web URL
	FieldTypePercent   SchemaFieldType = "percent"   // Percentage
	FieldTypeReference SchemaFieldType = "reference" // Lookup relationship

	// Extended types
	FieldTypeMasterDetail    SchemaFieldType = "masterdetail"    // Parent-child relationship
	FieldTypeFormula         SchemaFieldType = "formula"         // Calculated field
	FieldTypeRollupSummary   SchemaFieldType = "rollupsummary"   // Aggregate field
	FieldTypeRichtextarea    SchemaFieldType = "richtextarea"    // Rich text / HTML
	FieldTypeAutonumber      SchemaFieldType = "autonumber"      // Auto-increment
	FieldTypeMultiPicklist   SchemaFieldType = "multipicklist"   // Multi-select
	FieldTypeEncryptedString SchemaFieldType = "encryptedstring" // Encrypted text
	FieldTypePassword        SchemaFieldType = "password"        // Password field
	FieldTypeJson            SchemaFieldType = "json"            // JSON data (custom extension)
)

// NormalizeFieldType normalizes field type input to lowercase Salesforce-compatible format.
// This is the canonical function for field type normalization.
func NormalizeFieldType(input string) SchemaFieldType {
	return SchemaFieldType(strings.ToLower(input))
}

// IsReferenceType checks if the field type represents a relationship
func IsReferenceType(t SchemaFieldType) bool {
	return t == FieldTypeReference || t == FieldTypeMasterDetail
}

// TableCategory defines the category of a table
const (
	TableCategoryStandard = "standard"
	TableCategoryCustom   = "custom"
	TableCategorySystem   = "system"
)

// SharingModel represents object-level sharing model
type SharingModel string

const (
	SharingModelPrivate              SharingModel = "Private"
	SharingModelPublicRead           SharingModel = "PublicRead"
	SharingModelPublicReadWrite      SharingModel = "PublicReadWrite"
	SharingModelControlledByParent   SharingModel = "ControlledByParent"
	SharingModelFolderSharing        SharingModel = "FolderSharing"
	SharingModelFolderContentSharing SharingModel = "FolderContentSharing" // Report/Dashboard: access derived from FolderId's folder visibility
)

// DeleteRule represents referential integrity rules
type DeleteRule string

const (
	DeleteRuleRestrict DeleteRule = "Restrict"
	DeleteRuleCascade  DeleteRule = "Cascade"
	DeleteRuleSetNull  DeleteRule = "SetNull"
)

// ObjectCategory defines the category of an object
type ObjectCategory string

const (
	ObjectCategoryCustom ObjectCategory = "Custom"
	ObjectCategorySystem ObjectCategory = "System"
)

// OutboxEventStatus represents the processing status of outbox events
type OutboxEventStatus string

const (
	OutboxStatusPending   OutboxEventStatus = "pending"
	OutboxStatusProcessed OutboxEventStatus = "processed"
	OutboxStatusFailed    OutboxEventStatus = "failed"
)

// ApproverType for Approval Processes
type ApproverType string

const (
	ApproverTypeSelf     ApproverType = "Self"
	ApproverTypeSpecific ApproverType = "Specific"
)

// Navigation Item Types (aligned with frontend NavigationItemType union)
const (
	NavigationTypeObject    = "object"
	NavigationTypePage      = "page"
	NavigationTypeWeb       = "web"
	NavigationTypeDashboard = "dashboard"
)

// List View Defaults
const (
	ListViewDefaultDeveloperName = "RecentlyViewed"
)

// UI Defaults
const (
	LayoutNameDefault            = "Default Layout"
	SectionLabelInformation      = "Information"
	SectionLabelSystemInfo       = "System Information"
	SectionLabelAddress          = "Address Information"
	SectionLabelDescription      = "Description"
	SectionLabelDetails          = "Additional Details"
)

// Analytics Operations
const (
	AnalyticsOperationGroupBy = "group_by"
	AnalyticsOperationCount   = "count"
	AnalyticsOperationSum     = "sum"
	AnalyticsOperationAvg     = "avg"
)

// FieldSource classifies the provenance of a _System_Field row.
// Introduced by issue #1582 RFC v1.5 PR A3. Single source of truth for every
// writer (pack applier, bootstrap schema, BackfillSource) and every reader
// (ComputeIntrinsic Layer 1d, AssertSourceIntegrity).
const (
	FieldSourceManaged       = "managed"        // declared by system_tables.json
	FieldSourceStandardSpec  = "standard_spec"  // declared by standard_objects_spec.json
	FieldSourceTenantSpec    = "tenant_spec"    // declared by tenant_objects_spec.json
	FieldSourceCustom        = "custom"         // admin-created via CreateField
	FieldSourceBootstrapSeed = "bootstrap_seed" // reserved for future bootstrap-seed path (no writer today)
	FieldSourceDiscovered    = "discovered"     // DEFAULT — physical column without a declaration
)

// KnownFieldSources enumerates every valid FieldSource value. AssertSourceIntegrity
// rejects rows whose Source is outside this set. Keep aligned with the constants above.
var KnownFieldSources = []string{
	FieldSourceManaged,
	FieldSourceStandardSpec,
	FieldSourceTenantSpec,
	FieldSourceCustom,
	FieldSourceBootstrapSeed,
	FieldSourceDiscovered,
}
