package ucca

// ScopeDecision represents the output from the frontend Scope Wizard
type ScopeDecision struct {
	// Company profile
	EmployeeCount     int     `json:"employee_count"`
	AnnualRevenue     float64 `json:"annual_revenue"`
	Country           string  `json:"country"`
	Industry          string  `json:"industry"`
	LegalForm         string  `json:"legal_form,omitempty"`

	// Scope wizard answers
	ProcessesPersonalData    bool     `json:"processes_personal_data"`
	IsController             bool     `json:"is_controller"`
	IsProcessor              bool     `json:"is_processor"`
	DataArt9                 bool     `json:"data_art9"`
	DataMinors               bool     `json:"data_minors"`
	LargeScale               bool     `json:"large_scale"`
	SystematicMonitoring     bool     `json:"systematic_monitoring"`
	CrossBorderTransfer      bool     `json:"cross_border_transfer"`
	UsesProcessors           bool     `json:"uses_processors"`
	AutomatedDecisions       bool     `json:"automated_decisions"`
	ProcessesEmployeeData    bool     `json:"processes_employee_data"`
	ProcessesHealthData      bool     `json:"processes_health_data"`
	ProcessesFinancialData   bool     `json:"processes_financial_data"`
	UsesCookies              bool     `json:"uses_cookies"`
	UsesTracking             bool     `json:"uses_tracking"`
	UsesVideoSurveillance    bool     `json:"uses_video_surveillance"`
	OperatesPlatform         bool     `json:"operates_platform"`
	PlatformUserCount        int      `json:"platform_user_count,omitempty"`

	// AI usage
	ProcAIUsage              bool     `json:"proc_ai_usage"`
	IsAIProvider             bool     `json:"is_ai_provider"`
	IsAIDeployer             bool     `json:"is_ai_deployer"`
	HighRiskAI               bool     `json:"high_risk_ai"`
	LimitedRiskAI            bool     `json:"limited_risk_ai"`

	// Sector / NIS2
	Sector                   string   `json:"sector,omitempty"`
	SpecialServices          []string `json:"special_services,omitempty"`
	IsKRITIS                 bool     `json:"is_kritis"`
	IsFinancialInstitution   bool     `json:"is_financial_institution"`

	// Scope engine results
	DeterminedLevel          string   `json:"determined_level,omitempty"` // L1-L4
	TriggeredRules           []string `json:"triggered_rules,omitempty"`
	RequiredDocuments        []string `json:"required_documents,omitempty"`
	CertTarget               string   `json:"cert_target,omitempty"`
}

// MapScopeToFacts converts a ScopeDecision to UnifiedFacts
func MapScopeToFacts(scope *ScopeDecision) *UnifiedFacts {
	facts := NewUnifiedFacts()

	// Organization
	facts.Organization.EmployeeCount = scope.EmployeeCount
	facts.Organization.AnnualRevenue = scope.AnnualRevenue
	facts.Organization.Country = scope.Country
	facts.Organization.LegalForm = scope.LegalForm
	if scope.Country != "" {
		facts.Organization.EUMember = isEUCountryScope(scope.Country)
	}

	// Data Protection
	facts.DataProtection.ProcessesPersonalData = scope.ProcessesPersonalData
	facts.DataProtection.IsController = scope.IsController
	facts.DataProtection.IsProcessor = scope.IsProcessor
	facts.DataProtection.ProcessesSpecialCategories = scope.DataArt9
	facts.DataProtection.ProcessesMinorData = scope.DataMinors
	facts.DataProtection.LargeScaleProcessing = scope.LargeScale
	facts.DataProtection.SystematicMonitoring = scope.SystematicMonitoring
	facts.DataProtection.TransfersToThirdCountries = scope.CrossBorderTransfer
	facts.DataProtection.CrossBorderProcessing = scope.CrossBorderTransfer
	facts.DataProtection.UsesExternalProcessor = scope.UsesProcessors
	facts.DataProtection.AutomatedDecisionMaking = scope.AutomatedDecisions
	facts.DataProtection.AutomatedDecisions = scope.AutomatedDecisions
	facts.DataProtection.ProcessesEmployeeData = scope.ProcessesEmployeeData
	facts.DataProtection.ProcessesHealthData = scope.ProcessesHealthData
	facts.DataProtection.ProcessesFinancialData = scope.ProcessesFinancialData
	facts.DataProtection.UsesCookies = scope.UsesCookies
	facts.DataProtection.UsesTracking = scope.UsesTracking
	facts.DataProtection.UsesVideoSurveillance = scope.UsesVideoSurveillance
	facts.DataProtection.OperatesPlatform = scope.OperatesPlatform
	facts.DataProtection.PlatformUserCount = scope.PlatformUserCount

	// DPO requirement (German law: >= 20 employees processing personal data)
	if scope.EmployeeCount >= 20 && scope.ProcessesPersonalData {
		facts.DataProtection.RequiresDSBByLaw = true
	}

	// AI Usage
	facts.AIUsage.UsesAI = scope.ProcAIUsage
	facts.AIUsage.IsAIProvider = scope.IsAIProvider
	facts.AIUsage.IsAIDeployer = scope.IsAIDeployer
	facts.AIUsage.HasHighRiskAI = scope.HighRiskAI
	facts.AIUsage.HasLimitedRiskAI = scope.LimitedRiskAI

	// Sector
	if scope.Sector != "" {
		facts.Sector.PrimarySector = scope.Sector
	} else if scope.Industry != "" {
		facts.MapDomainToSector(scope.Industry)
	}
	facts.Sector.SpecialServices = scope.SpecialServices
	facts.Sector.IsKRITIS = scope.IsKRITIS
	facts.Sector.KRITISThresholdMet = scope.IsKRITIS
	facts.Sector.IsFinancialInstitution = scope.IsFinancialInstitution

	// Financial
	if scope.IsFinancialInstitution {
		facts.Financial.IsRegulated = true
		facts.Financial.DORAApplies = true
	}

	return facts
}

func isEUCountryScope(country string) bool {
	euCountries := map[string]bool{
		"DE": true, "AT": true, "BE": true, "BG": true, "HR": true, "CY": true,
		"CZ": true, "DK": true, "EE": true, "FI": true, "FR": true, "GR": true,
		"HU": true, "IE": true, "IT": true, "LV": true, "LT": true, "LU": true,
		"MT": true, "NL": true, "PL": true, "PT": true, "RO": true, "SK": true,
		"SI": true, "ES": true, "SE": true,
	}
	return euCountries[country]
}
