package backend.dao.impl;

import backend.dao.interfaces.UserDaoIF;
import backend.model.User;
import backend.utils.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDao implements UserDaoIF {

    @Override
    public User findByUsername(String username) {
        User user = null;
        //1. 从连接池中创建数据库连接
        Connection conn = DBHelper.getInstance().getConnection();
        //2. 创建Statement对象
        String sql = "select username,password,role,email,createdAt from USER where username = ?";
        try {
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, username);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                user = new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setRole(rs.getString("role"));
                user.setEmail(rs.getString("email"));
                user.setCreatedAt(rs.getTimestamp("createdAt"));
            }
        } catch (SQLException e) {
            System.out.println("查询用户失败: " + e.getMessage());
        }
        return user;
    }
    @Override
    public User findById(int id) {
        User user = null;
        //1. 从连接池中创建数据库连接
        Connection conn = DBHelper.getInstance().getConnection();
        //2. 创建Statement对象
        String sql = "select username,password,role,email,createdAt from USER where id = ?";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1,id);
            ResultSet rs = stmt.executeQuery();
            while(rs.next()) {
                user = new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setRole(rs.getString("role"));
                user.setEmail(rs.getString("email"));
                user.setCreatedAt(rs.getTimestamp("createdAt"));
            }
        }catch (SQLException e) {
            System.out.println("查询用户失败: " + e.getMessage());
        }
        return user;
    }

    @Override
    public boolean addUser(User user) {
        Connection conn = DBHelper.getInstance().getConnection();
        String sql = "insert into USER (username,password,role,email,createdAt) values (?,?,?,?,?)";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,user.getUsername());
            stmt.setString(2,user.getPassword());
            stmt.setString(3,user.getRole());
            stmt.setString(4,user.getEmail());
            stmt.setTimestamp(5,user.getCreatedAt());
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("添加用户失败: " + e.getMessage());
        }
        return false;
    }

    @Override
    public boolean updateUser(User user) {
        Connection conn = DBHelper.getInstance().getConnection();
        String sql = "update USER set username = ?,password = ?,role = ?,email = ?,createdAt = ? where id = ?";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,user.getUsername());
            stmt.setString(2,user.getPassword());
            stmt.setString(3,user.getRole());
            stmt.setString(4,user.getEmail());
            stmt.setTimestamp(5,user.getCreatedAt());
            stmt.setInt(6,user.getId());
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("更新用户失败: " + e.getMessage());
        }
        return false;
    }

    @Override
    public boolean deleteUser(int id) {
        Connection conn = DBHelper.getInstance().getConnection();
        String sql = "delete from USER where id = ?";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1,id);
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("删除用户失败: " + e.getMessage());
        }
        return false;
    }

    @Override
    public List<User> findAll() {
        Connection conn = DBHelper.getInstance().getConnection();
        String sql = "select id,username,password,role,email,createdAt from USER";
        try {
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setRole(rs.getString("role"));
                user.setEmail(rs.getString("email"));
                user.setCreatedAt(rs.getTimestamp("createdAt"));
                return List.of(user);
            }
        }catch (SQLException e) {
            System.out.println("查询所有用户失败: " + e.getMessage());
        }
        return List.of();
    }
}
