package de.accso.flexinale.backoffice.domain.model;

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

import java.time.LocalDateTime;
import java.util.Optional;

public final class Vorfuehrung implements Identifiable, Versionable, Mergeable<Vorfuehrung>, EqualsByContent {   // TODO is only final as otherwise Spotbugs complains, get rid of Exception in constructor!
    public record Zeit(LocalDateTime raw) implements RawWrapper<LocalDateTime> {
        public Zeit(LocalDateTime raw) {
            this.raw = raw.withNano(0); // precision is second
        }
    }

    public final Id id;
    public final Version version;

    public final Zeit zeit;

    @DoNotCheckInArchitectureTests
    public final Film film;
    @DoNotCheckInArchitectureTests
    public final KinoSaal kinoSaal;

    public Vorfuehrung(final Id id,
                       final Zeit zeit,
                       final Film film,
                       final KinoSaal kinoSaal) {
        this(id, Versionable.unknownVersion(), zeit, film, kinoSaal);
    }

    public Vorfuehrung(final Id id, final Version version,
                       final Zeit zeit,
                       final Film film,
                       final KinoSaal kinoSaal) {
        this.id = id;
        this.version = version;
        this.zeit = zeit;
        this.film = film;
        this.kinoSaal = kinoSaal;

        //TODO this validation should not be done here. If class is not final, Spotbugs complains. For DB entities use "nullable=false" and "optional=false" for DB checks. In domain classes check with jakarta.annotation NonNull? Also: Why is KinoSaal obligatory but not Film?
        if (kinoSaal == null) {
            throw new FlexinaleIllegalArgumentException("KinoSaal of Vorfuehrung " + this +
                    " must not be null");
        }
        //TODO this validation should not be done here but in KinoSaal itself
        if (kinoSaal.anzahlPlaetze == null) {
            throw new FlexinaleIllegalArgumentException("KinoSaal's Anzahl Plaetze of Vorfuehrung " + this +
                    " must not be null");
        }
    }

    @Override
    public Id id() {
        return id;
    }

    @Override
    public Version version() {
        return version;
    }

    @Override
    public Vorfuehrung merge(final Vorfuehrung newData) {
        // merging Vorfuehrung has some business flaws - like when its film or its kinoSaal is changed,
        //   then all sold tickets might get obsolete - we ignore this here but care for that in Ticketing
        Film mergedFilm = this.film.merge(newData.film);

        Kino mergedKino = this.kinoSaal.kino.merge(newData.kinoSaal.kino);
        Optional<KinoSaal> mergedKinoSaal = mergedKino.kinoSaele.stream().filter(
                kinoSaal -> kinoSaal.id.equals(this.kinoSaal.id)).findFirst();
        if (mergedKinoSaal.isEmpty()) {
            throw new DeveloperMistakeException("merged KinoSaal from Vorfuehrung could not be retrieved");
        }

        return new Vorfuehrung(this.id, this.version,
                newData.zeit, mergedFilm, mergedKinoSaal.get());
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

    @Override
    public boolean equals(final Object o) {
        if (!equalsByContent(o)) return false;

        if (this == o) {return true;}
        if (o == null || getClass() != o.getClass()) {return false;}
        Vorfuehrung that = (Vorfuehrung) o;
        return new EqualsBuilder().append(version, that.version).isEquals();
    }

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

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

        Vorfuehrung that = (Vorfuehrung) o;

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

        if (film == null && that.film == null) result = true;
        if (film == null && that.film != null) return false;
        if (film != null) {
            result = film.equalsByContent(that.film);
            if (!result) return false;
        }

        if (kinoSaal == null && that.kinoSaal == null) result = true;
        if (kinoSaal == null && that.kinoSaal != null) return false;
        if (kinoSaal != null) {
            result = kinoSaal.equalsByContent(that.kinoSaal);
            return result;
        }

        return true;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(id).append(version)
                .append(zeit)
                .append(film)
                .append(kinoSaal)
                .toHashCode();
    }
}
