package ucca

import (
	"time"

	"github.com/google/uuid"
)

// Keep imports used by DecisionTreeResult.
var (
	_ uuid.UUID
	_ time.Time
)

// ============================================================================
// Constants / Enums
// ============================================================================

// Feasibility represents the overall assessment result
type Feasibility string

const (
	FeasibilityYES         Feasibility = "YES"
	FeasibilityCONDITIONAL Feasibility = "CONDITIONAL"
	FeasibilityNO          Feasibility = "NO"
)

// RiskLevel represents the overall risk classification
type RiskLevel string

const (
	RiskLevelMINIMAL      RiskLevel = "MINIMAL"
	RiskLevelLOW          RiskLevel = "LOW"
	RiskLevelMEDIUM       RiskLevel = "MEDIUM"
	RiskLevelHIGH         RiskLevel = "HIGH"
	RiskLevelUNACCEPTABLE RiskLevel = "UNACCEPTABLE"
)

// Complexity represents implementation complexity
type Complexity string

const (
	ComplexityLOW    Complexity = "LOW"
	ComplexityMEDIUM Complexity = "MEDIUM"
	ComplexityHIGH   Complexity = "HIGH"
)

// Severity represents rule severity
type Severity string

const (
	SeverityINFO  Severity = "INFO"
	SeverityWARN  Severity = "WARN"
	SeverityBLOCK Severity = "BLOCK"
)

// Domain represents the business domain
type Domain string

const (
	// Industrie & Produktion
	DomainAutomotive            Domain = "automotive"
	DomainMechanicalEngineering Domain = "mechanical_engineering"
	DomainPlantEngineering      Domain = "plant_engineering"
	DomainElectricalEngineering Domain = "electrical_engineering"
	DomainAerospace             Domain = "aerospace"
	DomainChemicals             Domain = "chemicals"
	DomainFoodBeverage          Domain = "food_beverage"
	DomainTextiles              Domain = "textiles"
	DomainPackaging             Domain = "packaging"

	// Energie & Versorgung
	DomainUtilities Domain = "utilities"
	DomainEnergy    Domain = "energy"
	DomainOilGas    Domain = "oil_gas"

	// Land- & Forstwirtschaft
	DomainAgriculture Domain = "agriculture"
	DomainForestry    Domain = "forestry"
	DomainFishing     Domain = "fishing"

	// Bau & Immobilien
	DomainConstruction       Domain = "construction"
	DomainRealEstate         Domain = "real_estate"
	DomainFacilityManagement Domain = "facility_management"

	// Gesundheit & Soziales
	DomainHealthcare     Domain = "healthcare"
	DomainMedicalDevices Domain = "medical_devices"
	DomainPharma         Domain = "pharma"
	DomainElderlyCare    Domain = "elderly_care"
	DomainSocialServices Domain = "social_services"

	// Bildung & Forschung
	DomainEducation          Domain = "education"
	DomainHigherEducation    Domain = "higher_education"
	DomainVocationalTraining Domain = "vocational_training"
	DomainResearch           Domain = "research"

	// Finanzen & Versicherung
	DomainFinance    Domain = "finance"
	DomainBanking    Domain = "banking"
	DomainInsurance  Domain = "insurance"
	DomainInvestment Domain = "investment"

	// Handel & Logistik
	DomainRetail    Domain = "retail"
	DomainEcommerce Domain = "ecommerce"
	DomainWholesale Domain = "wholesale"
	DomainLogistics Domain = "logistics"

	// IT & Telekommunikation
	DomainITServices    Domain = "it_services"
	DomainTelecom       Domain = "telecom"
	DomainCybersecurity Domain = "cybersecurity"

	// Recht & Beratung
	DomainLegal       Domain = "legal"
	DomainConsulting  Domain = "consulting"
	DomainTaxAdvisory Domain = "tax_advisory"

	// Oeffentlicher Sektor
	DomainPublic  Domain = "public_sector"
	DomainDefense Domain = "defense"
	DomainJustice Domain = "justice"

	// Marketing & Medien
	DomainMarketing     Domain = "marketing"
	DomainMedia         Domain = "media"
	DomainEntertainment Domain = "entertainment"

	// HR & Personal
	DomainHR         Domain = "hr"
	DomainRecruiting Domain = "recruiting"

	// Tourismus & Gastronomie
	DomainHospitality Domain = "hospitality"
	DomainTourism     Domain = "tourism"

	// Sonstige
	DomainNonprofit Domain = "nonprofit"
	DomainSports    Domain = "sports"
	DomainGeneral   Domain = "general"
)

