package iace

// HazardPattern defines a rule that matches component/energy tags to
// hazards, measures, and evidence. When a pattern's required tags are all
// present (AND) and none of its excluded tags are present (NOT), it fires.
type HazardPattern struct {
	ID                    string   `json:"id"`
	NameDE                string   `json:"name_de"`
	NameEN                string   `json:"name_en"`
	RequiredComponentTags []string `json:"required_component_tags"`
	RequiredEnergyTags    []string `json:"required_energy_tags"`
	RequiredLifecycles    []string `json:"required_lifecycle_phases"`
	ExcludedComponentTags []string `json:"excluded_component_tags"`
	GeneratedHazardCats   []string `json:"generated_hazard_categories"`
	SuggestedMeasureIDs   []string `json:"suggested_measure_ids"`
	SuggestedEvidenceIDs  []string `json:"suggested_evidence_ids"`
	Priority              int      `json:"priority"`
	// Expert calculation hint — shown when pattern fires and expert validation is needed
	RequiresExpertCalculation bool   `json:"requires_expert_calculation,omitempty"`
	ExpertHintDE              string `json:"expert_hint_de,omitempty"`
	ExpertHintEN              string `json:"expert_hint_en,omitempty"`
	// ClarificationQuestionsDE: konkrete Fragen die der Bediener mit dem
	// Anlagenbauer abklaeren sollte, bevor er das Hazard als abgedeckt
	// betrachtet. Die Engine erfindet keine Begruendungen, sondern hinter-
	// legt nur prueffaehige Fragen aus Norm-Wissen. Beispiele:
	//   - "Liegt ein Pruefprotokoll nach EN 60204-1 vor?"
	//   - "Wird die Sicherheitsfunktion durch CE-Konformitaet der
	//     Subkomponente abgedeckt? (Herstellererklaerung anfordern)"
	//   - "Ist DCS-Achsbegrenzung am Roboter konfiguriert und validiert?"
	// Im Init-Handler werden diese Fragen an die Hazard.Description
	// angehaengt mit einem klaren Trenner — kein DB-Schema-Aenderungs-
	// bedarf.
	ClarificationQuestionsDE []string `json:"clarification_questions_de,omitempty"`
	// Detail fields — populated into Hazard DB when pattern fires
	ScenarioDE    string `json:"scenario_de,omitempty"`    // Gefahrensituation
	TriggerDE     string `json:"trigger_de,omitempty"`     // Gefaehrdendes Ereignis
	HarmDE        string `json:"harm_de,omitempty"`        // Moegliche Verletzung
	AffectedDE    string `json:"affected_de,omitempty"`    // Betroffene Personen
	ZoneDE        string `json:"zone_de,omitempty"`        // Gefahrstelle/Zone
	DefaultSeverity  int `json:"default_severity,omitempty"`  // 1-5
	DefaultExposure  int `json:"default_exposure,omitempty"`  // 1-5
	// MachineTypes restricts this pattern to specific machine types.
	// Empty = fires for all machine types. If set, only fires when the
	// project's machine_type is in this list. Prevents e.g. press-specific
	// patterns from firing for a cobot project.
	MachineTypes []string `json:"machine_types,omitempty"`
	// OperationalStates restricts this pattern to specific operational states.
	// Empty/nil = fires in ALL states (backwards compatible). If set, the pattern
	// only fires when the project declares at least one matching state.
	// Standard states: startup, homing, automatic_operation, manual_operation,
	// teach_mode, maintenance, cleaning, emergency_stop, recovery_mode.
	OperationalStates []string `json:"operational_states,omitempty"`
	// StateTransitions restricts to specific state transitions (format: "from→to").
	// Hazards at transitions are critical — e.g. "maintenance→automatic_operation"
	// is where "unexpected restart" fatalities occur. Empty = no transition filter.
	StateTransitions []string `json:"state_transitions,omitempty"`
	// HumanRoles restricts this pattern to projects where specific human roles
	// interact with the machine. Empty/nil = fires for all roles (backwards compatible).
	// Standard roles: operator, maintenance_tech, programmer, cleaning_staff, bystander, supervisor.
	HumanRoles []string `json:"human_roles,omitempty"`
	// GeneratedHazardType sets the ISO 12100 type for hazards created by this pattern.
	// "hazard" = source only, "hazardous_situation" = person exposed, "harm" = injury.
	// Empty = default (hazardous_situation).
	GeneratedHazardType string `json:"generated_hazard_type,omitempty"`
	// GuardableByEnclosure marks a contact/entanglement hazard that an interlocked
	// enclosure removes during normal operation. When the project emits the
	// "interlocked_enclosure" tag, such a pattern is re-scoped to maintenance/
	// cleaning (guard open) and does NOT fire as a normal-operation hazard.
	// Generic EN ISO 14120 logic — surfaced by the warewashing GT (the spray
	// arm rotates behind the interlocked door).
	GuardableByEnclosure bool `json:"guardable_by_enclosure,omitempty"`
	// RequiredFailureModes restricts this pattern to fire only when at least one
	// of the listed failure modes is relevant (by ComponentType match against project components).
	// Empty/nil = fires regardless of failure modes (backwards compatible).
	RequiredFailureModes []string `json:"required_failure_modes,omitempty"`
	// ApplicableLifecycles lists the ISO 12100 lifecycle phases where this hazard
	// is relevant. Written into the Hazard's LifecyclePhase field on creation.
	// Empty = not set (pattern does not specify lifecycle applicability).
	ApplicableLifecycles []string `json:"applicable_lifecycles,omitempty"`
	// ISO12100Section is a free-text identifier of the ISO 12100 Annex B
	// section that owns this hazard type (e.g. "6.2.2.1" or "6.3.5.5").
	// Stored as an identifier only — the norm text is NOT included to
	// keep the library urheberrechtlich neutral (DIN/Beuth license).
	// The frontend renders it as "EN ISO 12100 Abschnitt 6.3.5.5".
	ISO12100Section string `json:"iso_12100_section,omitempty"`
	// DefaultAvoidability is our avoidance parameter: 1 = avoidable under
	// certain conditions, 2 = hardly avoidable. Feeds BreakPilot's own risk
	// model (risk_estimation.go) — NOT a reproduced norm risk graph.
	DefaultAvoidability int `json:"default_avoidability,omitempty"` // 1 or 2
	// SecondaryHarms describes consequential damage chains beyond the
	// classical IACE Hazard→Harm step: end-customer safety, product
	// liability, food safety, environmental, reputation, financial.
	// See secondary_harms.go and the strategy discussion (2026-05-20).
	// Empty for hazards with no downstream chain.
	SecondaryHarms []SecondaryHarm `json:"secondary_harms,omitempty"`
}

// Standard human roles for machinery interaction (ISO 12100 + BetrSichV).
const (
	RoleOperator        = "operator"
	RoleMaintenanceTech = "maintenance_tech"
	RoleProgrammer      = "programmer"
	RoleCleaningStaff   = "cleaning_staff"
	RoleBystander       = "bystander"
	RoleSupervisor      = "supervisor"
)

// AllHumanRoles returns the 6 standard human roles.
func AllHumanRoles() []string {
	return []string{
		RoleOperator, RoleMaintenanceTech, RoleProgrammer,
		RoleCleaningStaff, RoleBystander, RoleSupervisor,
	}
}
