package de.assecutor.votianlt.model;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.math.BigDecimal;

@Data
@Document(collection = "jobs")
public class Job {
    @Id
    @JsonIgnore
    private ObjectId id;

    // Metadaten
    @Field("job_number")
    private String jobNumber; // Eindeutige Auftragsnummer

    @Field("status")
    private JobStatus status = JobStatus.CREATED; // Status des Auftrags

    @Field("created_at")
    private LocalDateTime createdAt;

    @Field("updated_at")
    private LocalDateTime updatedAt;

    @Field("created_by")
    private String createdBy; // Benutzer, der den Auftrag erstellt hat

    @Field("is_draft")
    private boolean isDraft = false; // Kennzeichnet Entwürfe

    // Auftraggeber/Rechnungsempfänger
    @Field("customer_selection")
    private String customerSelection; // Kunde01 | KOTVor K01Nach

    // Abholadresse
    @Field("pickup_company")
    private String pickupCompany;

    @Field("pickup_salutation")
    private String pickupSalutation;

    @Field("pickup_first_name")
    private String pickupFirstName;

    @Field("pickup_last_name")
    private String pickupLastName;

    @Field("pickup_phone")
    private String pickupPhone;

    @Field("pickup_street")
    private String pickupStreet;

    @Field("pickup_house_number")
    private String pickupHouseNumber;

    @Field("pickup_address_addition")
    private String pickupAddressAddition;

    @Field("pickup_zip")
    private String pickupZip;

    @Field("pickup_city")
    private String pickupCity;

    // Lieferadresse
    @Field("delivery_company")
    private String deliveryCompany;

    @Field("delivery_salutation")
    private String deliverySalutation;

    @Field("delivery_first_name")
    private String deliveryFirstName;

    @Field("delivery_last_name")
    private String deliveryLastName;

    @Field("delivery_phone")
    private String deliveryPhone;

    @Field("delivery_street")
    private String deliveryStreet;

    @Field("delivery_house_number")
    private String deliveryHouseNumber;

    @Field("delivery_address_addition")
    private String deliveryAddressAddition;

    @Field("delivery_zip")
    private String deliveryZip;

    @Field("delivery_city")
    private String deliveryCity;

    // Digitale Abwicklung per App
    @Field("digital_processing")
    private boolean digitalProcessing;

    @Field("app_user")
    private String appUser;

    // Termine
    @Field("pickup_date")
    private LocalDate pickupDate;

    @Field("delivery_date")
    private LocalDate deliveryDate;

    // Bemerkung
    @Field("remark")
    private String remark;

    // Preis (netto)
    @Field("price")
    private BigDecimal price;

    /**
     * Returns the ObjectId as string for JSON serialization. This ensures that the
     * job id is returned as a string when jobs are retrieved via API.
     */
    @JsonGetter("id")
    public String getIdAsString() {
        return id != null ? id.toString() : null;
    }
}
