package dao;

import dto.Message;
import dto.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ThreadDAO {
    private Connection con;

    public ThreadDAO(){
        this.con = new BDConnection().getConnection();
    }

    public List<Message> getAllMessages(){
        ArrayList<Message> messages = new ArrayList<>();
        try{
            Statement stmt = this.con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM msg;");

            while(rs.next()){
                int msgId = rs.getInt(1);
                int userIdMsg = rs.getInt(2);
                int threadId = rs.getInt(3);
                String msg = rs.getString(4);

                messages.add(new Message(msgId,userIdMsg,threadId,msg));
            }
        } catch (SQLException sqle) {
            sqle.getStackTrace();
        }

        return messages;
    }

    public Thread getThreadById(int threadID) throws SQLException {
        Thread thread = new Thread();
        PreparedStatement ps = this.con.prepareStatement("SELECT threadID, threadName FROM thread WHERE threadID = ?");
        try {
            ps.setInt(1, threadID);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                thread.setThreadID(rs.getInt(1));
                thread.setName(rs.getString(2));
            }
        }
        catch (SQLException sqle) {
            sqle.getStackTrace();
        }
        return thread;
    }

    public List<Message> getAllMessagesFromThread(int threadId){
        ArrayList<Message> messages = new ArrayList<>();
        try{
            PreparedStatement ps = this.con.prepareStatement("SELECT msg FROM msg WHERE thread_id=?;");
            ps.setInt(1, threadId);
            ResultSet rs = ps.executeQuery();

            while(rs.next()){
                int msgId = rs.getInt(1);
                int userIdMsg = rs.getInt(2);
                String msg = rs.getString(3);

                messages.add(new Message(msgId,userIdMsg,threadId,msg));
            }
        } catch (SQLException sqle) {
            sqle.getStackTrace();
        }

        return messages;
    }

    public List<User> getFollowersOfThread(int threadId) throws SQLException{
        ArrayList<User> followers = new ArrayList<>();
        try {
            PreparedStatement ps = this.con.prepareStatement("""
            SELECT u.userID, u.username, u.password FROM userAccount AS u
            INNER JOIN follow AS f ON f.userID=u.userID
            WHERE f.threadID_follow = ?
            """);
            ps.setInt(1, threadId);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                int userId = rs.getInt(1);
                String username = rs.getString(2);
                String password = rs.getString(3);

                followers.add(new User(userId, username, password));
            }
        } catch (SQLException sqle) {
            sqle.getStackTrace();
        }
        return followers;
    }

    // Créer un thread
    public void createThread(User user, String threadName) throws SQLException {
        PreparedStatement ps = this.con.prepareStatement("INSERT INTO thread (userID_thread, threadName) VALUES(?, ?)");
        try {
            ps.setString(1, ""+user.getId());
            ps.setString(2, threadName);
        }
        catch (SQLException sqle) {
            sqle.getStackTrace();
        }
    }


//
//    public String getUserById(int id){
//        StringBuilder txt = new StringBuilder();
//        try{
//            PreparedStatement stmt = this.con.prepareStatement("SELECT * FROM userAccount WHERE id=?");
//            stmt.setInt(1, id);
//            ResultSet rs = stmt.executeQuery();
//
//            if(rs.next()){
//                int userId = rs.getInt(1);
//                String username = rs.getString(2);
//                String pwd = rs.getString(3);
//
//                txt.append("Id : ").append(userId).append(" Username : ").append(username).append(" Password : ").append(pwd).append('\n');
//            }
//        } catch (SQLException sqle) {
//            sqle.getStackTrace();
//        }
//
//        return txt.toString();
//    }
//
//    public void addUser(User nUser){
//        try{
//            PreparedStatement stmt = this.con.prepareStatement("INSERT INTO userAccount VALUES (?,?,?)");
//            stmt.setInt(1, nUser.getID());
//            stmt.setString(2, nUser.getUserName());
//            stmt.setString(3, nUser.getPwd());
//
//            stmt.executeUpdate();
//
//        } catch (SQLException sqle) {
//            sqle.getStackTrace();
//        }
//    }
}