using System;
using System.Data;
using UserAuthApp.BLL;

namespace UserAuthApp.Web
{
    public partial class Dashboard : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // ── SECURITY CHECK: If not logged in, redirect to Login ──
            if (Session["Username"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }

            // Only load data on the first page load (not on postbacks)
            if (!IsPostBack)
            {
                LoadUserDetails();
            }
        }

        private void LoadUserDetails()
        {
            // Get the logged-in username from the Session
            string username = Session["Username"].ToString();

            // Call BLL to get user details
            UserBLL userBLL = new UserBLL();
            DataTable dt = userBLL.GetUserDetails(username);

            // Display the user's information
            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                lblWelcome.Text = "Welcome, " + row["FirstName"].ToString()
                                  + " " + row["LastName"].ToString() + "!";
                lblEmail.Text = row["Email"].ToString();
                lblUsername.Text = row["Username"].ToString();
                lblMemberSince.Text = Convert.ToDateTime(row["CreatedDate"])
                                      .ToString("MMMM dd, yyyy");
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            // Clear the session (log out)
            Session.Clear();
            Session.Abandon();

            // Redirect back to Login page
            Response.Redirect("Login.aspx");
        }
    }
}
