using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using administration.Models;

namespace administration.Controllers;

public class HomeController : Controller
{
    private readonly FinancesContext _context;

    public HomeController(FinancesContext context)
    {
        _context = context;
    }

    public IActionResult Index()
    {
        // Récupère un utilisateur fictif pour l’exemple
        var user = _context.Users.Where(X => X.Id == 2).First(); // à remplacer par un filtre réel (par ex. Email ou Id)

        if (user != null)
        {
            // Stocker dans la session
            HttpContext.Session.SetInt32("UserId", user.Id);
            HttpContext.Session.SetString("UserName", user.Name);
        }
        return View();
    }

    public IActionResult Profile()
    {
        int? userId = HttpContext.Session.GetInt32("UserId");
        string userName = HttpContext.Session.GetString("UserName");

        ViewBag.UserId = userId;
        ViewBag.UserName = userName;

        return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}
