package output

import (
	"fmt"
	"html/template"
	"os"
	"time"

	"github.com/step-security/dev-machine-guard/internal/buildinfo"
	"github.com/step-security/dev-machine-guard/internal/model"
)

type htmlData struct {
	ScanTime          string
	Version           string
	Device            model.Device
	AITools           []model.AITool
	IDEInstallations  []model.IDE
	IDEExtensions     []model.Extension
	MCPConfigs        []model.MCPConfig
	NodePkgManagers   []model.PkgManager
	NodeProjects      []model.ProjectInfo
	BrewPkgManager    *model.PkgManager
	BrewFormulae      []model.BrewPackage
	BrewCasks         []model.BrewPackage
	PythonPkgManagers []model.PkgManager
	PythonPackages    []model.PythonPackage
	PythonProjects    []model.ProjectInfo
	Summary           model.Summary
}

func typeLabel(t string) string {
	switch t {
	case "cli_tool":
		return "CLI Tool"
	case "general_agent":
		return "Agent"
	case "framework":
		return "Framework"
	default:
		return t
	}
}

// HTML generates a self-contained HTML report file.
func HTML(outputFile string, result *model.ScanResult) error {
	f, err := os.Create(outputFile)
	if err != nil {
		return fmt.Errorf("creating HTML file: %w", err)
	}
	defer func() { _ = f.Close() }()

	scanTime := time.Unix(result.ScanTimestamp, 0).Format("2006-01-02 15:04:05")

	data := htmlData{
		ScanTime:          scanTime,
		Version:           buildinfo.Version,
		Device:            result.Device,
		AITools:           result.AIAgentsAndTools,
		IDEInstallations:  result.IDEInstallations,
		IDEExtensions:     result.IDEExtensions,
		MCPConfigs:        result.MCPConfigs,
		NodePkgManagers:   result.NodePkgManagers,
		NodeProjects:      result.NodeProjects,
		BrewPkgManager:    result.BrewPkgManager,
		BrewFormulae:      result.BrewFormulae,
		BrewCasks:         result.BrewCasks,
		PythonPkgManagers: result.PythonPkgManagers,
		PythonPackages:    result.PythonPackages,
		PythonProjects:    result.PythonProjects,
		Summary:           result.Summary,
	}

	funcMap := template.FuncMap{
		"ideDisplayName":      ideDisplayName,
		"typeLabel":           typeLabel,
		"platformDisplayName": model.PlatformDisplayName,
		"add":                 func(a, b int) int { return a + b },
		"formatBytes":         formatBytes,
		"formatCPU":           formatCPU,
	}

	tmpl, err := template.New("report").Funcs(funcMap).Parse(htmlTemplate)
	if err != nil {
		return fmt.Errorf("parsing HTML template: %w", err)
	}

	return tmpl.Execute(f, data)
}

