package net.tinsae.shop;

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

class ItemDao{

    public List<Item> findAll() {
        String query = "SELECT * FROM items";
        try( // with resource
             var con = Util.getConnection();
             var stm = con.prepareStatement(query);
        ){
            List<Item> items = new ArrayList<>();
            var res = stm.executeQuery();
            while(res.next()){
                items.add(convert(res));
            }
            return items;

        }catch (SQLException e){
            System.out.println("Error reading from database, Reason : "+ e.getMessage());
            return Collections.emptyList();
        }
    }

    public Item findById(int id) {
        String query = "SELECT * FROM items WHERE id = ?";
        Item item = null;
        try(
                var con = Util.getConnection();
                var stm = con.prepareStatement(query);
        ){
            stm.setInt(1, id);
            var res = stm.executeQuery();
            if (res.next()){
                item = convert(res);
            }
            return item;
        }catch(SQLException e){
            System.out.println("Error inserting data to database, Reason : "+ e.getMessage());
            return item;
        }
    }

    public boolean insert(Item item) {
        String query = "INSERT INTO items (name, description, price) VALUES (?,?,?)";
        try(
                var con = Util.getConnection();
                var stm = con.prepareStatement(query);
        ){
            //stm.setInt(1, item.getId()); // this should normally be auto
            stm.setString(1, item.getName());
            stm.setString(2, item.getDescription());
            stm.setFloat(3, item.getPrice());
            stm.executeUpdate();
            return true;
        }catch(SQLException e){
            System.out.println("Error inserting data to database, Reason : "+ e.getMessage());
            return false;
        }
    }

    public boolean update(Item item) {
        String query = "UPDATE items SET name = ?,description = ?,price = ? WHERE id = ?";
        try(
                var con = Util.getConnection();
                var stm = con.prepareStatement(query);
        ){
            stm.setString(1, item.getName());
            stm.setString(2, item.getDescription());
            stm.setFloat(3, item.getPrice());
            stm.setInt(4, item.getId());
            stm.executeUpdate();
            return true;
        }catch(SQLException e){
            System.out.println("Error updating data to database, Reason : "+ e.getMessage());
            return false;
        }
    }

    public boolean delete(int id) {
        String query = "DELETE FROM items WHERE id = ?";
        try(
                var con = Util.getConnection();
                var stm = con.prepareStatement(query);
        ){
            stm.setInt(1, id);
            stm.executeUpdate();
            return true;
        }catch(SQLException e){
            System.out.println("Error deleting data from database, Reason : "+ e.getMessage());
            return false;
        }
    }

    protected Item convert(ResultSet res){
        try{
            return Item.createItem(
                    res.getInt("id"),
                    res.getString("name"),
                    res.getString("description"),
                    res.getFloat("price"));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    
}