package com.kamco.cd.training.postgres.entity;

import com.kamco.cd.training.menu.dto.MenuDto;
import com.kamco.cd.training.postgres.CommonDateEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "tb_menu")
public class MenuEntity extends CommonDateEntity {
  @Id
  @Column(name = "menu_uid")
  private String menuUid;

  @Column(name = "menu_nm")
  private String menuNm;

  @Column(name = "menu_url")
  private String menuUrl;

  @Column(name = "description")
  private String description;

  @Column(name = "menu_order")
  private Long menuOrder;

  @NotNull
  @Column(name = "is_use", nullable = true)
  private Boolean isUse = true;

  @NotNull
  @Column(name = "deleted", nullable = false)
  private Boolean deleted = false;

  private Long createdUid;
  private Long updatedUid;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "parent_menu_uid")
  private MenuEntity parent;

  @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  private List<MenuEntity> children = new ArrayList<>();

  @Column(name = "menu_api_uri")
  private String menuApiUri;

  public MenuDto.Basic toDto() {
    return new MenuDto.Basic(
        this.menuUid,
        this.menuNm,
        this.menuUrl,
        this.description,
        this.menuOrder,
        this.isUse,
        this.deleted,
        this.createdUid,
        this.updatedUid,
        this.children.stream().map(MenuEntity::toDto).toList(),
        this.getCreatedDate(),
        this.getModifiedDate(),
        this.menuApiUri);
  }
}