const htmlTemplate = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>StepSecurity Dev Machine Guard Report</title>
<style>
  * { margin: 0; padding: 0; box-sizing: border-box; }
  body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #faf7fb; color: #193447; line-height: 1.6;
  }
  .header {
    background: linear-gradient(135deg, #7037f5, #9b59f5);
    color: #fff; padding: 32px 0; text-align: center;
  }
  .header h1 { font-size: 1.6em; font-weight: 600; margin-bottom: 4px; }
  .header p { opacity: 0.85; font-size: 0.95em; }
  .container { max-width: 960px; margin: 0 auto; padding: 24px 16px; }
  .summary-cards {
    display: flex; gap: 12px; margin-bottom: 28px; flex-wrap: wrap;
  }
  .card {
    flex: 1; min-width: 120px; background: #fff; border-radius: 10px;
    padding: 18px 16px; text-align: center;
    border: 1px solid #e8e0f0; box-shadow: 0 1px 3px rgba(112,55,245,0.06);
  }
  .card .number { font-size: 2em; font-weight: 700; color: #7037f5; }
  .card .label { font-size: 0.82em; color: #8a94a6; margin-top: 2px; }
  .device-grid {
    display: grid; grid-template-columns: 1fr 1fr; gap: 8px 32px;
    background: #fff; border-radius: 10px; padding: 20px 24px;
    margin-bottom: 28px; border: 1px solid #e8e0f0;
  }
  .device-grid .field { display: flex; gap: 12px; padding: 6px 0; }
  .device-grid .field-label { color: #8a94a6; min-width: 90px; font-size: 0.9em; }
  .device-grid .field-value { font-weight: 500; }
  .section { margin-bottom: 28px; }
  .section-header {
    display: flex; align-items: center; justify-content: space-between; cursor: pointer;
    padding-bottom: 6px; border-bottom: 2px solid #f0ebff; margin-bottom: 12px;
    user-select: none;
  }
  .section-header h2 { font-size: 1.1em; color: #7037f5; margin: 0; }
  .section-header .count {
    background: #f0ebff; color: #7037f5;
    padding: 2px 10px; border-radius: 10px; font-size: 0.85em;
  }
  .section-header .toggle {
    font-size: 1.2em; color: #7037f5; transition: transform 0.2s;
    margin-left: 8px;
  }
  .section-header .toggle.collapsed { transform: rotate(-90deg); }
  .section-body { overflow: hidden; transition: max-height 0.3s ease; }
  .section-body.collapsed { max-height: 0 !important; overflow: hidden; }
  table {
    width: 100%; border-collapse: collapse; background: #fff;
    border-radius: 10px; overflow: hidden; border: 1px solid #e8e0f0;
  }
  th {
    background: #f0ebff; color: #7037f5; font-weight: 600;
    text-align: left; padding: 10px 14px; font-size: 0.85em;
    text-transform: uppercase; letter-spacing: 0.5px;
  }
  td { padding: 9px 14px; border-top: 1px solid #f0ebff; font-size: 0.92em; }
  tr:hover td { background: #faf7fb; }
  .type-badge {
    background: #f0ebff; color: #7037f5; padding: 2px 8px;
    border-radius: 10px; font-size: 0.8em;
  }
  .project-row { background: #f8f5ff; }
  .project-row td { font-weight: 600; color: #7037f5; border-top: 2px solid #e8e0f0; }
  .pkg-row td { padding-left: 36px; color: #555; font-size: 0.88em; }
  .footer {
    text-align: center; padding: 24px; color: #8a94a6; font-size: 0.85em;
    border-top: 1px solid #e8e0f0; margin-top: 12px;
  }
  .footer a { color: #7037f5; text-decoration: none; }
  .footer a:hover { text-decoration: underline; }
  .scan-meta { text-align: center; color: #8a94a6; font-size: 0.85em; margin-bottom: 20px; }
  @media print {
    body { background: #fff; }
    .header { background: #7037f5; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    .card { break-inside: avoid; }
    .section-body.collapsed { max-height: none !important; }
    .toggle { display: none; }
  }
  @media (max-width: 600px) {
    .summary-cards { flex-direction: column; }
    .device-grid { grid-template-columns: 1fr; }
  }
</style>
</head>
<body>
<div class="header">
  <h1>StepSecurity Dev Machine Guard Report</h1>
  <p>Developer Environment Security Scanner</p>
</div>
<div class="container">

<p class="scan-meta">Scanned at {{.ScanTime}} &middot; Agent v{{.Version}}</p>

<div class="summary-cards">
  <div class="card"><div class="number">{{.Summary.AIAgentsAndToolsCount}}</div><div class="label">AI Agents & Tools</div></div>
  <div class="card"><div class="number">{{.Summary.IDEInstallationsCount}}</div><div class="label">IDEs & Apps</div></div>
  <div class="card"><div class="number">{{.Summary.IDEExtensionsCount}}</div><div class="label">IDE Extensions</div></div>
  <div class="card"><div class="number">{{.Summary.MCPConfigsCount}}</div><div class="label">MCP Servers</div></div>
  <div class="card"><div class="number">{{.Summary.NodeProjectsCount}}</div><div class="label">Node.js Projects</div></div>
  <div class="card"><div class="number">{{add .Summary.BrewFormulaeCount .Summary.BrewCasksCount}}</div><div class="label">Brew Packages</div></div>
  <div class="card"><div class="number">{{.Summary.PythonProjectsCount}}</div><div class="label">Python Venvs</div></div>
</div>

<div class="device-grid">
  <div class="field"><span class="field-label">Hostname</span><span class="field-value">{{.Device.Hostname}}</span></div>
  <div class="field"><span class="field-label">Serial</span><span class="field-value">{{.Device.SerialNumber}}</span></div>
  <div class="field"><span class="field-label">{{platformDisplayName .Device.Platform}}</span><span class="field-value">{{.Device.OSVersion}}</span></div>
  <div class="field"><span class="field-label">User</span><span class="field-value">{{.Device.UserIdentity}}</span></div>
  {{with formatCPU .Device.Resources}}<div class="field"><span class="field-label">CPU</span><span class="field-value">{{.}}</span></div>{{end}}
  {{if .Device.Resources.MemoryBytes}}<div class="field"><span class="field-label">Memory</span><span class="field-value">{{formatBytes .Device.Resources.MemoryBytes}}</span></div>{{end}}
  {{if .Device.Resources.DiskTotalBytes}}<div class="field"><span class="field-label">Disk</span><span class="field-value">{{formatBytes .Device.Resources.DiskTotalBytes}}</span></div>{{end}}
</div>

<div class="section">
  <div class="section-header" onclick="toggleSection(this)">
    <h2>AI Agents and Tools <span class="count">{{.Summary.AIAgentsAndToolsCount}}</span></h2>
    <span class="toggle">&#9660;</span>
  </div>
  <div class="section-body">
  <table>
    <tr><th>Name</th><th>Version</th><th>Type</th><th>Vendor</th></tr>
    {{if .AITools}}{{range .AITools}}<tr><td>{{.Name}}</td><td>{{.Version}}</td><td><span class="type-badge">{{typeLabel .Type}}</span></td><td>{{.Vendor}}</td></tr>
    {{end}}{{else}}<tr><td colspan="4" style="text-align:center;color:#8a94a6;">None detected</td></tr>{{end}}
  </table>
  </div>
</div>

<div class="section">
  <div class="section-header" onclick="toggleSection(this)">
    <h2>IDE &amp; AI Desktop Apps <span class="count">{{.Summary.IDEInstallationsCount}}</span></h2>
    <span class="toggle">&#9660;</span>
  </div>
  <div class="section-body">
  <table>
    <tr><th>Name</th><th>Version</th><th>Vendor</th><th>Path</th></tr>
    {{if .IDEInstallations}}{{range .IDEInstallations}}<tr><td>{{ideDisplayName .IDEType}}</td><td>{{.Version}}</td><td>{{.Vendor}}</td><td style="color:#8a94a6;font-size:0.85em;">{{.InstallPath}}</td></tr>
    {{end}}{{else}}<tr><td colspan="4" style="text-align:center;color:#8a94a6;">None detected</td></tr>{{end}}
  </table>
  </div>
</div>

<div class="section">
  <div class="section-header" onclick="toggleSection(this)">
    <h2>MCP Servers <span class="count">{{.Summary.MCPConfigsCount}}</span></h2>
    <span class="toggle">&#9660;</span>
  </div>
  <div class="section-body">
  <table>
    <tr><th>Source</th><th>Vendor</th></tr>
    {{if .MCPConfigs}}{{range .MCPConfigs}}<tr><td>{{.ConfigSource}}</td><td>{{.Vendor}}</td></tr>
    {{end}}{{else}}<tr><td colspan="2" style="text-align:center;color:#8a94a6;">None detected</td></tr>{{end}}
  </table>
  </div>
</div>

<div class="section">
  <div class="section-header" onclick="toggleSection(this)">
    <h2>IDE Extensions <span class="count">{{.Summary.IDEExtensionsCount}}</span></h2>
    <span class="toggle collapsed">&#9660;</span>
  </div>
  <div class="section-body collapsed">
  <table>
    <tr><th>Extension ID</th><th>Version</th><th>Publisher</th><th>IDE</th></tr>
    {{if .IDEExtensions}}{{range .IDEExtensions}}<tr><td>{{.ID}}</td><td>{{.Version}}</td><td>{{.Publisher}}</td><td>{{.IDEType}}</td></tr>
    {{end}}{{else}}<tr><td colspan="4" style="text-align:center;color:#8a94a6;">None detected</td></tr>{{end}}
  </table>
  </div>
</div>

{{if .NodePkgManagers}}
<div class="section">
  <div class="section-header" onclick="toggleSection(this)">
    <h2>Node.js Projects <span class="count">{{.Summary.NodeProjectsCount}}</span></h2>
    <span class="toggle collapsed">&#9660;</span>
  </div>
  <div class="section-body collapsed">
  <table>
    <tr><th>Package Manager</th><th>Version</th><th>Path</th></tr>
    {{range .NodePkgManagers}}<tr><td>{{.Name}}</td><td>{{.Version}}</td><td style="color:#8a94a6;font-size:0.85em;">{{.Path}}</td></tr>
    {{end}}
  </table>
  {{if .NodeProjects}}
  <table style="margin-top:12px;">
    <tr><th>Project Path</th><th>PM</th><th>Packages</th></tr>
    {{range .NodeProjects}}<tr class="project-row"><td>{{.Path}}</td><td>{{.PackageManager}}</td><td>{{len .Packages}}</td></tr>
    {{range .Packages}}<tr class="pkg-row"><td>{{.Name}}</td><td colspan="2">{{.Version}}</td></tr>
    {{end}}{{end}}
  </table>
  {{end}}
  </div>
</div>
{{end}}

{{if .BrewPkgManager}}
<div class="section">
  <div class="section-header" onclick="toggleSection(this)">
    <h2>Homebrew <span class="count">{{add .Summary.BrewFormulaeCount .Summary.BrewCasksCount}} packages</span></h2>
    <span class="toggle collapsed">&#9660;</span>
  </div>
  <div class="section-body collapsed">
  <p style="margin-bottom:12px;color:#8a94a6;">Homebrew v{{.BrewPkgManager.Version}} &middot; {{.BrewPkgManager.Path}}</p>
  {{if .BrewFormulae}}
  <h3 style="font-size:0.95em;color:#7037f5;margin:8px 0;">Formulae ({{.Summary.BrewFormulaeCount}})</h3>
  <table>
    <tr><th>Name</th><th>Version</th></tr>
    {{range .BrewFormulae}}<tr><td>{{.Name}}</td><td>{{.Version}}</td></tr>
    {{end}}
  </table>
  {{end}}
  {{if .BrewCasks}}
  <h3 style="font-size:0.95em;color:#7037f5;margin:12px 0 8px;">Casks ({{.Summary.BrewCasksCount}})</h3>
  <table>
    <tr><th>Name</th><th>Version</th></tr>
    {{range .BrewCasks}}<tr><td>{{.Name}}</td><td>{{.Version}}</td></tr>
    {{end}}
  </table>
  {{end}}
  </div>
</div>
{{end}}

{{if .PythonPkgManagers}}
<div class="section">
  <div class="section-header" onclick="toggleSection(this)">
    <h2>Python <span class="count">{{.Summary.PythonProjectsCount}} venvs</span></h2>
    <span class="toggle">&#9660;</span>
  </div>
  <div class="section-body">
  <table>
    <tr><th>Package Manager</th><th>Version</th><th>Path</th></tr>
    {{range .PythonPkgManagers}}<tr><td>{{.Name}}</td><td>{{.Version}}</td><td style="color:#8a94a6;font-size:0.85em;">{{.Path}}</td></tr>
    {{end}}
  </table>
  {{if .PythonPackages}}
  <h3 style="font-size:0.95em;color:#7037f5;margin:12px 0 8px;">Global Packages ({{len .PythonPackages}})</h3>
  <table>
    <tr><th>Package</th><th>Version</th></tr>
    {{range .PythonPackages}}<tr><td>{{.Name}}</td><td>{{.Version}}</td></tr>
    {{end}}
  </table>
  {{end}}
  {{if .PythonProjects}}
  <h3 style="font-size:0.95em;color:#7037f5;margin:12px 0 8px;">Virtual Environment Projects ({{.Summary.PythonProjectsCount}})</h3>
  <table>
    <tr><th>Project Path</th><th>PM</th><th>Packages</th></tr>
    {{range .PythonProjects}}<tr class="project-row"><td>{{.Path}}</td><td>{{.PackageManager}}</td><td>{{len .Packages}}</td></tr>
    {{range .Packages}}<tr class="pkg-row"><td>{{.Name}}</td><td colspan="2">{{.Version}}</td></tr>
    {{end}}{{end}}
  </table>
  {{end}}
  </div>
</div>
{{end}}

</div>
<div class="footer">
  Generated by <a href="https://github.com/step-security/dev-machine-guard">StepSecurity Dev Machine Guard</a> v{{.Version}}
</div>
<script>
function toggleSection(header) {
  var body = header.nextElementSibling;
  var toggle = header.querySelector('.toggle');
  body.classList.toggle('collapsed');
  toggle.classList.toggle('collapsed');
}
</script>
</body>
</html>`
