package at.procon.ted.model.dto;

import at.procon.ted.model.entity.ContractNature;
import at.procon.ted.model.entity.NoticeType;
import at.procon.ted.model.entity.ProcedureType;
import at.procon.ted.model.entity.VectorizationStatus;
import at.procon.dip.domain.ted.search.dto.TedStructuredSearchFacets;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.UUID;

/**
 * DTOs for procurement document API responses.
 * 
 * @author Martin.Schweitzer@procon.co.at and claude.ai
 */
public class DocumentDtos {

    /**
     * Summary DTO for list views and search results.
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class DocumentSummary {
        private UUID id;
        private String publicationId;
        private String noticeId;
        private NoticeType noticeType;
        private String projectTitle;
        private String buyerName;
        private String buyerCountryCode;
        private String buyerCity;
        private ContractNature contractNature;
        private ProcedureType procedureType;
        private LocalDate publicationDate;
        private OffsetDateTime submissionDeadline;
        private List<String> cpvCodes;
        private Integer totalLots;
        private BigDecimal estimatedValue;
        private String estimatedValueCurrency;
        private Double similarity; // For semantic search results
    }

    /**
     * Detailed DTO for single document view.
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class DocumentDetail {
        private UUID id;
        private String documentHash;
        
        // Identifiers
        private String publicationId;
        private String noticeId;
        private String ojsId;
        private String contractFolderId;
        
        // Classification
        private NoticeType noticeType;
        private String noticeSubtypeCode;
        private String sdkVersion;
        private String ublVersion;
        private String languageCode;

        // Dates
        private OffsetDateTime issueDateTime;
        private LocalDate publicationDate;
        private OffsetDateTime submissionDeadline;
        
        // Buyer information
        private String buyerName;
        private String buyerCountryCode;
        private String buyerCity;
        private String buyerPostalCode;
        private String buyerNutsCode;
        private String buyerActivityType;
        private String buyerLegalType;
        
        // Project information
        private String projectTitle;
        private String projectDescription;
        private String internalReference;
        private ContractNature contractNature;
        private ProcedureType procedureType;
        
        // Classification codes
        private List<String> cpvCodes;
        private List<String> nutsCodes;
        
        // Financial
        private BigDecimal estimatedValue;
        private String estimatedValueCurrency;
        
        // Lots
        private Integer totalLots;
        private Integer maxLotsAwarded;
        private Integer maxLotsSubmitted;
        private List<LotSummary> lots;
        
        // Organizations
        private List<OrganizationSummary> organizations;
        
        // Legal
        private String regulatoryDomain;
        private Boolean euFunded;
        
        // Vectorization
        private VectorizationStatus vectorizationStatus;
        private OffsetDateTime vectorizedAt;
        
        // Metadata
        private String sourceFilename;
        private Long fileSizeBytes;
        private OffsetDateTime createdAt;
        private OffsetDateTime updatedAt;
    }

    /**
     * Lot summary for document detail view.
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class LotSummary {
        private UUID id;
        private String lotId;
        private String internalId;
        private String title;
        private String description;
        private List<String> cpvCodes;
        private List<String> nutsCodes;
        private BigDecimal estimatedValue;
        private String estimatedValueCurrency;
        private Double durationValue;
        private String durationUnit;
        private OffsetDateTime submissionDeadline;
        private Boolean euFunded;
    }

    /**
     * Organization summary for document detail view.
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class OrganizationSummary {
        private UUID id;
        private String orgReference;
        private String role;
        private String name;
        private String companyId;
        private String countryCode;
        private String city;
        private String postalCode;
        private String nutsCode;
        private String websiteUri;
        private String email;
        private String phone;
    }

    /**
     * Search request for structured + semantic search.
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class SearchRequest {
        // Structured filters
        private String countryCode;
        private List<String> countryCodes;
        private NoticeType noticeType;
        private ContractNature contractNature;
        private ProcedureType procedureType;
        private String cpvPrefix;
        private List<String> cpvCodes;
        private String nutsCode;
        private List<String> nutsCodes;
        private LocalDate publicationDateFrom;
        private LocalDate publicationDateTo;
        private OffsetDateTime submissionDeadlineAfter;
        private Boolean euFunded;
        private String buyerNameContains;
        private String projectTitleContains;
        
        // Semantic search
        private String semanticQuery;
        private Double similarityThreshold;

        // Additional options
        private Boolean includeFacets;
        private Integer facetBucketLimit;
        
        // Pagination
        private Integer page;
        private Integer size;
        private String sortBy;
        private String sortDirection;
    }

    /**
     * Search response with pagination.
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class SearchResponse {
        private List<DocumentSummary> documents;
        private int page;
        private int size;
        private long totalElements;
        private int totalPages;
        private boolean hasNext;
        private boolean hasPrevious;
        private TedStructuredSearchFacets facets;
    }

    /**
     * Statistics response.
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class StatisticsResponse {
        private long totalDocuments;
        private long vectorizedDocuments;
        private long pendingVectorization;
        private long failedVectorization;
        private int uniqueCountries;
        private LocalDate earliestPublication;
        private LocalDate latestPublication;
        private long totalLots;
        private List<CountryStats> countryStatistics;
        private List<NoticeTypeStats> noticeTypeStatistics;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CountryStats {
        private String countryCode;
        private long documentCount;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class NoticeTypeStats {
        private NoticeType noticeType;
        private long documentCount;
    }
}
