using BCrypt.Net;
using MySql.Data.MySqlClient;
using PdfSharp.Fonts;
using System.Globalization;
using System.Reflection;
using System.Windows;
using static SkyTeam.BuchungenPage;

namespace SkyTeam
{
    public partial class App : Application
    {
        // Beim Start der Anwendung einen Standard Admin-Benutzer erstellen, falls keiner existiert , selbGedacht.
        public App()
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("de");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("de");
            GlobalFontSettings.FontResolver = new CustomFontResolver();
        }

        private void CreateDefaultAdmin()
        {
            string connectionString = DatenbankServices.GetConnection(); 

            try
            {
                using (MySqlConnection conn = new MySqlConnection(connectionString))
                {
                    conn.Open();

                    string checkQuery = "SELECT COUNT(*) FROM users WHERE Rolle = 'Admin'";
                    MySqlCommand checkCmd = new MySqlCommand(checkQuery, conn);
                    long count = (long)checkCmd.ExecuteScalar();

                    if (count == 0)
                    {
                        string hashedPassword = BCrypt.Net.BCrypt.HashPassword("admin");

                        string insertQuery = @"
                            INSERT INTO users (Vorname, Nachname, Email, PasswortHash, Rolle, Stadt, CreatedAt) 
                            VALUES ('System', 'Root', 'admin@skyteam.com', @hash, 'Admin', 'HQ', NOW())";

                        MySqlCommand insertCmd = new MySqlCommand(insertQuery, conn);
                        insertCmd.Parameters.AddWithValue("@hash", hashedPassword);
                        insertCmd.ExecuteNonQuery();

                        MessageBox.Show("Ein Standard-Admin wurde erstellt!\nEmail: admin@skyteam.com\nPasswort: admin");
                    }
                }
            }
            catch(Exception ex) { 
            
            MessageBox.Show("Fehler beim Erstellen des Standard Admins: " + ex.Message);
            }
        }
    }
}