package inventory;

/**
 * Product class representing items in the inventory.
 * Requirements:
 * - Private fields: id, name, type, price, quantity
 * - Constructor with validation
 * - Getter and setter methods
 * - sell(), addStock(), isInStock() methods
 * - toString() method
 */
public class Product {

    private final String id;
    private final String name;
    private final String type;
    private double price;
    private int quantity;

    /**
     * @param id - unique product identifier
     * @param name - product name
     * @param type - product category (BOOK or ELECTRONICS)
     * @param price - product price (must be non-negative)
     * @param quantity - stock quantity (must be non-negative)
     * @throws IllegalArgumentException if price or quantity is negative
     */
    public Product(String id, String name, String type, double price, int quantity) {
        if (price <= 0) {
            throw new IllegalArgumentException("Price cannot be negative or zero");
        }
        if (quantity <= 0) {
            throw new IllegalArgumentException("Quantity cannot be negative or zero");
        }
        if (id == null) {
            throw new IllegalArgumentException("Id cannot be null");
        }
        if (name == null) {
            throw new IllegalArgumentException("Name cannot be null");
        }
        if (type == null) {
            throw new IllegalArgumentException("Type cannot be null");
        }
        this.id = id;
        this.name = name;
        this.type = type;
        this.price = price;
        this.quantity = quantity;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public double getPrice() {
        return price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setPrice(double price) {
        if (price <= 0) {
            throw new IllegalArgumentException("Price cannot be negative or zero");
        }
        this.price = price;
    }

    public void setQuantity(int quantity) {
        if (quantity <= 0) {
            throw new IllegalArgumentException("Quantity cannot be negative or zero");
        }
        this.quantity = quantity;
    }

    /**
     * @param amount - quantity to sell
     * @return true if sale successful, false if insufficient stock
     */
    public boolean sell(int amount) {
        if (amount <= 0 || amount > quantity) {
            return false;
        }

        quantity -= amount;
        return true;
    }

    /**
     * @param amount - quantity to add to stock
     */
    public void addStock(int amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("Amount cannot be negative or zero");
        }
        quantity += amount;
    }

    /**
     * @return true if product has stock available
     */
    public boolean isInStock() {
        return quantity > 0;
    }

    /**
     * @return formatted string representation of the product
     */
    @Override
    public String toString() {
        return "Product{id='%s', name='%s', type='%s', price=%f, quantity=%d}"
                .formatted(id, name, type, price, quantity);
    }
}