@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion

echo ============================================
echo    DÜSK WPF Projektstruktur
echo ============================================
echo.

REM Zum Verzeichnis wechseln, in dem die Batch-Datei liegt
cd /d "%~dp0"

REM Hauptverzeichnis
set PROJECT_ROOT=duesk-wpf
set MODELS_DIR=%PROJECT_ROOT%\Models
set SERVICES_DIR=%PROJECT_ROOT%\Services
set VIEWS_DIR=%PROJECT_ROOT%\Views
set CONVERTERS_DIR=%PROJECT_ROOT%\Converters
set RESOURCES_DIR=%PROJECT_ROOT%\Resources

echo Erstelle Projekt in: %CD%\%PROJECT_ROOT%
echo.

REM Lösche vorhandenes Verzeichnis (optional - auskommentieren falls gewünscht)
if exist "%PROJECT_ROOT%" (
    echo Lösche vorhandenes Verzeichnis...
    rmdir /s /q "%PROJECT_ROOT%"
)

echo [1/8] Erstelle Hauptverzeichnisse...
mkdir "%PROJECT_ROOT%" 2>nul
mkdir "%MODELS_DIR%" 2>nul
mkdir "%SERVICES_DIR%" 2>nul
mkdir "%VIEWS_DIR%" 2>nul
mkdir "%CONVERTERS_DIR%" 2>nul
mkdir "%RESOURCES_DIR%" 2>nul

echo [2/8] Erstelle DueskWPF.csproj...
(
echo ^<Project Sdk="Microsoft.NET.Sdk"^>
echo.
echo   ^<PropertyGroup^>
echo     ^<OutputType^>WinExe^</OutputType^>
echo     ^<TargetFramework^>net8.0-windows^</TargetFramework^>
echo     ^<Nullable^>enable^</Nullable^>
echo     ^<UseWPF^>true^</UseWPF^>
echo     ^<UseWindowsForms^>false^</UseWindowsForms^>
echo     ^<ImplicitUsings^>enable^</ImplicitUsings^>
echo     ^<ApplicationIcon^>Resources\duesk.ico^</ApplicationIcon^>
echo     ^<AssemblyName^>DueskWPF^</AssemblyName^>
echo     ^<RootNamespace^>DueskWPF^</RootNamespace^>
echo   ^</PropertyGroup^>
echo.
echo   ^<ItemGroup^>
echo     ^<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /^>
echo     ^<PackageReference Include="LiveCharts.Wpf" Version="0.9.7" /^>
echo     ^<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" /^>
echo   ^</ItemGroup^>
echo.
echo   ^<ItemGroup^>
echo     ^<None Update="Resources\*.ico"^>
echo       ^<CopyToOutputDirectory^>PreserveNewest^</CopyToOutputDirectory^>
echo     ^</None^>
echo   ^</ItemGroup^>
echo.
echo ^</Project^>
) > "%PROJECT_ROOT%\DueskWPF.csproj"

echo [3/8] Erstelle App.xaml...
(
echo ^<Application x:Class="DueskWPF.App"
echo              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
echo              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
echo              StartupUri="Views/LoginWindow.xaml"^>
echo     ^<Application.Resources^>
echo         ^<ResourceDictionary^>
echo             ^<ResourceDictionary.MergedDictionaries^>
echo                 ^<ResourceDictionary Source="Resources/Styles.xaml"/^>
echo             ^</ResourceDictionary.MergedDictionaries^>
echo             ^<SolidColorBrush x:Key="PrimaryBrush" Color="#2196F3"/^>
echo             ^<SolidColorBrush x:Key="PrimaryDarkBrush" Color="#1976D2"/^>
echo             ^<SolidColorBrush x:Key="SecondaryBrush" Color="#FF5722"/^>
echo             ^<SolidColorBrush x:Key="SuccessBrush" Color="#4CAF50"/^>
echo             ^<SolidColorBrush x:Key="ErrorBrush" Color="#F44336"/^>
echo             ^<SolidColorBrush x:Key="WarningBrush" Color="#FF9800"/^>
echo             ^<SolidColorBrush x:Key="InfoBrush" Color="#00BCD4"/^>
echo             ^<FontFamily x:Key="DefaultFont"^>Segoe UI^</FontFamily^>
echo             ^<System:Double x:Key="DefaultFontSize"^>14^</System:Double^>
echo             ^<System:Double x:Key="LargeFontSize"^>18^</System:Double^>
echo             ^<System:Double x:Key="TitleFontSize"^>24^</System:Double^>
echo             ^<BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/^>
echo         ^</ResourceDictionary^>
echo     ^</Application.Resources^>
echo ^</Application^>
) > "%PROJECT_ROOT%\App.xaml"

