package iace

import (
	"encoding/json"
	"fmt"

	"github.com/google/uuid"
)

// hazardUUID generates a deterministic UUID for a hazard library entry
// based on category and a 1-based index within that category.
func hazardUUID(category string, index int) uuid.UUID {
	name := fmt.Sprintf("iace.hazard.%s.%d", category, index)
	return uuid.NewSHA1(uuid.NameSpaceDNS, []byte(name))
}

// mustMarshalJSON marshals the given value to json.RawMessage, panicking on error.
// This is safe to use for static data known at compile time.
func mustMarshalJSON(v interface{}) json.RawMessage {
	data, err := json.Marshal(v)
	if err != nil {
		panic(fmt.Sprintf("hazard_library: failed to marshal JSON: %v", err))
	}
	return data
}

// GetBuiltinHazardLibrary returns the complete built-in hazard library with 40+
// template entries for SW/FW/KI hazards in industrial machines. These entries are
// intended to be seeded into the iace_hazard_library table during initial setup.
//
// All entries have IsBuiltin=true and TenantID=nil (system-level templates).
// UUIDs are deterministic, generated via uuid.NewSHA1 based on category and index.
func GetBuiltinHazardLibrary() []HazardLibraryEntry {
	var all []HazardLibraryEntry
	all = append(all, builtinHazardsAISW()...)
	all = append(all, builtinHazardsAIFW()...)
	all = append(all, builtinHazardsSoftwareHMI()...)
	all = append(all, builtinHazardsConfigIntegration()...)
	all = append(all, builtinHazardsMachineSafety()...)
	all = append(all, builtinHazardsSafetyFunctions()...)
	all = append(all, builtinHazardsISO12100Mechanical()...)
	all = append(all, builtinHazardsISO12100ElectricalThermal()...)
	all = append(all, builtinHazardsISO12100Pneumatic()...)
	all = append(all, builtinHazardsISO12100Env()...)
	return all
}
