package handlers

import (
	"net/http"

	"github.com/breakpilot/ai-compliance-sdk/internal/ucca"
	"github.com/gin-gonic/gin"
)

// ListPatterns returns all available architecture patterns
func (h *UCCAHandlers) ListPatterns(c *gin.Context) {
	var response []gin.H

	if h.policyEngine != nil {
		yamlPatterns := h.policyEngine.GetAllPatterns()
		response = make([]gin.H, 0, len(yamlPatterns))
		for _, p := range yamlPatterns {
			response = append(response, gin.H{
				"id":             p.ID,
				"title":          p.Title,
				"description":    p.Description,
				"benefit":        p.Benefit,
				"effort":         p.Effort,
				"risk_reduction": p.RiskReduction,
			})
		}
	} else {
		patterns := ucca.GetAllPatterns()
		response = make([]gin.H, len(patterns))
		for i, p := range patterns {
			response[i] = gin.H{
				"id":             p.ID,
				"title":          p.Title,
				"title_de":       p.TitleDE,
				"description":    p.Description,
				"description_de": p.DescriptionDE,
				"benefits":       p.Benefits,
				"requirements":   p.Requirements,
			}
		}
	}

	c.JSON(http.StatusOK, gin.H{"patterns": response})
}

// ListControls returns all available compliance controls
func (h *UCCAHandlers) ListControls(c *gin.Context) {
	var response []gin.H

	if h.policyEngine != nil {
		yamlControls := h.policyEngine.GetAllControls()
		response = make([]gin.H, 0, len(yamlControls))
		for _, ctrl := range yamlControls {
			response = append(response, gin.H{
				"id":          ctrl.ID,
				"title":       ctrl.Title,
				"description": ctrl.Description,
				"gdpr_ref":    ctrl.GDPRRef,
				"effort":      ctrl.Effort,
			})
		}
	} else {
		for id, ctrl := range ucca.ControlLibrary {
			response = append(response, gin.H{
				"id":          id,
				"title":       ctrl.Title,
				"description": ctrl.Description,
				"severity":    ctrl.Severity,
				"category":    ctrl.Category,
				"gdpr_ref":    ctrl.GDPRRef,
			})
		}
	}

	c.JSON(http.StatusOK, gin.H{"controls": response})
}

// ListProblemSolutions returns all problem-solution mappings
func (h *UCCAHandlers) ListProblemSolutions(c *gin.Context) {
	if h.policyEngine == nil {
		c.JSON(http.StatusOK, gin.H{
			"problem_solutions": []gin.H{},
			"message":           "Problem-solutions only available with YAML policy engine",
		})
		return
	}

	problemSolutions := h.policyEngine.GetProblemSolutions()
	response := make([]gin.H, len(problemSolutions))
	for i, ps := range problemSolutions {
		solutions := make([]gin.H, len(ps.Solutions))
		for j, sol := range ps.Solutions {
			solutions[j] = gin.H{
				"id":              sol.ID,
				"title":           sol.Title,
				"pattern":         sol.Pattern,
				"control":         sol.Control,
				"removes_problem": sol.RemovesProblem,
				"team_question":   sol.TeamQuestion,
			}
		}

		triggers := make([]gin.H, len(ps.Triggers))
		for j, t := range ps.Triggers {
			triggers[j] = gin.H{
				"rule":            t.Rule,
				"without_control": t.WithoutControl,
			}
		}

		response[i] = gin.H{
			"problem_id": ps.ProblemID,
			"title":      ps.Title,
			"triggers":   triggers,
			"solutions":  solutions,
		}
	}

	c.JSON(http.StatusOK, gin.H{"problem_solutions": response})
}

// ListExamples returns all available didactic examples
func (h *UCCAHandlers) ListExamples(c *gin.Context) {
	examples := ucca.GetAllExamples()

	response := make([]gin.H, len(examples))
	for i, ex := range examples {
		response[i] = gin.H{
			"id":             ex.ID,
			"title":          ex.Title,
			"title_de":       ex.TitleDE,
			"description":    ex.Description,
			"description_de": ex.DescriptionDE,
			"domain":         ex.Domain,
			"outcome":        ex.Outcome,
			"outcome_de":     ex.OutcomeDE,
			"lessons":        ex.Lessons,
			"lessons_de":     ex.LessonsDE,
		}
	}

	c.JSON(http.StatusOK, gin.H{"examples": response})
}

// ListRules returns all rules for transparency
func (h *UCCAHandlers) ListRules(c *gin.Context) {
	var response []gin.H
	var policyVersion string

	if h.policyEngine != nil {
		yamlRules := h.policyEngine.GetAllRules()
		policyVersion = h.policyEngine.GetPolicyVersion()
		response = make([]gin.H, len(yamlRules))
		for i, r := range yamlRules {
			response[i] = gin.H{
				"code":        r.ID,
				"category":    r.Category,
				"title":       r.Title,
				"description": r.Description,
				"severity":    r.Severity,
				"gdpr_ref":    r.GDPRRef,
				"rationale":   r.Rationale,
				"controls":    r.Effect.ControlsAdd,
				"patterns":    r.Effect.SuggestedPatterns,
				"risk_add":    r.Effect.RiskAdd,
			}
		}
	} else {
		rules := h.legacyRuleEngine.GetRules()
		policyVersion = "1.0.0-legacy"
		response = make([]gin.H, len(rules))
		for i, r := range rules {
			response[i] = gin.H{
				"code":           r.Code,
				"category":       r.Category,
				"title":          r.Title,
				"title_de":       r.TitleDE,
				"description":    r.Description,
				"description_de": r.DescriptionDE,
				"severity":       r.Severity,
				"score_delta":    r.ScoreDelta,
				"gdpr_ref":       r.GDPRRef,
				"controls":       r.Controls,
				"patterns":       r.Patterns,
			}
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"rules":          response,
		"total":          len(response),
		"policy_version": policyVersion,
		"categories": []string{
			"A. Datenklassifikation",
			"B. Zweck & Rechtsgrundlage",
			"C. Automatisierung",
			"D. Training & Modell",
			"E. Hosting",
			"F. Domain-spezifisch",
			"G. Aggregation",
		},
	})
}
