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

import at.procon.dip.architecture.SchemaNames;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
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.PreUpdate;
import jakarta.persistence.Table;
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;

/**
 * Reverse-link helper that maps a canonical time entry to one or more source-system entity identifiers.
 */
@Entity
@Table(schema = SchemaNames.TIME, name = "time_entry_source_link", indexes = {
        @Index(name = "idx_time_entry_link_entry", columnList = "time_entry_id"),
        @Index(name = "idx_time_entry_link_source_lookup", columnList = "source_system, source_entity_type, source_external_id"),
        @Index(name = "idx_time_entry_link_role", columnList = "linked_role")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TimeEntrySourceLink {

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

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "time_entry_id", nullable = false)
    private TimeEntry timeEntry;

    @Enumerated(EnumType.STRING)
    @JdbcTypeCode(SqlTypes.NAMED_ENUM)
    @Column(name = "source_system", columnDefinition = "TIME.time_source_system")
    private TimeSourceSystem sourceSystem;

    @Column(name = "source_entity_type", nullable = false, length = 120)
    private String sourceEntityType;

    @Column(name = "source_external_id", nullable = false, length = 255)
    private String sourceExternalId;

    @Column(name = "linked_role", length = 120)
    private String linkedRole;

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

    @Column(name = "source_updated_at")
    private OffsetDateTime sourceUpdatedAt;

    @Column(name = "source_deleted_at")
    private OffsetDateTime sourceDeletedAt;

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

    @Builder.Default
    @Column(name = "updated_at", nullable = false)
    private OffsetDateTime updatedAt = OffsetDateTime.now();

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

    @PreUpdate
    protected void onUpdate() {
        updatedAt = OffsetDateTime.now();
    }
}
