package com.kamco.cd.kamcoback.zoo.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.kamco.cd.kamcoback.common.utils.enums.EnumType;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.ZonedDateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

public class AnimalDto {

  @Schema(name = "AnimalAddReq", description = "동물 생성 요청")
  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  public static class AddReq {

    private Category category;
    private Species species;
    private String name;
    private String zooUuid; // 동물원 UUID (선택)
  }

  @Schema(name = "AnimalBasic", description = "동물 기본 정보")
  @Getter
  public static class Basic {

    @JsonIgnore private Long id;
    private String uuid;
    private Category category;
    private Species species;
    private String name;

    @JsonFormat(
        shape = JsonFormat.Shape.STRING,
        pattern = "yyyy-MM-dd'T'HH:mm:ssXXX",
        timezone = "Asia/Seoul")
    private ZonedDateTime createdDate;

    @JsonFormat(
        shape = JsonFormat.Shape.STRING,
        pattern = "yyyy-MM-dd'T'HH:mm:ssXXX",
        timezone = "Asia/Seoul")
    private ZonedDateTime modifiedDate;

    public Basic(
        Long id,
        String uuid,
        String name,
        Category category,
        Species species,
        ZonedDateTime createdDate,
        ZonedDateTime modifiedDate) {
      this.id = id;
      this.uuid = uuid;
      this.name = name;
      this.category = category;
      this.species = species;
      this.createdDate = createdDate;
      this.modifiedDate = modifiedDate;
    }
  }

  @Getter
  @AllArgsConstructor
  public enum Category implements EnumType {
    // @formatter:off
    MAMMALS("100", "포유류"), // 땅에 사는 동물
    BIRDS("200", "조류"), // 하늘을 나는 동물
    FISH("300", "어류"),
    AMPHIBIANS("400", "양서류"),
    REPTILES("500", "파충류"),
    INSECTS("500", "곤충"),
    INVERTEBRATES("500", "무척추동물"),
    ;
    // @formatter:on
    private final String id;
    private final String text;
  }

  @Getter
  @AllArgsConstructor
  public enum Species implements EnumType {
    // @formatter:off
    DOG("101", "개"),
    CAT("102", "강아지"),
    DOVE("201", "비둘기"),
    EAGLE("202", "독수리"),
    SALMON("301", "연어"),
    TUNA("302", "참치"),
    ;
    // @formatter:on
    private final String id;
    private final String text;
  }

  @Schema(name = "AnimalSearchReq", description = "동물 검색 요청")
  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  public static class SearchReq {

    // 검색 조건
    private String name;
    private Category category;
    private Species species;

    // 페이징 파라미터
    private int page = 0;
    private int size = 20;
    private String sort;

    public Pageable toPageable() {
      if (sort != null && !sort.isEmpty()) {
        String[] sortParams = sort.split(",");
        String property = sortParams[0];
        Sort.Direction direction =
            sortParams.length > 1 ? Sort.Direction.fromString(sortParams[1]) : Sort.Direction.ASC;
        return PageRequest.of(page, size, Sort.by(direction, property));
      }
      return PageRequest.of(page, size);
    }
  }
}
