<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Felhom Hub — {{.Config.CustomerID}}</title>
    <link rel="stylesheet" href="/style.css">
</head>
<body>
    <div class="container">
        <header>
            <h1>Felhom Hub</h1>
            <nav class="nav-links">
                <a href="/" class="nav-link">Dashboard</a>
                <a href="/configs" class="nav-link active">Customers</a>
                <a href="/apps" class="nav-link">Apps</a>
                <a href="/configuration" class="nav-link">Configuration</a>
            </nav>
        </header>

        <a href="/configs" class="back-link">&larr; All customers</a>

        {{if .Flash}}
        <div class="flash flash-success">
            {{if eq .Flash "created"}}Configuration created successfully.
            {{else if eq .Flash "updated"}}Configuration updated.
            {{else if eq .Flash "password_regenerated"}}Retrieval password regenerated.
            {{end}}
        </div>
        {{end}}

        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem;">
            <h2 style="margin: 0;">
                <code>{{.Config.CustomerID}}</code>
                {{if .Config.CustomerName}}<span class="text-muted" style="font-weight: 400;"> — {{.Config.CustomerName}}</span>{{end}}
            </h2>
            <div style="display: flex; gap: 0.5rem;">
                <a href="/configs/{{.Config.CustomerID}}/edit" class="btn btn-outline">Edit</a>
                <form method="POST" action="/configs/{{.Config.CustomerID}}/delete" style="display:inline"
                    onsubmit="return confirm('Delete configuration for {{.Config.CustomerID}}? This cannot be undone.')">
                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </div>
        </div>

        <div class="card">
            <h2>Customer Details</h2>
            <div class="info-grid">
                <div class="info-item">
                    <span class="label">Customer ID</span>
                    <span class="value"><code>{{.Config.CustomerID}}</code></span>
                </div>
                <div class="info-item">
                    <span class="label">Name</span>
                    <span class="value">{{if .Config.CustomerName}}{{.Config.CustomerName}}{{else}}—{{end}}</span>
                </div>
                <div class="info-item">
                    <span class="label">Domain</span>
                    <span class="value">{{if .Config.Domain}}{{.Config.Domain}}{{else}}—{{end}}</span>
                </div>
                <div class="info-item">
                    <span class="label">Email</span>
                    <span class="value">{{if .Config.Email}}{{.Config.Email}}{{else}}—{{end}}</span>
                </div>
                <div class="info-item">
                    <span class="label">Created</span>
                    <span class="value">{{timeAgo .Config.CreatedAt}}</span>
                </div>
                <div class="info-item">
                    <span class="label">Updated</span>
                    <span class="value">{{timeAgo .Config.UpdatedAt}}</span>
                </div>
            </div>
        </div>

        <div class="card">
            <h2>Credentials</h2>
            <div class="credential-row">
                <div>
                    <span class="label">Retrieval Password</span>
                    <div class="credential-box">
                        <code id="retrieval-pw">{{.Config.RetrievalPassword}}</code>
                        <button type="button" class="copy-btn" onclick="copyText('retrieval-pw')" title="Copy">&#x2398;</button>
                    </div>
                </div>
                <form method="POST" action="/configs/{{.Config.CustomerID}}/regen-password" style="margin-top: 0.5rem;"
                    onsubmit="return confirm('Regenerate retrieval password? The old password will stop working immediately.')">
                    <button type="submit" class="btn btn-outline btn-sm">Regenerate</button>
                </form>
            </div>
            <div class="credential-row" style="margin-top: 1rem;">
                <div>
                    <span class="label">API Key</span>
                    <div class="credential-box">
                        <code id="api-key">{{.Config.APIKey}}</code>
                        <button type="button" class="copy-btn" onclick="copyText('api-key')" title="Copy">&#x2398;</button>
                    </div>
                </div>
                <span class="form-hint">Used by the controller for ongoing hub communication (reports, notifications, backups)</span>
            </div>
        </div>

        <div class="card">
            <h2>Setup Commands</h2>
            <p class="text-muted" style="margin-bottom: 1rem; font-size: 0.85rem;">Use one of these methods to configure a customer node:</p>

            <h3>Option 1: docker-setup.sh (recommended)</h3>
            <div class="credential-box">
                <code id="cmd-setup">sudo ./docker-setup.sh --hub-customer {{.Config.CustomerID}} --hub-password {{.Config.RetrievalPassword}}</code>
                <button type="button" class="copy-btn" onclick="copyText('cmd-setup')" title="Copy">&#x2398;</button>
            </div>

            <h3 style="margin-top: 1rem;">Option 2: Direct download</h3>
            <div class="credential-box">
                <code id="cmd-curl">curl -fsSL https://hub.felhom.eu/api/v1/config/{{.Config.CustomerID}} -H "X-Retrieval-Password: {{.Config.RetrievalPassword}}" -o controller.yaml</code>
                <button type="button" class="copy-btn" onclick="copyText('cmd-curl')" title="Copy">&#x2398;</button>
            </div>
        </div>

        <div class="card">
            <h2>YAML Preview</h2>
            <div id="yaml-preview" class="yaml-preview">
                <p class="text-muted">Loading preview...</p>
            </div>
        </div>

        <footer>
            <p>Felhom Hub {{hubVersion}} — Customer Management</p>
        </footer>
    </div>

    <script>
    function copyText(elementId) {
        const el = document.getElementById(elementId);
        const text = el.textContent || el.innerText;
        navigator.clipboard.writeText(text.trim()).then(function() {
            const btn = el.parentElement.querySelector('.copy-btn');
            const orig = btn.innerHTML;
            btn.innerHTML = '&#x2713;';
            setTimeout(function() { btn.innerHTML = orig; }, 1500);
        });
    }

    // Load YAML preview
    fetch('/configs/{{.Config.CustomerID}}/preview')
        .then(function(r) { return r.text(); })
        .then(function(yaml) {
            document.getElementById('yaml-preview').innerHTML = '<pre>' + yaml.replace(/&/g,'&amp;').replace(/</g,'&lt;') + '</pre>';
        })
        .catch(function() {
            document.getElementById('yaml-preview').innerHTML = '<p class="text-muted">Failed to load preview.</p>';
        });
    </script>
</body>
</html>
