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

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
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 ZooDto {

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

    private String name;
    private String location;
    private String description;
  }

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

    @JsonIgnore private Long id;
    private String uuid;
    private String name;
    private String location;
    private String description;

    @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,
        String location,
        String description,
        ZonedDateTime createdDate,
        ZonedDateTime modifiedDate) {
      this.id = id;
      this.uuid = uuid;
      this.name = name;
      this.location = location;
      this.description = description;
      this.createdDate = createdDate;
      this.modifiedDate = modifiedDate;
    }
  }

  @Schema(name = "ZooDetail", description = "동물원 상세 정보 (동물 개수 포함)")
  @Getter
  public static class Detail extends Basic {

    private Long activeAnimalCount;

    public Detail(
        Long id,
        String uuid,
        String name,
        String location,
        String description,
        ZonedDateTime createdDate,
        ZonedDateTime modifiedDate,
        Long activeAnimalCount) {
      super(id, uuid, name, location, description, createdDate, modifiedDate);
      this.activeAnimalCount = activeAnimalCount;
    }
  }

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

    // 검색 조건
    private String name;
    private String location;

    // 페이징 파라미터
    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);
    }
  }
}
