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

import at.procon.dip.architecture.SchemaNames;
import at.procon.dip.domain.document.entity.Document;
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.OneToOne;
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;

/**
 * Shared canonical time-entry fact that can be sourced from multiple external systems.
 */
@Entity
@Table(schema = SchemaNames.TIME, name = "time_entry", indexes = {
        @Index(name = "idx_time_entry_source_system", columnList = "source_system"),
        @Index(name = "idx_time_entry_external_id", columnList = "external_id"),
        @Index(name = "idx_time_entry_start", columnList = "entry_start"),
        @Index(name = "idx_time_entry_end", columnList = "entry_end"),
        @Index(name = "idx_time_entry_source_updated", columnList = "source_updated_at")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TimeEntry {

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

    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "document_id", nullable = false, unique = true)
    private Document document;

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

    @Column(name = "external_id", nullable = false, length = 255)
    private String externalId;

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

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

    @Column(name = "entry_start")
    private OffsetDateTime entryStart;

    @Column(name = "entry_end")
    private OffsetDateTime entryEnd;

    @Column(name = "duration_seconds")
    private Long durationSeconds;

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

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

    @Column(name = "billable")
    private Boolean billable;

    @Column(name = "source_created_at")
    private OffsetDateTime sourceCreatedAt;

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

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

    @Column(name = "raw_status", length = 120)
    private String rawStatus;

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

    @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();
    }
}