echo [4/8] Erstelle App.xaml.cs...
(
echo using System.Windows;
echo using DueskWPF.Services;
echo.
echo namespace DueskWPF;
echo.
echo public partial class App : Application
echo {
echo     protected override void OnStartup(StartupEventArgs e)
echo     {
echo         base.OnStartup(e);
echo         SessionManager.Instance.LoadSession();
echo     }
echo }
) > "%PROJECT_ROOT%\App.xaml.cs"

echo [5/8] Erstelle Model-Dateien...

REM Models/LoginResponse.cs
(
echo using Newtonsoft.Json;
echo.
echo namespace DueskWPF.Models;
echo.
echo public class LoginResponse
echo {
echo     [JsonProperty("success")]
echo     public bool Success { get; set; }
echo     
echo     [JsonProperty("userID")]
echo     public string? UserID { get; set; }
echo     
echo     [JsonProperty("session")]
echo     public string? Session { get; set; }
echo     
echo     [JsonProperty("error")]
echo     public string? Error { get; set; }
echo }
) > "%MODELS_DIR%\LoginResponse.cs"

REM Models/Group.cs
(
echo using Newtonsoft.Json;
echo.
echo namespace DueskWPF.Models;
echo.
echo public class Group
echo {
echo     [JsonProperty("gruppeID")]
echo     public int GruppeID { get; set; }
echo     
echo     [JsonProperty("name")]
echo     public string Name { get; set; } = string.Empty;
echo }
) > "%MODELS_DIR%\Group.cs"

REM Models/Norms.cs (gekürzt)
(
echo namespace DueskWPF.Models;
echo.
echo public static class Norms
echo {
echo     public static readonly double[][] NormSE_HS = new[][]
echo     {
echo         new[] { 21.33, 25.33, 29.33, 33.32, 37.32 },
echo         new[] { 20.87, 24.95, 29.03, 33.13, 37.18 },
echo         new[] { 17.93, 21.37, 24.80, 28.23, 31.67 },
echo         new[] { 13.98, 17.71, 21.44, 25.17, 28.90 },
echo         new[] { 24.60, 28.55, 33.04, 37.53, 42.01 },
echo         new[] { 15.53, 18.97, 22.40, 25.83, 29.27 }
echo     };
echo.
echo     public static readonly string[] Competencies = 
echo     {
echo         "Arbeitsverhalten", "Lernverhalten", "Sozialverhalten",
echo         "Fachkompetenz", "Personale Kompetenz", "Methodenkompetenz"
echo     };
echo }
) > "%MODELS_DIR%\Norms.cs"

echo [6/8] Erstelle Service-Dateien...

REM Services/SessionManager.cs
(
echo using System;
echo using System.IO;
echo using System.Text.Json;
echo.
echo namespace DueskWPF.Services;
echo.
echo public class SessionManager
echo {
echo     private static readonly Lazy<SessionManager> _instance = new(() => new SessionManager());
echo     public static SessionManager Instance => _instance.Value;
echo     private const string SessionFile = "session.json";
echo     public string? UserId { get; private set; }
echo     public string? Session { get; private set; }
echo     public bool IsLoggedIn => !string.IsNullOrEmpty(UserId) && !string.IsNullOrEmpty(Session);
echo.
echo     private SessionManager() { }
echo.
echo     public void SetSession(string userId, string session)
echo     {
echo         UserId = userId;
echo         Session = session;
echo         SaveSession();
echo     }
echo.
echo     public void Clear()
echo     {
echo         UserId = null;
echo         Session = null;
echo         if (File.Exists(SessionFile)) File.Delete(SessionFile);
echo     }
echo.
echo     private void SaveSession()
echo     {
echo         var data = new { UserId, Session };
echo         var json = JsonSerializer.Serialize(data);
echo         File.WriteAllText(SessionFile, json);
echo     }
echo.
echo     public void LoadSession()
echo     {
echo         if (!File.Exists(SessionFile)) return;
echo         try
echo         {
echo             var json = File.ReadAllText(SessionFile);
echo             var data = JsonSerializer.Deserialize<Dictionary<string, string>>(json);
echo             if (data != null)
echo             {
echo                 UserId = data.GetValueOrDefault("UserId");
echo                 Session = data.GetValueOrDefault("Session");
echo             }
echo         }
echo         catch { }
echo     }
echo }
) > "%SERVICES_DIR%\SessionManager.cs"

