package de.accso.flexinale.backoffice.api_contract.event.model;

import de.accso.flexinale.common.shared_kernel.*;
import org.apache.commons.lang3.builder.EqualsBuilder;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public record KinoTO(Id id, Version version,
                     Name name, Adresse adresse, EmailAdresse emailAdresse,
                     @DoNotCheckInArchitectureTests Set<KinoSaalTO> kinoSaele)
        implements Identifiable, Versionable, EqualsByContent, Serializable
{
    public record Name(String raw) implements RawWrapper<String> {}
    public record Adresse(String raw) implements RawWrapper<String> {}
    public record EmailAdresse(String raw) implements RawWrapper<String> {}

    public KinoTO(final Id id, final Version version) {
        this(id, version, new KinoTO.Name(""), new KinoTO.Adresse(""), new KinoTO.EmailAdresse(""),
                new HashSet<>()); // cannot use Set.of() as this would be an ImmutableCollections.EMPTY_SET
    }

    public KinoTO(final Id id,
                  final Name name, final Adresse adresse,
                  final EmailAdresse emailAdresse, final Set<KinoSaalTO> kinoSaele) {
        this(id, Versionable.unknownVersion(), name, adresse, emailAdresse, kinoSaele);
    }

    @SuppressWarnings({"ConstantValue", "unused"})
    @Override
    public boolean equalsByContent(final Object o) {
        if (this == o) {return true;}

        if (o == null || getClass() != o.getClass()) {return false;}

        KinoTO that = (KinoTO) o;

        boolean result = new EqualsBuilder()
                .append(id, that.id)
                .append(name, that.name)
                .append(adresse, that.adresse).append(emailAdresse, that.emailAdresse)
                .isEquals();
        if (!result) return false;

        if (kinoSaele == null && that.kinoSaele == null) return true;
        if (kinoSaele == null && that.kinoSaele != null) return false;
        if (kinoSaele != null && that.kinoSaele == null) return false;

        List<KinoSaalTO> thisKSList = kinoSaele.stream().toList();
        List<KinoSaalTO> thatKSList = that.kinoSaele.stream().toList();
        if (thisKSList.size() != thatKSList.size()) return false;
        for (int counter = 0; counter < thisKSList.size(); counter++) {
            if (!thisKSList.get(counter).equalsByContent(thatKSList.get(counter))) return false;
        }

        return true;
    }

}
