package backend.dao.impl;

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

import java.sql.*;
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(String.valueOf(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(String.valueOf(rs.getTimestamp("createdAt")));
            }
        }catch (SQLException e) {
            System.out.println("查询用户失败: " + e.getMessage());
        }
        return user;
    }

    @Override
    public int addUser(User user) {
        Connection conn = DBHelper.getInstance().getConnection();
        int row=0;
        if(conn==null)
            return 0;
        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.setDate(5,user.getCreatedAt());
            row = stmt.executeUpdate();
            stmt.close();
            DBConnectionPool.releaseConnection(conn);
        } catch (SQLException e) {
            System.out.println("添加用户失败: " + e.getMessage());
        }
        return row;
    }

    @Override
    public int updateUser(User user) {
        Connection conn = DBHelper.getInstance().getConnection();
        int row = 0;
        if(conn==null)
            return 0;

        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.setDate(5,user.getCreatedAt());
            stmt.setInt(6,user.getId());
            row = stmt.executeUpdate();
            stmt.close();
            DBConnectionPool.releaseConnection(conn);
        } catch (SQLException e) {
            System.out.println("更新用户失败: " + e.getMessage());
        }
        return row;
    }

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

    @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(String.valueOf(rs.getTimestamp("createdAt")));
                return List.of(user);
            }
        }catch (SQLException e) {
            System.out.println("查询所有用户失败: " + e.getMessage());
        }
        return List.of();
    }
}