REM Services/ApiService.cs (gekürzt)
(
echo using System.Net.Http;
echo using System.Text;
echo using System.Threading.Tasks;
echo using Newtonsoft.Json;
echo using DueskWPF.Models;
echo.
echo namespace DueskWPF.Services;
echo.
echo public class ApiService
echo {
echo     private static readonly HttpClient _httpClient = new();
echo     private const string BaseUrl = "https://paul-koop.org/api/";
echo.
echo     public async Task<LoginResponse> LoginAsync(string username, string password)
echo     {
echo         var data = new { username, password };
echo         var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
echo         var response = await _httpClient.PostAsync($"{BaseUrl}api_login.php", content);
echo         var json = await response.Content.ReadAsStringAsync();
echo         return JsonConvert.DeserializeObject<LoginResponse>(json) ?? new LoginResponse();
echo     }
echo }
) > "%SERVICES_DIR%\ApiService.cs"

REM Services/Calculator.cs (gekürzt)
(
echo using System;
echo using DueskWPF.Models;
echo.
echo namespace DueskWPF.Services;
echo.
echo public static class Calculator
echo {
echo     public static int[] CalculateSums(System.Collections.Generic.IReadOnlyList<int> items)
echo     {
echo         var sums = new int[7];
echo         for (var i = 0; i < 10; i++) sums[1] += items[i];
echo         for (var i = 10; i < 20; i++) sums[2] += items[i];
echo         for (var i = 20; i < 28; i++) sums[3] += items[i];
echo         sums[3] += items[8] + items[9];
echo         for (var i = 28; i < 36; i++) sums[4] += items[i];
echo         sums[5] = items[0] + items[1] + items[5] + items[6] + items[7] +
echo                   items[8] + items[9] + items[11] + items[12] + items[13] + items[14];
echo         sums[6] = items[2] + items[3] + items[4] + items[8] + items[9] +
echo                   items[10] + items[16] + items[17];
echo         return sums;
echo     }
echo }
) > "%SERVICES_DIR%\Calculator.cs"

echo [7/8] Erstelle View-Dateien (Grundgerüste)...

REM Views/LoginWindow.xaml
(
echo ^<Window x:Class="DueskWPF.Views.LoginWindow"
echo         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
echo         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
echo         Title="DÜSK - Anmeldung" Height="450" Width="400"
echo         WindowStartupLocation="CenterScreen"^>
echo     ^<Grid Margin="30"^>
echo         ^<TextBlock Text="DÜSK - Anmeldung" HorizontalAlignment="Center" VerticalAlignment="Center"/^>
echo     ^</Grid^>
echo ^</Window^>
) > "%VIEWS_DIR%\LoginWindow.xaml"

REM Views/LoginWindow.xaml.cs
(
echo using System.Windows;
echo.
echo namespace DueskWPF.Views;
echo.
echo public partial class LoginWindow : Window
echo {
echo     public LoginWindow()
echo     {
echo         InitializeComponent();
echo     }
echo }
) > "%VIEWS_DIR%\LoginWindow.xaml.cs"

REM Views/MainWindow.xaml
(
echo ^<Window x:Class="DueskWPF.Views.MainWindow"
echo         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
echo         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
echo         Title="DÜSK - Hauptansicht" Height="700" Width="1100"
echo         WindowStartupLocation="CenterScreen"^>
echo     ^<Grid^>
echo         ^<TextBlock Text="DÜSK - Hauptansicht" HorizontalAlignment="Center" VerticalAlignment="Center"/^>
echo     ^</Grid^>
echo ^</Window^>
) > "%VIEWS_DIR%\MainWindow.xaml"

REM Views/MainWindow.xaml.cs
(
echo using System.Windows;
echo.
echo namespace DueskWPF.Views;
echo.
echo public partial class MainWindow : Window
echo {
echo     public MainWindow()
echo     {
echo         InitializeComponent();
echo     }
echo }
) > "%VIEWS_DIR%\MainWindow.xaml.cs"

REM Weitere View-Grundgerüste
echo ^<Window x:Class="DueskWPF.Views.ProfileDetailWindow" ...^>^</Window^> > "%VIEWS_DIR%\ProfileDetailWindow.xaml"
echo namespace DueskWPF.Views; public partial class ProfileDetailWindow : Window { } > "%VIEWS_DIR%\ProfileDetailWindow.xaml.cs"
echo ^<Window x:Class="DueskWPF.Views.ProfileEditWindow" ...^>^</Window^> > "%VIEWS_DIR%\ProfileEditWindow.xaml"
echo namespace DueskWPF.Views; public partial class ProfileEditWindow : Window { } > "%VIEWS_DIR%\ProfileEditWindow.xaml.cs"
echo ^<Window x:Class="DueskWPF.Views.GroupManagerWindow" ...^>^</Window^> > "%VIEWS_DIR%\GroupManagerWindow.xaml"
echo namespace DueskWPF.Views; public partial class GroupManagerWindow : Window { } > "%VIEWS_DIR%\GroupManagerWindow.xaml.cs"
echo ^<Window x:Class="DueskWPF.Views.TimeSeriesWindow" ...^>^</Window^> > "%VIEWS_DIR%\TimeSeriesWindow.xaml"
echo namespace DueskWPF.Views; public partial class TimeSeriesWindow : Window { } > "%VIEWS_DIR%\TimeSeriesWindow.xaml.cs"

