package de.assecutor.votianlt.model;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import de.assecutor.votianlt.model.task.BaseTask;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.Field;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Embedded delivery station within a Job. Each job can have up to 25 delivery
 * stations. This is NOT a standalone MongoDB document - it is stored as part of
 * the Job document.
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DeliveryStation {

    @Field("station_id")
    @JsonIgnore
    private ObjectId stationId;

    @Field("station_order")
    private int stationOrder;

    @Field("company")
    private String company;

    @Field("salutation")
    private String salutation;

    @Field("first_name")
    private String firstName;

    @Field("last_name")
    private String lastName;

    @Field("phone")
    private String phone;

    @Field("street")
    private String street;

    @Field("house_number")
    private String houseNumber;

    @Field("address_addition")
    private String addressAddition;

    @Field("zip")
    private String zip;

    @Field("city")
    private String city;

    @Field("delivery_date")
    private LocalDate deliveryDate;

    @Field("delivery_time")
    private LocalTime deliveryTime;

    @Field("tasks")
    private List<BaseTask> tasks = new ArrayList<>();

    @JsonGetter("stationId")
    public String getStationIdAsString() {
        return stationId != null ? stationId.toHexString() : null;
    }

    public ObjectId ensureStationId() {
        if (stationId == null) {
            stationId = new ObjectId();
        }
        return stationId;
    }
}
