using System;
using UserAuthApp.BLL;  // Reference to Business Logic Layer

namespace UserAuthApp.Web
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // If user is already logged in, send them to the Dashboard
            if (Session["Username"] != null)
            {
                Response.Redirect("Dashboard.aspx");
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            // Don't proceed if client-side validation failed
            if (!Page.IsValid)
                return;

            try
            {
                // ── STEP A: Collect data from the form into a UserEntity ──
                UserEntity user = new UserEntity
                {
                    FirstName = txtFirstName.Text.Trim(),
                    LastName = txtLastName.Text.Trim(),
                    Email = txtEmail.Text.Trim(),
                    Username = txtUsername.Text.Trim(),
                    Password = txtPassword.Text  // BLL will hash this
                };

                // ── STEP B: Call the Business Logic Layer ──
                UserBLL userBLL = new UserBLL();
                int result = userBLL.RegisterUser(user);

                // ── STEP C: Display the appropriate message ──
                switch (result)
                {
                    case 1:
                        lblMessage.Text = "✓ Account created successfully! You can now sign in.";
                        lblMessage.CssClass = "message success";
                        ClearForm();
                        break;

                    case -1:
                        lblMessage.Text = "✗ That username is already taken. Please choose another.";
                        lblMessage.CssClass = "message error";
                        break;

                    case -2:
                        lblMessage.Text = "✗ That email is already registered. Try signing in instead.";
                        lblMessage.CssClass = "message error";
                        break;

                    case -3:
                        lblMessage.Text = "✗ Please fill in all fields correctly. Password must be at least 6 characters.";
                        lblMessage.CssClass = "message error";
                        break;

                    default:
                        lblMessage.Text = "✗ Something went wrong. Please try again.";
                        lblMessage.CssClass = "message error";
                        break;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = "✗ Error: " + ex.Message;
                lblMessage.CssClass = "message error";
            }
        }

        // Helper method to clear all form fields after successful registration
        private void ClearForm()
        {
            txtFirstName.Text = string.Empty;
            txtLastName.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtUsername.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtConfirmPassword.Text = string.Empty;
        }
    }
}
