package com.airlock.iam.common.infrastructure;

import com.airlock.iam.base.api.domain.model.validation.GenericError;
import com.airlock.iam.base.api.domain.model.validation.ValidationResult;
import com.airlock.iam.base.api.domain.model.validation.ValidatorError;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.annotation.Nullable;
import java.time.LocalDate;

public final class LocalDateRange {
   @Nullable
   private final LocalDate fromDate;
   @Nullable
   private final LocalDate toDate;

   public ValidationResult validateInInclusiveRange(LocalDate date) {
      if (this.fromDate != null && date.isBefore(this.fromDate)) {
         return ValidationResult.invalid(ValidatorError.validatorErrorBuilder(GenericError.AFTER).parameter("after", this.fromDate));
      } else {
         return this.toDate != null && this.toDate.isBefore(date)
            ? ValidationResult.invalid(ValidatorError.validatorErrorBuilder(GenericError.BEFORE).parameter("before", this.toDate))
            : ValidationResult.valid();
      }
   }

   public static LocalDateRange of(@Nullable LocalDate from, @Nullable LocalDate to) {
      return builder().fromDate(from).toDate(to).build();
   }

   @Override
   public String toString() {
      return "LocalDateRange(" + this.fromDate + ", " + this.toDate + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   public static LocalDateRange.LocalDateRangeBuilder builder() {
      return new LocalDateRange.LocalDateRangeBuilder();
   }

   @Nullable
   @SuppressFBWarnings(justification = "generated code")
   public LocalDate getFromDate() {
      return this.fromDate;
   }

   @Nullable
   @SuppressFBWarnings(justification = "generated code")
   public LocalDate getToDate() {
      return this.toDate;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof LocalDateRange other)) {
         return false;
      } else {
         Object this$fromDate = this.fromDate;
         Object other$fromDate = other.fromDate;
         if (this$fromDate == null ? other$fromDate == null : this$fromDate.equals(other$fromDate)) {
            Object this$toDate = this.toDate;
            Object other$toDate = other.toDate;
            return this$toDate == null ? other$toDate == null : this$toDate.equals(other$toDate);
         } else {
            return false;
         }
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public int hashCode() {
      int PRIME = 59;
      int result = 1;
      Object $fromDate = this.fromDate;
      result = result * 59 + ($fromDate == null ? 43 : $fromDate.hashCode());
      Object $toDate = this.toDate;
      return result * 59 + ($toDate == null ? 43 : $toDate.hashCode());
   }

   @SuppressFBWarnings(justification = "generated code")
   public LocalDateRange(@Nullable LocalDate fromDate, @Nullable LocalDate toDate) {
      this.fromDate = fromDate;
      this.toDate = toDate;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class LocalDateRangeBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private LocalDate fromDate;
      @SuppressFBWarnings(justification = "generated code")
      private LocalDate toDate;

      @SuppressFBWarnings(justification = "generated code")
      LocalDateRangeBuilder() {
      }

      @SuppressFBWarnings(justification = "generated code")
      public LocalDateRange.LocalDateRangeBuilder fromDate(@Nullable LocalDate fromDate) {
         this.fromDate = fromDate;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LocalDateRange.LocalDateRangeBuilder toDate(@Nullable LocalDate toDate) {
         this.toDate = toDate;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LocalDateRange build() {
         return new LocalDateRange(this.fromDate, this.toDate);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "LocalDateRange.LocalDateRangeBuilder(fromDate=" + this.fromDate + ", toDate=" + this.toDate + ")";
      }
   }
}
