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.time.OffsetDateTime;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(schema = SchemaNames.TED, name = "ted_notice_organization", indexes = {
        @Index(name = "idx_ted_notice_org_projection", columnList = "notice_projection_id"),
        @Index(name = "idx_ted_notice_org_country", columnList = "country_code")
}, uniqueConstraints = {
        @UniqueConstraint(name = "uq_ted_notice_org_projection_ref", columnNames = {"notice_projection_id", "org_reference"})
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TedNoticeOrganization {

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

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

    @Column(name = "org_reference", length = 50)
    private String orgReference;

    @Column(name = "role", length = 50)
    private String role;

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

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

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

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

    @Column(name = "postal_code", length = 255)
    private String postalCode;

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

    @Column(name = "nuts_code", length = 10)
    private String nutsCode;

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

    @Column(name = "email", length = 255)
    private String email;

    @Column(name = "phone", length = 50)
    private String phone;

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

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