package com.nisum.dao;

import com.nisum.model.User;
import com.nisum.util.DBConnection;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDAO {


    public User getById(int userId) throws SQLException {

        String sql = "SELECT * FROM Users WHERE UserID = ?";

        try (Connection conn = DBConnection.get();

             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setInt(1, userId);

            try (ResultSet rs = ps.executeQuery()) {

                if (rs.next()) {

                    return mapUser(rs);

                }

            }

        }

        return null;

    }

    /** Fetch a user by their email (for login). */

    public User getByEmail(String email) throws SQLException {

        String sql = "SELECT * FROM Users WHERE Email = ?";

        try (Connection conn = DBConnection.get();

             PreparedStatement ps = conn.prepareStatement(sql)) {

            ps.setString(1, email);

            try (ResultSet rs = ps.executeQuery()) {

                if (rs.next()) {

                    return mapUser(rs);

                }

            }

        }

        return null;

    }

    /** Create a new user; returns generated UserID. */

    public int create(User user) throws SQLException {

        String sql = "INSERT INTO Users (FirstName, LastName, Password, Email, PhoneNumber) "

                + "VALUES (?, ?, ?, ?, ?)";

        try (Connection conn = DBConnection.get();

             PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {

            ps.setString(1, user.getFirstName());

            ps.setString(2, user.getLastName());

            ps.setString(3, user.getPassword());

            ps.setString(4, user.getEmail());

            ps.setString(5, user.getPhoneNumber());

            ps.executeUpdate();

            try (ResultSet keys = ps.getGeneratedKeys()) {

                if (keys.next()) {

                    return keys.getInt(1);

                }

            }

        }

        throw new SQLException("User creation failed, no ID obtained.");

    }

    /** List all users (for admin views). */

    public List<User> listAll() throws SQLException {

        List<User> list = new ArrayList<>();

        String sql = "SELECT * FROM Users";

        try (Connection conn = DBConnection.get();

             PreparedStatement ps = conn.prepareStatement(sql);

             ResultSet rs = ps.executeQuery()) {

            while (rs.next()) {

                list.add(mapUser(rs));

            }

        }

        return list;

    }

    /** Map a result‑set row to a User POJO. */

    private User mapUser(ResultSet rs) throws SQLException {

        return new User(

                rs.getInt("UserID"),

                rs.getString("FirstName"),

                rs.getString("LastName"),

                rs.getString("Password"),

                rs.getString("Email"),

                rs.getString("PhoneNumber")

        );

    }

}


