package ucca

// ============================================================================
// YAML-based Policy Engine — Types
// ============================================================================

// DefaultPolicyPath is the default location for the policy file
var DefaultPolicyPath = "policies/ucca_policy_v1.yaml"

// PolicyConfig represents the full YAML policy structure
type PolicyConfig struct {
	Policy             PolicyMetadata            `yaml:"policy"`
	Thresholds         Thresholds                `yaml:"thresholds"`
	Patterns           map[string]PatternDef     `yaml:"patterns"`
	Controls           map[string]ControlDef     `yaml:"controls"`
	Rules              []RuleDef                 `yaml:"rules"`
	ProblemSolutions   []ProblemSolutionDef      `yaml:"problem_solutions"`
	EscalationTriggers []EscalationTriggerDef    `yaml:"escalation_triggers"`
}

// PolicyMetadata contains policy header info
type PolicyMetadata struct {
	Name               string   `yaml:"name"`
	Version            string   `yaml:"version"`
	Jurisdiction       string   `yaml:"jurisdiction"`
	Basis              []string `yaml:"basis"`
	DefaultFeasibility string   `yaml:"default_feasibility"`
	DefaultRiskScore   int      `yaml:"default_risk_score"`
}

// Thresholds for risk scoring and escalation
type Thresholds struct {
	Risk       RiskThresholds `yaml:"risk"`
	Escalation []string       `yaml:"escalation"`
}

// RiskThresholds defines risk level boundaries
type RiskThresholds struct {
	Minimal      int `yaml:"minimal"`
	Low          int `yaml:"low"`
	Medium       int `yaml:"medium"`
	High         int `yaml:"high"`
	Unacceptable int `yaml:"unacceptable"`
}

// PatternDef represents an architecture pattern from YAML
type PatternDef struct {
	ID            string `yaml:"id"`
	Title         string `yaml:"title"`
	Description   string `yaml:"description"`
	Benefit       string `yaml:"benefit"`
	Effort        string `yaml:"effort"`
	RiskReduction int    `yaml:"risk_reduction"`
}

// ControlDef represents a required control from YAML
type ControlDef struct {
	ID          string `yaml:"id"`
	Title       string `yaml:"title"`
	Description string `yaml:"description"`
	GDPRRef     string `yaml:"gdpr_ref"`
	Effort      string `yaml:"effort"`
}

// RuleDef represents a single rule from YAML
type RuleDef struct {
	ID          string       `yaml:"id"`
	Category    string       `yaml:"category"`
	Title       string       `yaml:"title"`
	Description string       `yaml:"description"`
	Condition   ConditionDef `yaml:"condition"`
	Effect      EffectDef    `yaml:"effect"`
	Severity    string       `yaml:"severity"`
	GDPRRef     string       `yaml:"gdpr_ref"`
	Rationale   string       `yaml:"rationale"`
}

// ConditionDef represents a rule condition (supports field checks and compositions)
type ConditionDef struct {
	// Simple field check
	Field    string      `yaml:"field,omitempty"`
	Operator string      `yaml:"operator,omitempty"`
	Value    interface{} `yaml:"value,omitempty"`

	// Composite conditions
	AllOf []ConditionDef `yaml:"all_of,omitempty"`
	AnyOf []ConditionDef `yaml:"any_of,omitempty"`

	// Aggregate conditions (evaluated after all rules)
	Aggregate string `yaml:"aggregate,omitempty"`
}

// EffectDef represents the effect when a rule triggers
type EffectDef struct {
	RiskAdd           int      `yaml:"risk_add,omitempty"`
	Feasibility       string   `yaml:"feasibility,omitempty"`
	ControlsAdd       []string `yaml:"controls_add,omitempty"`
	SuggestedPatterns []string `yaml:"suggested_patterns,omitempty"`
	Escalation        bool     `yaml:"escalation,omitempty"`
	Art22Risk         bool     `yaml:"art22_risk,omitempty"`
	TrainingAllowed   bool     `yaml:"training_allowed,omitempty"`
	LegalBasis        string   `yaml:"legal_basis,omitempty"`
}

// ProblemSolutionDef maps problems to solutions
type ProblemSolutionDef struct {
	ProblemID string              `yaml:"problem_id"`
	Title     string              `yaml:"title"`
	Triggers  []ProblemTriggerDef `yaml:"triggers"`
	Solutions []SolutionDef       `yaml:"solutions"`
}

// ProblemTriggerDef defines when a problem is triggered
type ProblemTriggerDef struct {
	Rule           string `yaml:"rule"`
	WithoutControl string `yaml:"without_control,omitempty"`
}

// SolutionDef represents a potential solution
type SolutionDef struct {
	ID             string `yaml:"id"`
	Title          string `yaml:"title"`
	Pattern        string `yaml:"pattern,omitempty"`
	Control        string `yaml:"control,omitempty"`
	RemovesProblem bool   `yaml:"removes_problem"`
	TeamQuestion   string `yaml:"team_question"`
}

// EscalationTriggerDef defines when to escalate to DSB
type EscalationTriggerDef struct {
	Condition string `yaml:"condition"`
	Reason    string `yaml:"reason"`
}
