package com.oguerreiro.resilient.service.dto.security;

import java.util.ArrayList;
import java.util.List;

import com.oguerreiro.resilient.security.resillient.SecurityGroup;
import com.oguerreiro.resilient.service.dto.AbstractResilientDTO;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;

/**
 * A DTO for the {@link com.oguerreiro.resilient.security.SecurityGroup} entity.
 */
@Schema(description = "SecurityGroup")
public class SecurityGroupDTO extends AbstractResilientDTO<Long> {

  private static final long serialVersionUID = 1L;

  @NotNull
  @Schema(description = "Required and unique code for the group", requiredMode = Schema.RequiredMode.REQUIRED)
  private String code;

  private String name;

  private List<SecurityGroupPermissionDTO> securityGroupPermissions = new ArrayList<>();

  // Needed for Jackson
  public SecurityGroupDTO() {
  }

  public SecurityGroupDTO(SecurityGroup securityGroup) {
    if (securityGroup == null)
      return;

    this.setId(securityGroup.getId());
    this.setVersion(securityGroup.getVersion());
    this.code = securityGroup.getCode();
    this.name = securityGroup.getName();

    this.securityGroupPermissions = securityGroup.getSecurityGroupPermissions().stream().map(
        SecurityGroupPermissionDTO::new).toList();
  }

  public String getCode() {
    return code;
  }

  public void setCode(String code) {
    this.code = code;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public List<SecurityGroupPermissionDTO> getSecurityGroupPermissions() {
    return securityGroupPermissions;
  }

  public void setSecurityGroupPermissions(List<SecurityGroupPermissionDTO> securityGroupPermissions) {
    this.securityGroupPermissions = securityGroupPermissions;
  }

  //@formatter:off
  @Override
  public String toString() {
    return "SecurityGroupDTO{" + 
              "id=" + getId() + 
              ", code='" + getCode() + "'" + 
              ", name='" + getName() + "'" +
              ", version=" + getVersion() + 
           "}";
  }
//@formatter:on
}
