package dev.dao;

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

import javax.xml.crypto.Data;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuItemDao {

    public List<MenuItem> findAll() {
        String sql = "SELECT * FROM menu";
        List<MenuItem> result = new ArrayList<>();

        try {
            PreparedStatement stmt = DatabaseConnection.getConnection().prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();

            while (rs.next()){
                result.add(new MenuItem(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("description"),
                        rs.getDouble("price"),
                        rs.getString("category")
                ));
            }
        } catch (SQLException e) {
            System.err.println("Unable to fetch menu: " + e.getMessage());
        }

        return result;
    }

    public MenuItem findById(int id) {

        MenuItem item = null;
        String sql = "SELECT * FROM menu WHERE id=?";


        try {
            PreparedStatement stmt = DatabaseConnection.getConnection().prepareStatement(sql);
            stmt.setInt(1,id);

            ResultSet rs = stmt.executeQuery();
            if (rs.next()){
                item = new MenuItem(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("description"),
                        rs.getDouble("price"),
                        rs.getString("category")
                );
            }

        } catch (SQLException e) {
            System.err.println("Unable to fetch item: " + e.getMessage());
        }

        return item;
    }

}