package models

import "encoding/xml"

// DCTX File Format Models
//
// This file defines the data structures for parsing and generating DCTX
// (Data Dictionary) XML files, which are used by Clarion development tools
// for database schema definitions.

// DCTXDictionary represents the root element of a DCTX file.
type DCTXDictionary struct {
	XMLName   xml.Name       `xml:"Dictionary"`
	Name      string         `xml:"Name,attr"`
	Version   string         `xml:"Version,attr"`
	Tables    []DCTXTable    `xml:"Table"`
	Relations []DCTXRelation `xml:"Relation,omitempty"`
}

// DCTXTable represents a table definition in DCTX format.
type DCTXTable struct {
	Guid        string       `xml:"Guid,attr"`
	Name        string       `xml:"Name,attr"`
	Prefix      string       `xml:"Prefix,attr"`
	Driver      string       `xml:"Driver,attr,omitempty"`
	Owner       string       `xml:"Owner,attr,omitempty"`
	Path        string       `xml:"Path,attr,omitempty"`
	Description string       `xml:"Description,attr,omitempty"`
	Fields      []DCTXField  `xml:"Field"`
	Keys        []DCTXKey    `xml:"Key,omitempty"`
	Options     []DCTXOption `xml:"Option,omitempty"`
}

// DCTXField represents a field/column definition in DCTX format.
// Fields can be nested for GROUP structures.
type DCTXField struct {
	Guid       string       `xml:"Guid,attr"`
	Name       string       `xml:"Name,attr"`
	DataType   string       `xml:"DataType,attr"`
	Size       int          `xml:"Size,attr,omitempty"`
	NoPopulate bool         `xml:"NoPopulate,attr,omitempty"`
	Thread     bool         `xml:"Thread,attr,omitempty"`
	Fields     []DCTXField  `xml:"Field,omitempty"` // For GROUP fields (nested structures)
	Options    []DCTXOption `xml:"Option,omitempty"`
}

// DCTXKey represents an index or key definition in DCTX format.
type DCTXKey struct {
	Guid        string          `xml:"Guid,attr"`
	Name        string          `xml:"Name,attr"`
	KeyType     string          `xml:"KeyType,attr,omitempty"`
	Primary     bool            `xml:"Primary,attr,omitempty"`
	Unique      bool            `xml:"Unique,attr,omitempty"`
	Order       int             `xml:"Order,attr,omitempty"`
	Description string          `xml:"Description,attr,omitempty"`
	Components  []DCTXComponent `xml:"Component"`
}

// DCTXComponent represents a component of a key, referencing a field in the index.
type DCTXComponent struct {
	Guid    string `xml:"Guid,attr"`
	FieldId string `xml:"FieldId,attr,omitempty"`
	Order   int    `xml:"Order,attr"`
	Ascend  bool   `xml:"Ascend,attr,omitempty"`
}

// DCTXOption represents a property option in DCTX format for metadata storage.
type DCTXOption struct {
	Property      string `xml:"Property,attr"`
	PropertyType  string `xml:"PropertyType,attr,omitempty"`
	PropertyValue string `xml:"PropertyValue,attr"`
}

// DCTXRelation represents a relationship/foreign key in DCTX format.
type DCTXRelation struct {
	Guid            string             `xml:"Guid,attr"`
	PrimaryTable    string             `xml:"PrimaryTable,attr"`
	ForeignTable    string             `xml:"ForeignTable,attr"`
	PrimaryKey      string             `xml:"PrimaryKey,attr,omitempty"`
	ForeignKey      string             `xml:"ForeignKey,attr,omitempty"`
	Delete          string             `xml:"Delete,attr,omitempty"`
	Update          string             `xml:"Update,attr,omitempty"`
	ForeignMappings []DCTXFieldMapping `xml:"ForeignMapping,omitempty"`
	PrimaryMappings []DCTXFieldMapping `xml:"PrimaryMapping,omitempty"`
}

// DCTXFieldMapping represents a field mapping in a relation for multi-column foreign keys.
type DCTXFieldMapping struct {
	Guid  string `xml:"Guid,attr"`
	Field string `xml:"Field,attr"`
}
