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.time.LocalDateTime;

public record VorfuehrungTO(Id id, Version version,
                            Zeit zeit,
                            @DoNotCheckInArchitectureTests FilmTO film,
                            @DoNotCheckInArchitectureTests KinoSaalTO kinoSaal,
                            Id kinoId)
        implements Identifiable, Versionable, EqualsByContent, Serializable
{
    public record Zeit(LocalDateTime raw) implements RawWrapper<LocalDateTime> {
        public Zeit(LocalDateTime raw) {
            this.raw = raw.withNano(0); // precision is second
        }
    }

    public VorfuehrungTO(final Id id, final Version version,
                         final Zeit zeit,
                         final FilmTO film,
                         final KinoSaalTO kinoSaal,
                         final Id kinoId) {
        this.id = id;
        this.version = version;
        this.zeit = zeit;
        this.film = film;
        this.kinoSaal = kinoSaal;

        // VorfuehrungTO also gets kinoId, which is actually redundant (as a KinoSaalTO already contains the Kino).
        //  But during serialization the Kino is set to null! Therefore, we add the extra kinoId here!
        this.kinoId = kinoId;

        //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");
        }
        //TODO Why is this here (check not done in any other Vorfuehrung* class)?
        if (kinoId == null) {
            throw new FlexinaleIllegalArgumentException("Kino Id of Vorfuehrung " + this +
                    " must not be null");
        }
    }

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

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

        VorfuehrungTO that = (VorfuehrungTO) o;

        boolean result = new EqualsBuilder()
                .append(id, that.id)
                .append(film, that.film)
                .append(zeit, that.zeit)
                .append(kinoId, that.kinoId)
                .isEquals();
        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;
    }
}
