package ch.zhaw.gartenverwaltung.types;

import java.time.LocalDate;
import java.util.Objects;
import java.util.Optional;

public class Crop {
    private Long cropId = null;
    private final long plantId;
    private final LocalDate startDate;
    private double area = 1;

    /**
     * Default Constructor (needed for deserialization)
     */
    public Crop() {
        plantId = 0;
        startDate = null;
    }

    public Crop(long plantId, LocalDate startDate) {
        this.plantId = plantId;
        this.startDate = startDate;
    }

    // Builder-Style setter
    public Crop withId(long cropId) {
        this.cropId = cropId;
        return this;
    }
    public Crop withArea(double area) {
        this.area = area;
        return this;
    }

    // Getters
    public Optional<Long> getCropId() {
        return Optional.ofNullable(cropId);
    }

    public long getPlantId() { return plantId; }
    public LocalDate getStartDate() { return startDate; }
    public double getArea() { return area; }

    @Override
    public boolean equals(Object other) {
        if (this == other) return true;
        if (other instanceof Crop otherCrop) {
            return Objects.equals(this.cropId, otherCrop.cropId) &&
                    plantId == otherCrop.plantId &&
                    startDate != null && startDate.equals(otherCrop.startDate) &&
                    area == otherCrop.area;
        }
        return false;
    }

    @Override
    public int hashCode() {
        int startCode = startDate != null ? startDate.hashCode() : 0;
        return (int) plantId ^ (startCode << 16);
    }

    @Override
    public String toString() {
        return String.format("Crop [ cropId: %d, plantId: %d, startDate: %s, area: %f ]",
                cropId,
                plantId,
                startDate,
                area);
    }
}
