using System;
using System.Data;
using System.Data.SqlClient;

namespace UserAuthApp.DAL
{
    public class UserDAL
    {
        // Store the connection string
        private readonly string _connectionString;

        // Constructor — gets the connection string when this class is created
        public UserDAL()
        {
            _connectionString = ConnectionHelper.GetConnectionString();
        }

        // ─────────────────────────────────────────────
        // METHOD: Register a new user
        // Returns: 1 = Success, -1 = Username exists, -2 = Email exists
        // ─────────────────────────────────────────────
        public int RegisterUser(string firstName, string lastName,
                                string email, string username, string passwordHash)
        {
            int result = 0;

            // 'using' ensures the connection is properly closed even if an error occurs
            using (SqlConnection conn = new SqlConnection(_connectionString))
            {
                // Create a command that calls our stored procedure
                using (SqlCommand cmd = new SqlCommand("sp_RegisterUser", conn))
                {
                    // Tell it we're calling a stored procedure (not a raw SQL query)
                    cmd.CommandType = CommandType.StoredProcedure;

                    // Add the input parameters (must match stored procedure parameter names)
                    cmd.Parameters.AddWithValue("@FirstName", firstName);
                    cmd.Parameters.AddWithValue("@LastName", lastName);
                    cmd.Parameters.AddWithValue("@Email", email);
                    cmd.Parameters.AddWithValue("@Username", username);
                    cmd.Parameters.AddWithValue("@PasswordHash", passwordHash);

                    // Add the OUTPUT parameter to receive the result
                    SqlParameter outputParam = new SqlParameter("@Result", SqlDbType.Int);
                    outputParam.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(outputParam);

                    // Open connection, execute, then read the output
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    result = Convert.ToInt32(outputParam.Value);
                }
            }
            // Connection is automatically closed here

            return result;
        }

        // ─────────────────────────────────────────────
        // METHOD: Authenticate a user (Login)
        // Returns: 1 = Success, -1 = Invalid credentials
        // ─────────────────────────────────────────────
        public int LoginUser(string username, string passwordHash)
        {
            int result = 0;

            using (SqlConnection conn = new SqlConnection(_connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("sp_LoginUser", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.AddWithValue("@Username", username);
                    cmd.Parameters.AddWithValue("@PasswordHash", passwordHash);

                    SqlParameter outputParam = new SqlParameter("@Result", SqlDbType.Int);
                    outputParam.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(outputParam);

                    conn.Open();
                    cmd.ExecuteNonQuery();
                    result = Convert.ToInt32(outputParam.Value);
                }
            }

            return result;
        }

        // ─────────────────────────────────────────────
        // METHOD: Get user details by username
        // Returns: A DataTable with user information
        // ─────────────────────────────────────────────
        public DataTable GetUserByUsername(string username)
        {
            DataTable dt = new DataTable();

            using (SqlConnection conn = new SqlConnection(_connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("sp_GetUserByUsername", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Username", username);

                    conn.Open();

                    // SqlDataAdapter fills a DataTable with the results
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.Fill(dt);
                    }
                }
            }

            return dt;
        }
    }
}