echo [8/8] Erstelle Konverter und Styles...

REM Converters/BoolToVisibilityConverter.cs
(
echo using System;
echo using System.Globalization;
echo using System.Windows;
echo using System.Windows.Data;
echo.
echo namespace DueskWPF.Converters;
echo.
echo [ValueConversion(typeof(bool), typeof(Visibility))]
echo public class BoolToVisibilityConverter : IValueConverter
echo {
echo     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
echo     {
echo         if (value is bool boolValue)
echo         {
echo             if (parameter?.ToString() == "Inverse")
echo                 return boolValue ? Visibility.Collapsed : Visibility.Visible;
echo             return boolValue ? Visibility.Visible : Visibility.Collapsed;
echo         }
echo         return Visibility.Collapsed;
echo     }
echo.
echo     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
echo     {
echo         if (value is Visibility visibility)
echo         {
echo             if (parameter?.ToString() == "Inverse")
echo                 return visibility != Visibility.Visible;
echo             return visibility == Visibility.Visible;
echo         }
echo         return false;
echo     }
echo }
) > "%CONVERTERS_DIR%\BoolToVisibilityConverter.cs"

REM Resources/Styles.xaml
(
echo ^<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
echo                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"^>
echo     ^<Style TargetType="Button"^>
echo         ^<Setter Property="FontFamily" Value="Segoe UI"/^>
echo         ^<Setter Property="FontSize" Value="13"/^>
echo         ^<Setter Property="Padding" Value="10,6"/^>
echo         ^<Setter Property="Cursor" Value="Hand"/^>
echo     ^</Style^>
echo ^</ResourceDictionary^>
) > "%RESOURCES_DIR%\Styles.xaml"

echo.
echo ============================================
echo    Projektstruktur erfolgreich erstellt!
echo ============================================
echo.
echo Projektverzeichnis: %CD%\%PROJECT_ROOT%
echo.
echo Verzeichnisstruktur:
echo %PROJECT_ROOT%\
echo ├── DueskWPF.csproj
echo ├── App.xaml
echo ├── App.xaml.cs
echo ├── Models\
echo │   ├── LoginResponse.cs
echo │   ├── Group.cs
echo │   └── Norms.cs
echo ├── Services\
echo │   ├── SessionManager.cs
echo │   ├── ApiService.cs
echo │   └── Calculator.cs
echo ├── Views\
echo │   ├── LoginWindow.xaml
echo │   ├── LoginWindow.xaml.cs
echo │   ├── MainWindow.xaml
echo │   ├── MainWindow.xaml.cs
echo │   ├── ProfileDetailWindow.xaml
echo │   ├── ProfileDetailWindow.xaml.cs
echo │   ├── ProfileEditWindow.xaml
echo │   ├── ProfileEditWindow.xaml.cs
echo │   ├── GroupManagerWindow.xaml
echo │   ├── GroupManagerWindow.xaml.cs
echo │   ├── TimeSeriesWindow.xaml
echo │   └── TimeSeriesWindow.xaml.cs
echo ├── Converters\
echo │   └── BoolToVisibilityConverter.cs
echo └── Resources\
echo     └── Styles.xaml
echo.
echo ============================================
echo.
echo Nächste Schritte:
echo ============================================
echo.
echo 1. Wechseln Sie in das Projektverzeichnis:
echo    cd %CD%\%PROJECT_ROOT%
echo.
echo 2. Öffnen Sie das Projekt in Visual Studio:
echo    start DueskWPF.csproj
echo.
echo 3. Stellen Sie die NuGet-Pakete wieder her:
echo    dotnet restore
echo.
echo 4. Bauen Sie das Projekt:
echo    dotnet build
echo.
echo 5. Starten Sie die App:
echo    dotnet run
echo.
echo Hinweis: Die View-Dateien enthalten nur Grundgerüste.
echo         Der vollständige Code muss aus der Dokumentation kopiert werden.
echo.
echo Druecken Sie eine beliebige Taste zum Beenden...
pause > nul