package ucca

import (
	"bytes"
	"fmt"
	"time"
)

// ExportMarkdown exports the overview as Markdown (for compatibility)
func (e *PDFExporter) ExportMarkdown(overview *ManagementObligationsOverview) (*ExportMemoResponse, error) {
	var buf bytes.Buffer

	// Title
	buf.WriteString(fmt.Sprintf("# Regulatorische Pflichten-Uebersicht\n\n"))
	if overview.OrganizationName != "" {
		buf.WriteString(fmt.Sprintf("**Organisation:** %s\n\n", overview.OrganizationName))
	}
	buf.WriteString(fmt.Sprintf("**Stand:** %s\n\n", overview.AssessmentDate.Format("02.01.2006")))

	// Executive Summary
	buf.WriteString("## Executive Summary\n\n")
	summary := overview.ExecutiveSummary
	buf.WriteString(fmt.Sprintf("| Metrik | Wert |\n"))
	buf.WriteString(fmt.Sprintf("|--------|------|\n"))
	buf.WriteString(fmt.Sprintf("| Anwendbare Regulierungen | %d |\n", summary.TotalRegulations))
	buf.WriteString(fmt.Sprintf("| Gesamtzahl Pflichten | %d |\n", summary.TotalObligations))
	buf.WriteString(fmt.Sprintf("| Kritische Pflichten | %d |\n", summary.CriticalObligations))
	buf.WriteString(fmt.Sprintf("| Kommende Fristen (30 Tage) | %d |\n", summary.UpcomingDeadlines))
	buf.WriteString(fmt.Sprintf("| Compliance Score | %d%% |\n\n", summary.ComplianceScore))

	// Key Risks
	if len(summary.KeyRisks) > 0 {
		buf.WriteString("### Wesentliche Risiken\n\n")
		for _, risk := range summary.KeyRisks {
			buf.WriteString(fmt.Sprintf("- %s\n", risk))
		}
		buf.WriteString("\n")
	}

	// Recommended Actions
	if len(summary.RecommendedActions) > 0 {
		buf.WriteString("### Empfohlene Massnahmen\n\n")
		for _, action := range summary.RecommendedActions {
			buf.WriteString(fmt.Sprintf("- %s\n", action))
		}
		buf.WriteString("\n")
	}

	// Applicable Regulations
	buf.WriteString("## Anwendbare Regulierungen\n\n")
	buf.WriteString("| Regulierung | Klassifizierung | Pflichten | Grund |\n")
	buf.WriteString("|-------------|-----------------|-----------|-------|\n")
	for _, reg := range overview.ApplicableRegulations {
		buf.WriteString(fmt.Sprintf("| %s | %s | %d | %s |\n", reg.Name, reg.Classification, reg.ObligationCount, reg.Reason))
	}
	buf.WriteString("\n")

	// Sanctions Summary
	buf.WriteString("## Sanktionsrisiken\n\n")
	sanctions := overview.SanctionsSummary
	if sanctions.MaxFinancialRisk != "" {
		buf.WriteString(fmt.Sprintf("- **Max. Finanzrisiko:** %s\n", sanctions.MaxFinancialRisk))
	}
	buf.WriteString(fmt.Sprintf("- **Persoenliche Haftung:** %v\n", sanctions.PersonalLiabilityRisk))
	buf.WriteString(fmt.Sprintf("- **Strafrechtliche Konsequenzen:** %v\n\n", sanctions.CriminalLiabilityRisk))
	if sanctions.Summary != "" {
		buf.WriteString(fmt.Sprintf("*%s*\n\n", sanctions.Summary))
	}

	// Obligations
	buf.WriteString("## Pflichten-Uebersicht\n\n")
	for _, obl := range overview.Obligations {
		buf.WriteString(fmt.Sprintf("### %s - %s\n\n", obl.ID, obl.Title))
		buf.WriteString(fmt.Sprintf("**Prioritaet:** %s | **Verantwortlich:** %s\n\n", obl.Priority, obl.Responsible))
		if len(obl.LegalBasis) > 0 {
			buf.WriteString("**Rechtsgrundlage:** ")
			for i, lb := range obl.LegalBasis {
				if i > 0 {
					buf.WriteString(", ")
				}
				buf.WriteString(lb.Norm)
			}
			buf.WriteString("\n\n")
		}
		buf.WriteString(fmt.Sprintf("%s\n\n", obl.Description))
	}

	// Incident Deadlines
	if len(overview.IncidentDeadlines) > 0 {
		buf.WriteString("## Meldepflichten bei Vorfaellen\n\n")
		for _, dl := range overview.IncidentDeadlines {
			buf.WriteString(fmt.Sprintf("- **%s:** %s an %s\n", dl.Phase, dl.Deadline, dl.Recipient))
		}
		buf.WriteString("\n")
	}

	// Footer
	buf.WriteString("---\n\n")
	buf.WriteString(fmt.Sprintf("*Generiert am %s mit BreakPilot AI Compliance SDK*\n", time.Now().Format("02.01.2006 15:04")))

	return &ExportMemoResponse{
		Content:     buf.String(),
		ContentType: "text/markdown",
		Filename:    fmt.Sprintf("pflichten-uebersicht-%s.md", time.Now().Format("2006-01-02")),
		GeneratedAt: time.Now(),
	}, nil
}
