package at.procon.ted.model.entity;

import at.procon.dip.architecture.SchemaNames;
import jakarta.persistence.*;
import lombok.*;

import java.time.OffsetDateTime;
import java.util.UUID;

/**
 * JPA Entity representing an organization mentioned in a procurement notice.
 * Can be buyers, review bodies, service providers, etc.
 * 
 * @author Martin.Schweitzer@procon.co.at and claude.ai
 */
@Entity
@Table(schema = SchemaNames.TED, name = "organization", indexes = {
    @Index(name = "idx_org_document", columnList = "document_id"),
    @Index(name = "idx_org_country", columnList = "country_code")
}, uniqueConstraints = {
    @UniqueConstraint(columnNames = {"document_id", "org_reference"})
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Organization {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "document_id", nullable = false)
    private ProcurementDocument document;

    /**
     * Internal organization reference from XML (e.g., "ORG-0001").
     */
    @Column(name = "org_reference", length = 100)
    private String orgReference;

    /**
     * Role of the organization (e.g., "buyer", "review-body", "ted-esen").
     */
    @Column(name = "role", length = 100)
    private String role;

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

    /**
     * Company/tax registration ID.
     */
    @Column(name = "company_id", columnDefinition = "TEXT")
    private String companyId;

    @Column(name = "country_code", length = 10)
    private String countryCode;

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

    @Column(name = "postal_code", columnDefinition = "TEXT")
    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 = 100)
    private String phone;

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

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