package dev.dao;

import dev.database.DatabaseConnection;
import dev.model.MenuItem;

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

public class MenuItemDao {

    public List<MenuItem> findAll() {

        List<MenuItem> result = new ArrayList<>();

        String quary = "SELECT * FROM menu_items";


        try (Connection c = DatabaseConnection.getConnection();
             Statement pst = c.createStatement()){

            ResultSet rst = pst.executeQuery(quary);
            while (rst.next()){
                MenuItem item = new MenuItem(rst.getInt("id"),
                        rst.getString("name"),
                        rst.getString("description"),
                        rst.getDouble("price"),
                        rst.getString("category"));
                result.add(item);
            }

            return  result;

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }


        return null;
    }

    public MenuItem findById(int id) {
        String quary = "SELECT * FROM menu_items WHERE id = ?";


        try (Connection c = DatabaseConnection.getConnection();
             PreparedStatement pst = c.prepareStatement(quary)){

            pst.setInt(1 , id);

            try (ResultSet rst = pst.executeQuery()){
                if(rst.next()){
                    MenuItem item = new MenuItem(rst.getInt("id"),
                            rst.getString("name"),
                            rst.getString("descrition"),
                            rst.getDouble("price"),
                            rst.getString("category"));


                    return item;
                }
                else {
                    return null;
                }

            }


        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

        return null;

    }

}