// ValidDomains contains all valid domain values
var ValidDomains = map[Domain]bool{
	DomainAutomotive: true, DomainMechanicalEngineering: true, DomainPlantEngineering: true,
	DomainElectricalEngineering: true, DomainAerospace: true, DomainChemicals: true,
	DomainFoodBeverage: true, DomainTextiles: true, DomainPackaging: true,
	DomainUtilities: true, DomainEnergy: true, DomainOilGas: true,
	DomainAgriculture: true, DomainForestry: true, DomainFishing: true,
	DomainConstruction: true, DomainRealEstate: true, DomainFacilityManagement: true,
	DomainHealthcare: true, DomainMedicalDevices: true, DomainPharma: true,
	DomainElderlyCare: true, DomainSocialServices: true,
	DomainEducation: true, DomainHigherEducation: true, DomainVocationalTraining: true, DomainResearch: true,
	DomainFinance: true, DomainBanking: true, DomainInsurance: true, DomainInvestment: true,
	DomainRetail: true, DomainEcommerce: true, DomainWholesale: true, DomainLogistics: true,
	DomainITServices: true, DomainTelecom: true, DomainCybersecurity: true,
	DomainLegal: true, DomainConsulting: true, DomainTaxAdvisory: true,
	DomainPublic: true, DomainDefense: true, DomainJustice: true,
	DomainMarketing: true, DomainMedia: true, DomainEntertainment: true,
	DomainHR: true, DomainRecruiting: true,
	DomainHospitality: true, DomainTourism: true,
	DomainNonprofit: true, DomainSports: true, DomainGeneral: true,
}

// AutomationLevel represents the degree of automation
type AutomationLevel string

const (
	AutomationAssistive      AutomationLevel = "assistive"
	AutomationSemiAutomated  AutomationLevel = "semi_automated"
	AutomationFullyAutomated AutomationLevel = "fully_automated"
)

// TrainingAllowed represents if training with data is permitted
type TrainingAllowed string

const (
	TrainingYES         TrainingAllowed = "YES"
	TrainingCONDITIONAL TrainingAllowed = "CONDITIONAL"
	TrainingNO          TrainingAllowed = "NO"
)

// ExportFormat specifies the export format
type ExportFormat string

const (
	ExportFormatJSON     ExportFormat = "json"
	ExportFormatMarkdown ExportFormat = "md"
)

// ============================================================================
// AI Act Decision Tree Types
// ============================================================================

// GPAICategory represents the GPAI classification result
type GPAICategory string

const (
	GPAICategoryNone     GPAICategory = "none"
	GPAICategoryStandard GPAICategory = "standard"
	GPAICategorySystemic GPAICategory = "systemic"
)

// GPAIClassification represents the result of the GPAI axis evaluation
type GPAIClassification struct {
	IsGPAI             bool     `json:"is_gpai"`
	IsSystemicRisk     bool     `json:"is_systemic_risk"`
	Category           GPAICategory `json:"gpai_category"`
	ApplicableArticles []string `json:"applicable_articles"`
	Obligations        []string `json:"obligations"`
}

// DecisionTreeAnswer represents a user's answer to a decision tree question
type DecisionTreeAnswer struct {
	QuestionID string `json:"question_id"`
	Value      bool   `json:"value"`
	Note       string `json:"note,omitempty"`
}

// DecisionTreeQuestion represents a single question in the decision tree
type DecisionTreeQuestion struct {
	ID          string `json:"id"`
	Axis        string `json:"axis"`        // "high_risk" or "gpai"
	Question    string `json:"question"`
	Description string `json:"description"` // Additional context
	ArticleRef  string `json:"article_ref"` // e.g., "Art. 5", "Anhang III"
	SkipIf      string `json:"skip_if,omitempty"` // Question ID — skip if that was answered "no"
}

// DecisionTreeDefinition represents the full decision tree structure for the frontend
type DecisionTreeDefinition struct {
	ID        string                 `json:"id"`
	Name      string                 `json:"name"`
	Version   string                 `json:"version"`
	Questions []DecisionTreeQuestion `json:"questions"`
}

// DecisionTreeEvalRequest is the API request for evaluating the decision tree
type DecisionTreeEvalRequest struct {
	SystemName        string                        `json:"system_name"`
	SystemDescription string                        `json:"system_description,omitempty"`
	Answers           map[string]DecisionTreeAnswer  `json:"answers"`
}

// DecisionTreeResult represents the combined evaluation result
type DecisionTreeResult struct {
	ID                  uuid.UUID          `json:"id"`
	TenantID            uuid.UUID          `json:"tenant_id"`
	ProjectID           *uuid.UUID         `json:"project_id,omitempty"`
	SystemName          string             `json:"system_name"`
	SystemDescription   string             `json:"system_description,omitempty"`
	Answers             map[string]DecisionTreeAnswer `json:"answers"`
	HighRiskResult      AIActRiskLevel     `json:"high_risk_result"`
	GPAIResult          GPAIClassification `json:"gpai_result"`
	CombinedObligations []string           `json:"combined_obligations"`
	ApplicableArticles  []string           `json:"applicable_articles"`
	CreatedAt           time.Time          `json:"created_at"`
	UpdatedAt           time.Time          `json:"updated_at"`
}
