package org.raddatz.familienarchiv.importing;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.raddatz.familienarchiv.tag.Tag;
import org.raddatz.familienarchiv.tag.TagService;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Loads {@code canonical-tag-tree.xlsx} into the tag domain via {@link TagService},
 * upserting each tag by its canonical {@code tag_path} (the source_ref). Parent links are
 * resolved by the parent's path, which is the child path with its last {@code /segment}
 * stripped. Rows are emitted parents-first by the normalizer, so a parent is always
 * resolved before any child references it.
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class TagTreeImporter {

    static final List<String> REQUIRED_HEADERS = List.of("tag_path", "parent_name", "tag_name");
    private static final String PATH_SEPARATOR = "/";

    private final TagService tagService;

    public int load(File artifact) {
        List<CanonicalSheetReader.Row> rows = CanonicalSheetReader.readRows(artifact, REQUIRED_HEADERS);
        Map<String, UUID> idByPath = new HashMap<>();
        int processed = 0;
        for (CanonicalSheetReader.Row row : rows) {
            String path = row.get("tag_path");
            if (path.isBlank()) continue;
            UUID parentId = resolveParentId(path, idByPath);
            Tag tag = tagService.upsertBySourceRef(path, row.get("tag_name"), parentId);
            idByPath.put(path, tag.getId());
            processed++;
        }
        log.info("Imported {} tags from {}", processed, artifact.getName());
        return processed;
    }

    private UUID resolveParentId(String path, Map<String, UUID> idByPath) {
        int lastSeparator = path.lastIndexOf(PATH_SEPARATOR);
        if (lastSeparator < 0) return null;
        String parentPath = path.substring(0, lastSeparator);
        return idByPath.get(parentPath);
    }
}
