package inventory;

/**
 * Factory class for creating Product objects with business rules.
 *
 * Requirements:
 * - Constants for product types
 * - Business rules: Books minimum $5, Electronics minimum $10
 * - Static factory method for product creation
 * - Exception handling for invalid inputs
 */
public class ProductFactory {

    public static final String BOOK_TYPE = "BOOK";
    public static final String ELECTRONICS_TYPE = "ELECTRONICS";
    public static final String FURNITURE_TYPE = "FURNITURE";

    /**
     * Factory method to create products with business rules
     * @param id - product identifier
     * @param name - product name
     * @param type - product type (must be BOOK or ELECTRONICS)
     * @param price - product price (must meet minimum requirements)
     * @param quantity - initial stock quantity
     * @return new Product instance
     * @throws IllegalArgumentException if validation fails
     */
    public static Product createProduct(String id, String name, String type, double price, int quantity) {

        if (!isValidProductType(type)) {
            throw new IllegalArgumentException("Product type '%s' not available.".formatted(type));
        }

        validateBusinessRules(type, price);

        return new Product(id, name, type, price, quantity);
    }

    public static boolean isValidProductType(String type) {
        return BOOK_TYPE.equals(type) || ELECTRONICS_TYPE.equals(type) || FURNITURE_TYPE.equals(type);
    }

    private static void validateBusinessRules(String type, double price) {
        if (BOOK_TYPE.equals(type) && price < 5.0) {
            throw new IllegalArgumentException("Book price must be at least 5");
        }

        if (ELECTRONICS_TYPE.equals(type) && price < 10.0) {
            throw new IllegalArgumentException("Electronics price must be at least 10");
        }

        if (FURNITURE_TYPE.equals(type) && price < 50.0) {
            throw new IllegalArgumentException("Furniture price must be at least 50");
        }
    }
}