package org.raddatz.familienarchiv.importing;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Async import state surfaced to {@code admin/system/ImportStatusCard.svelte} via the
 * generated types. The shape ({@code state, statusCode, processed, skippedFiles, skipped})
 * is kept verbatim from the retired MassImportService so the admin UI keeps working.
 */
public record ImportStatus(
        @Schema(requiredMode = Schema.RequiredMode.REQUIRED) State state,
        @Schema(requiredMode = Schema.RequiredMode.REQUIRED) String statusCode,
        @JsonIgnore String message,
        @Schema(requiredMode = Schema.RequiredMode.REQUIRED) int processed,
        @Schema(requiredMode = Schema.RequiredMode.REQUIRED) List<SkippedFile> skippedFiles,
        LocalDateTime startedAt
) {

    public enum State { IDLE, RUNNING, DONE, FAILED }

    public enum SkipReason {
        INVALID_FILENAME_PATH_TRAVERSAL,
        INVALID_PDF_SIGNATURE,
        FILE_READ_ERROR,
        ALREADY_EXISTS,
        S3_UPLOAD_FAILED
    }

    public record SkippedFile(
            @Schema(requiredMode = Schema.RequiredMode.REQUIRED) String filename,
            @Schema(requiredMode = Schema.RequiredMode.REQUIRED) SkipReason reason
    ) {}

    // Note: @Schema on a record accessor method is not picked up by SpringDoc; the
    // "skipped" count is a computed convenience field derived from skippedFiles.size().
    @JsonProperty("skipped")
    public int skipped() {
        return skippedFiles.size();
    }

    /** Defensive-copy constructor — callers cannot mutate the stored list after construction. */
    public ImportStatus {
        skippedFiles = List.copyOf(skippedFiles);
    }
}
