package ru.soune.nocopy.dto.violation;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Data;
import ru.soune.nocopy.entity.violation.Violation;
import ru.soune.nocopy.service.geo.GeoCountryService;

import java.time.LocalDateTime;
import java.util.List;

@Data
@Builder
public class ViolationResponse {

    private final GeoCountryService geoCountryService;

    @JsonProperty("violations")
    private List<ViolationDto> violations;

    @JsonProperty("total_elements")
    private long totalElements;

    @JsonProperty("total_pages")
    private int totalPages;

    @JsonProperty("current_page")
    private int currentPage;

    @JsonProperty("page_size")
    private int pageSize;

    @JsonProperty("has_next")
    private boolean hasNext;

    @JsonProperty("has_previous")
    private boolean hasPrevious;

    @Data
    @Builder
    public static class ViolationDto {

        @JsonProperty("id")
        private Long id;

        @JsonProperty("url")
        private String url;

        @JsonProperty("page_url")
        private String pageUrl;

        @JsonProperty("page_title")
        private String pageTitle;

        @JsonProperty("host")
        private String host;

        @JsonProperty("status")
        private String status;

        @JsonProperty("created_date")
        private LocalDateTime createdDate;

        @JsonProperty("country")
        private String country;

        @JsonProperty("country_code")
        private String countryCode;

        @JsonProperty("file_id")
        private String fileId;

        public static ViolationDto fromEntity(Violation violation) {
            return ViolationDto.builder()
                    .id(violation.getId())
                    .url(violation.getUrl())
                    .pageUrl(violation.getPageUrl())
                    .pageTitle(violation.getPageTitle())
                    .host(violation.getHost())
                    .status(violation.getStatus())
                    .createdDate(violation.getCreatedDate())
                    .fileId(violation.getFileEntity() != null ? violation.getFileEntity().getId() : null)
                    .build();
        }
    }
}
