@page "/setup-assistant"
@inject IOllamaSetupService SetupService
@inject ISettingsService SettingsService
@inject OperationState OpState
@inject ILocalizationService Loc
@inject OllamaSettings CurrentSettings

<PageTitle>Ollama Setup Assistent</PageTitle>

<div class="setup-assistant-container">
    <div class="setup-header">
        <h1>Ollama Setup Assistent</h1>
        <p>Prüfen Sie Ihre Einstellungen für eine optimale Performance.</p>
    </div>

    @if (recommendation == null)
    {
        <div class="setup-content centered">
            <button class="btn btn-primary" @onclick="RunAnalysis">Analyse starten</button>
        </div>
    }
    else
    {
        <div class="setup-content">
            <div class="status-card @(recommendation.IsCorrect ? "status-success" : "status-warning")">
                <h3>@recommendation.Message</h3>
            </div>

            <div class="analysis-details">
                <h4>Analyse-Ergebnisse:</h4>
                <ul>
                    @foreach (var finding in recommendation.Findings)
                    {
                        <li>@finding</li>
                    }
                </ul>
            </div>

            @if (!recommendation.IsCorrect)
            {
                <div class="recommendation-table">
                    <h4>Empfohlene Einstellungen:</h4>
                    <table>
                        <thead>
                            <tr>
                                <th>Einstellung</th>
                                <th>Aktuell</th>
                                <th>Empfohlen</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>Ollama URL</td>
                                <td>@CurrentSettings.OllamaUrl</td>
                                <td>@recommendation.RecommendedSettings.OllamaUrl</td>
                            </tr>
                            <tr>
                                <td>Style Model</td>
                                <td>@CurrentSettings.StyleModel</td>
                                <td>@recommendation.RecommendedSettings.StyleModel</td>
                            </tr>
                            <tr>
                                <td>Embedding Model</td>
                                <td>@CurrentSettings.EmbedModel</td>
                                <td>@recommendation.RecommendedSettings.EmbedModel</td>
                            </tr>
                            <tr>
                                <td>Spelling Model</td>
                                <td>@CurrentSettings.SpellingModel</td>
                                <td>@recommendation.RecommendedSettings.SpellingModel</td>
                            </tr>
                        </tbody>
                    </table>
                    
                    <div class="setup-actions">
                        <button class="btn btn-primary" @onclick="ApplySettings">Einstellungen übernehmen</button>
                    </div>
                </div>
            }
        </div>
    }
</div>

<style>
    .setup-assistant-container {
        max-width: 800px;
        margin: 2rem auto;
        padding: 2rem;
        background: var(--bg-card);
        border-radius: 8px;
        box-shadow: var(--shadow);
        color: var(--text-main);
    }
    .setup-header {
        text-align: center;
        margin-bottom: 2rem;
    }
    .setup-content.centered {
        display: flex;
        justify-content: center;
        padding: 3rem;
    }
    .status-card {
        padding: 1rem;
        border-radius: 8px;
        margin-bottom: 2rem;
        text-align: center;
    }
    .status-success {
        background: var(--bg-success);
        color: var(--text-success);
        border: 1px solid var(--border-success);
    }
    .status-warning {
        background: var(--bg-warning);
        color: var(--text-warning);
        border: 1px solid var(--border-warning);
    }
    .analysis-details {
        margin-bottom: 2rem;
    }
    .analysis-details ul {
        list-style: disc;
        padding-left: 1.5rem;
    }
    .recommendation-table table {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 2rem;
    }
    .recommendation-table th, .recommendation-table td {
        padding: 0.75rem;
        text-align: left;
        border-bottom: 1px solid var(--border);
    }
    .setup-actions {
        display: flex;
        gap: 1rem;
        justify-content: center;
    }
</style>

@code {
    private SetupRecommendation? recommendation;
    private bool _applying;

    private async Task RunAnalysis()
    {
        recommendation = await SetupService.AnalyzeSettingsAsync();
    }

    private async Task ApplySettings()
    {
        if (recommendation?.RecommendedSettings == null || _applying) return;
        _applying = true;

        try
        {
            var rec = recommendation.RecommendedSettings;

            await SettingsService.SaveOllamaUrlAsync(rec.OllamaUrl);
            await SettingsService.SaveStyleModelAsync(rec.StyleModel);
            await SettingsService.SaveRagModelAsync(rec.EmbedModel);
            await SettingsService.SaveSpellingModelAsync(rec.SpellingModel);

            OpState.SetStatus("Einstellungen wurden in der Datenbank gespeichert.", true);
            recommendation = await SetupService.AnalyzeSettingsAsync();
        }
        finally
        {
            _applying = false;
        }
    }
}
