package de.assecutor.votianlt.model;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
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.LocalDateTime;
import java.util.List;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "tasks")
public class TaskEntry {
    @Id
    @JsonIgnore
    private ObjectId id;

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

    @Field("job_id")
    @JsonIgnore
    private ObjectId jobId;

    @Field("task_type")
    private TaskType taskType = TaskType.CONFIRMATION;

    // Task-specific configuration data
    @Field("configuration")
    private TaskConfiguration configuration;

    // Completion tracking
    @Field("completed")
    private boolean completed = false;

    @Field("completed_at")
    private LocalDateTime completedAt;

    @Field("completed_by")
    private String completedBy;

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

    /**
     * Returns the station ObjectId as string for JSON serialization.
     */
    @JsonGetter("stationId")
    public String getStationIdAsString() {
        return stationId != null ? stationId.toHexString() : null;
    }

    /**
     * Returns the legacy job ObjectId as string for internal fallback handling.
     */
    public String getJobIdAsString() {
        return jobId != null ? jobId.toString() : null;
    }

    /**
     * Enum for different task types
     */
    public enum TaskType {
        CONFIRMATION(
                "Bestätigung"),
        SIGNATURE("Unterschrift"),
        TODOLIST("To-Do Liste"),
        PHOTO("Foto"),
        BARCODE("Barcode");

        private final String displayName;

        TaskType(String displayName) {
            this.displayName = displayName;
        }

        public String getDisplayName() {
            return displayName;
        }
    }

    /**
     * Configuration data for different task types
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class TaskConfiguration {
        // For CONFIRMATION: button text
        private String buttonText;

        // For TODOLIST: list of todo items
        private List<String> todoItems;

        // For PHOTO: min and max photo count
        private Integer minPhotoCount;
        private Integer maxPhotoCount;

        // For BARCODE: min and max barcode count
        private Integer minBarcodeCount;
        private Integer maxBarcodeCount;

        // Generic configuration map for future extensions
        private Map<String, Object> additionalConfig;
    }
}
