package dev.dao;

import dev.model.OrderDetail;

import java.util.List;
import dev.database.DatabaseConnection;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;

public class OrderDetailDao {

    public void save(OrderDetail detail) {

        // TODO:
        // Insert order detail
        String sql = "INSERT INTO order_details (order_id, menu_item_id, quantity, price_at_purchase) VALUES (?, ?, ?, ?)";
        try (Connection conn = DatabaseConnection.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)){
            Field orderIdField = OrderDetail.class.getDeclaredField("orderId");
            Field menuItemIdField = OrderDetail.class.getDeclaredField("menuItemId");
            Field quantityField = OrderDetail.class.getDeclaredField("quantity");
            Field priceField = OrderDetail.class.getDeclaredField("price");
            orderIdField.setAccessible(true);
            menuItemIdField.setAccessible(true);
            quantityField.setAccessible(true);
            priceField.setAccessible(true);
            stmt.setInt(1, (int) orderIdField.get(detail));
            stmt.setInt(2, (int) menuItemIdField.get(detail));
            stmt.setInt(3, (int) quantityField.get(detail));
            stmt.setDouble(4, (double) priceField.get(detail));
            stmt.executeUpdate();
        }
        catch (SQLException | NoSuchFieldException | IllegalAccessException ex){
            ex.printStackTrace();
        }
    }

    public List<OrderDetail> findByOrderId(int orderId) {

        // TODO:
        // Retrieve order details
        List<OrderDetail> details = new ArrayList<>();
        String sql = "SELECT * FROM order_details WHERE order_id = ?";
        try (Connection conn = DatabaseConnection.getConnection(); PreparedStatement stmt = conn.prepareStatement(sql)){
            stmt.setInt(1, orderId);
            try (ResultSet rs = stmt.executeQuery()){
                while (rs.next()){
                    OrderDetail detail = new OrderDetail();
                    Field idField = OrderDetail.class.getDeclaredField("id");
                    Field ordIdField = OrderDetail.class.getDeclaredField("orderId");
                    Field itemField = OrderDetail.class.getDeclaredField("menuItemId");
                    Field qtyField = OrderDetail.class.getDeclaredField("quantity");
                    Field prcField = OrderDetail.class.getDeclaredField("price");
                    idField.setAccessible(true);
                    ordIdField.setAccessible(true);
                    itemField.setAccessible(true);
                    qtyField.setAccessible(true);
                    prcField.setAccessible(true);
                    idField.set(detail, rs.getInt("id"));
                    ordIdField.set(detail, rs.getInt("order_id"));
                    itemField.set(detail, rs.getInt("menu_item_id"));
                    qtyField.set(detail, rs.getInt("quantity"));
                    prcField.set(detail, rs.getDouble("price_at_purchase"));
                    details.add(detail);
                }
            }
        }
        catch (SQLException | NoSuchFieldException | IllegalAccessException ex){
            ex.printStackTrace();
        }
        return details;
    }

}