import java.util.List;
import java.util.ArrayList;
import org.sql2o.*;

public class User {
  private int id;
  private String user_name;
  private int score;

  //CONSTRUCTOR//
  public User(String user_name, int score) {
    this.user_name = user_name;
    this.score = score;
  }

  //GETTERS//
  public String getUserName() {
    return user_name;
  }

  public int getId() {
    return id;
  }

  @Override
  public boolean equals(Object otherUser){
    if (!(otherUser instanceof User)) {
      return false;
    } else {
      User newUser = (User) otherUser;
      return this.getUserName().equals(newUser.getUserName());
    }
  }

  //CREATE//
  public void save() {
    try(Connection con = DB.sql2o.open()) {
      String sql = "INSERT INTO users(user_name) VALUES (:user_name)";
      this.id = (int) con.createQuery(sql,true)
      .addParameter("user_name", this.user_name)
      .executeUpdate()
      .getKey();
    }
  }

  //READ//
  public static List<User> all() {
    String sql = "SELECT id, user_name FROM users";
    try(Connection con = DB.sql2o.open()) {
      return con.createQuery(sql)
      .executeAndFetch(User.class);
    }
  }

  public static User find(int id) {
    try(Connection con = DB.sql2o.open()) {
      String sql = "SELECT * FROM Users where id=:id";
      User user = con.createQuery(sql)
      .addParameter("id", id)
      .executeAndFetchFirst(User.class);
      return user;
    }
  }

  // public List<Restaurant> getRestaurants() {
  //   String sql = "SELECT restaurants.* FROM users JOIN check_ins ON (users.id = check_ins.user_id) JOIN restaurants ON (check_ins.restaurant_id = restaurants.id) WHERE users.id = :user_id";
  //   try(Connection con = DB.sql2o.open()) {
  //     return con.createQuery(sql)
  //     .addParameter("user_id", id)
  //     .executeAndFetch(Restaurant.class);
  //   }
  // }

  //UPDATE//
  public void update(String newVenueName) {
    this.user_name = newVenueName;
    String sql = "UPDATE users SET user_name = :newVenueName WHERE id=:id";
    try(Connection con = DB.sql2o.open()) {
      con.createQuery(sql)
      .addParameter("newVenueName", newVenueName)
      .addParameter("id", this.id)
      .executeUpdate();
    }
  }

  // public void addRestaurant(Restaurant restaurant) {
  //   try(Connection con = DB.sql2o.open()) {
  //     String sql = "INSERT INTO check_ins (restaurant_id, user_id) VALUES (:restaurant_id, :user_id)";
  //     con.createQuery(sql)
  //     .addParameter("restaurant_id", restaurant.getId())
  //     .addParameter("user_id", this.getId())
  //     .executeUpdate();
  //   }
  // }

  // public void assignRestaurant(int restaurant_id) {
  //   restaurant_id = restaurant_id;
  //   String sql = "UPDATE check_ins SET restaurant_id = :restaurant_id WHERE user_id=:user_id";
  //   try(Connection con = DB.sql2o.open()) {
  //     con.createQuery(sql)
  //        .addParameter("restaurant_id", restaurant_id)
  //        .addParameter("id", this.id)
  //        .executeUpdate();
  //   }
  // }

  //DESTROY//
  public void delete() {
    try(Connection con = DB.sql2o.open()) {
      String sql = "DELETE FROM users WHERE id = :id;";
      con.createQuery(sql)
      .addParameter("id", id)
      .executeUpdate();

      String check_insQuery = "DELETE FROM check_ins WHERE user_id = :userId";
      con.createQuery(check_insQuery)
      .addParameter("userId", this.getId())
      .executeUpdate();
    }
  }
}

@@ -1424,35 +1424,32 @@
 er;%0A    %7D%0A  %7D%0A%0A 
- //
  public List%3CRes
@@ -1477,19 +1477,16 @@
 ts() %7B%0A 
- //
    Strin
@@ -1659,35 +1659,32 @@
 d = :user_id%22;%0A 
- //
    try(Connectio
@@ -1712,23 +1712,20 @@
 ()) %7B%0A  
-//
     
- 
 return c
@@ -1737,35 +1737,32 @@
 eateQuery(sql)%0A 
- //
      .addParamet
@@ -1776,27 +1776,24 @@
 r_id%22, id)%0A 
- //
      .execut
@@ -1818,36 +1818,30 @@
 nt.class);%0A 
- //
    %7D%0A 
- //
  %7D%0A%0A  //UPDA
@@ -2188,27 +2188,24 @@
     %7D%0A  %7D%0A%0A 
- //
  public void
@@ -2245,19 +2245,16 @@
 ant) %7B%0A 
- //
    try(C
@@ -2285,27 +2285,24 @@
 .open()) %7B%0A 
- //
      String 
@@ -2392,19 +2392,16 @@
 _id)%22;%0A 
- //
      con
@@ -2415,27 +2415,24 @@
 Query(sql)%0A 
- //
      .addPar
@@ -2477,23 +2477,20 @@
 Id())%0A  
-//
     
- 
 .addPara
@@ -2521,19 +2521,16 @@
 tId())%0A 
- //
      .ex
@@ -2545,36 +2545,27 @@
 ate();%0A 
- //
    %7D%0A 
- //
  %7D%0A%0A 
- //
  public 
@@ -2608,19 +2608,16 @@
 _id) %7B%0A 
- //
    resta
@@ -2643,19 +2643,16 @@
 nt_id;%0A 
- //
    Strin
@@ -2738,19 +2738,16 @@
 r_id%22;%0A 
- //
    try(C
@@ -2782,19 +2782,16 @@
 n()) %7B%0A 
- //
      con
@@ -2809,19 +2809,16 @@
 y(sql)%0A 
- //
         
@@ -2865,26 +2865,23 @@
 t_id)%0A  
-//
        
- 
 .addPara
@@ -2907,18 +2907,15 @@
 )%0A  
-//
        
- 
 .exe
@@ -2933,20 +2933,14 @@
 );%0A 
- //
    %7D%0A 
- //
  %7D%0A%0A

add restaurant codes to user