package at.procon.dip.domain.ted.entity;

import at.procon.dip.architecture.SchemaNames;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

@Entity
@Table(schema = SchemaNames.TED, name = "ted_notice_lot", indexes = {
        @Index(name = "idx_ted_notice_lot_projection", columnList = "notice_projection_id")
}, uniqueConstraints = {
        @UniqueConstraint(name = "uq_ted_notice_lot_projection_lot", columnNames = {"notice_projection_id", "lot_id"})
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TedNoticeLot {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "notice_projection_id", nullable = false)
    private TedNoticeProjection noticeProjection;

    @Column(name = "lot_id", nullable = false, length = 50)
    private String lotId;

    @Column(name = "internal_id", columnDefinition = "TEXT")
    private String internalId;

    @Column(name = "title", columnDefinition = "TEXT")
    private String title;

    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    @Column(name = "cpv_codes", columnDefinition = "VARCHAR(100)[]")
    @JdbcTypeCode(SqlTypes.ARRAY)
    private String[] cpvCodes;

    @Column(name = "nuts_codes", columnDefinition = "VARCHAR(20)[]")
    @JdbcTypeCode(SqlTypes.ARRAY)
    private String[] nutsCodes;

    @Column(name = "estimated_value", precision = 20, scale = 2)
    private BigDecimal estimatedValue;

    @Column(name = "estimated_value_currency", length = 3)
    private String estimatedValueCurrency;

    @Column(name = "duration_value")
    private Double durationValue;

    @Column(name = "duration_unit", length = 20)
    private String durationUnit;

    @Column(name = "submission_deadline")
    private OffsetDateTime submissionDeadline;

    @Column(name = "eu_funded")
    @Builder.Default
    private Boolean euFunded = false;

    @Builder.Default
    @Column(name = "created_at", nullable = false, updatable = false)
    private OffsetDateTime createdAt = OffsetDateTime.now();

    @PrePersist
    protected void onCreate() {
        createdAt = OffsetDateTime.now();
    }
}
