using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Watcher.Data;
using Watcher.ViewModels;

namespace Watcher.Controllers;

public class UserController : Controller
{

    private readonly AppDbContext _context;
    private readonly ILogger<UserController> _logger;

    public UserController(AppDbContext context, ILogger<UserController> logger)
    {
        _context = context;
        _logger = logger;
    }


    // Anzeigen der User-Informationen
    [Authorize]
    public async Task<IActionResult> Info()
    {
        var claims = User.Claims.Select(c => new { c.Type, c.Value }).ToList();
        var Identity_User = User.Identity?.Name;

        var user = await _context.Users.FirstOrDefaultAsync(u => u.Username == Identity_User);
        if (user == null) return NotFound();

        // Anzeigedaten
        var DbProvider = _context.Database.ProviderName;
        var username = user.Username;
        var mail = user.Email;
        var Id = user.Id;

        // Anzeigedaten an View übergeben
        ViewBag.Claims = claims;
        ViewBag.Name = username;
        ViewBag.Mail = mail;
        ViewBag.Id = Id;


        return View();
    }

    // Edit-Form anzeigen
    [Authorize]
    [HttpGet]
    public async Task<IActionResult> Edit()
    {
        var username = User.Identity?.Name;
        var user = await _context.Users.FirstOrDefaultAsync(u => u.Username == username);
        if (user == null) return NotFound();


        var model = new EditUserViewModel
        {
            Username = user.Username
        };
        return View(model);
    }

    // Edit speichern
    [Authorize]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(EditUserViewModel model)
    {
        if (!ModelState.IsValid) return View(model);

        var username = User.Identity?.Name;
        var user = await _context.Users.FirstOrDefaultAsync(u => u.Username == username);
        if (user == null) return NotFound();

        user.Username = model.Username;

        if (!string.IsNullOrWhiteSpace(model.NewPassword))
        {
            user.Password = BCrypt.Net.BCrypt.HashPassword(model.NewPassword);
        }

        await _context.SaveChangesAsync();

        // Eventuell hier das Auth-Cookie erneuern, wenn Username sich ändert

        _logger.LogTrace("Passwort-Change durchgeführt");

        return RedirectToAction("Index", "Home");
    }

    // Edit speichern
    [Authorize]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> UserSettings(EditUserViewModel model)
    {
        if (!ModelState.IsValid) return View(model);

        var username = User.Identity?.Name;
        var user = await _context.Users.FirstOrDefaultAsync(u => u.Username == username);
        if (user == null) return NotFound();

        var databaseProvider = _context.Database.ProviderName;

        user.Username = model.Username;

        // Passwort ändern
        if (!string.IsNullOrWhiteSpace(model.NewPassword))
        {
            user.Password = BCrypt.Net.BCrypt.HashPassword(model.NewPassword);
        }

        await _context.SaveChangesAsync();

        // Eventuell hier das Auth-Cookie erneuern, wenn Username sich ändert

        return RedirectToAction("Index", "Home");
    }
}