using System;
using System.Data;
using UserAuthApp.DAL;  // Reference to the Data Access Layer

namespace UserAuthApp.BLL
{
    public class UserBLL
    {
        // Create an instance of the DAL class
        private readonly UserDAL _userDAL;

        public UserBLL()
        {
            _userDAL = new UserDAL();
        }

        // ─────────────────────────────────────────────
        // METHOD: Register a new user
        // Returns: 1 = Success, -1 = Username exists, 
        //         -2 = Email exists, -3 = Validation failed
        // ─────────────────────────────────────────────
        public int RegisterUser(UserEntity user)
        {
            // ── BUSINESS RULE: All fields are required ──
            if (string.IsNullOrWhiteSpace(user.FirstName) ||
                string.IsNullOrWhiteSpace(user.LastName) ||
                string.IsNullOrWhiteSpace(user.Email) ||
                string.IsNullOrWhiteSpace(user.Username) ||
                string.IsNullOrWhiteSpace(user.Password))
            {
                return -3; // Validation failed
            }

            // ── BUSINESS RULE: Password must be at least 6 characters ──
            if (user.Password.Length < 6)
            {
                return -3;
            }

            // ── BUSINESS RULE: Email must be valid format ──
            if (!IsValidEmail(user.Email))
            {
                return -3;
            }

            // ── Hash the password BEFORE sending to database ──
            string passwordHash = PasswordHelper.HashPassword(user.Password);

            // ── Call the DAL to save to database ──
            return _userDAL.RegisterUser(
                user.FirstName,
                user.LastName,
                user.Email,
                user.Username,
                passwordHash
            );
        }

        // ─────────────────────────────────────────────
        // METHOD: Authenticate user login
        // Returns: 1 = Success, -1 = Invalid credentials
        // ─────────────────────────────────────────────
        public int LoginUser(string username, string password)
        {
            // Basic validation
            if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
            {
                return -1;
            }

            // Hash the password (we compare hashes, not plain text)
            string passwordHash = PasswordHelper.HashPassword(password);

            // Call DAL
            return _userDAL.LoginUser(username, passwordHash);
        }

        // ─────────────────────────────────────────────
        // METHOD: Get user details for the dashboard
        // ─────────────────────────────────────────────
        public DataTable GetUserDetails(string username)
        {
            return _userDAL.GetUserByUsername(username);
        }

        // ─────────────────────────────────────────────
        // HELPER: Validate email format
        // ─────────────────────────────────────────────
        private bool IsValidEmail(string email)
        {
            try
            {
                var addr = new System.Net.Mail.MailAddress(email);
                return addr.Address == email;
            }
            catch
            {
                return false;
            }
        }
    }
}
