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.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
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;

/**
 * Audit row for one synchronization run of a time-entry source system.
 */
@Entity
@Table(schema = SchemaNames.TIME, name = "time_sync_run", indexes = {
        @Index(name = "idx_time_sync_run_source_system", columnList = "source_system"),
        @Index(name = "idx_time_sync_run_scope_key", columnList = "scope_key"),
        @Index(name = "idx_time_sync_run_status", columnList = "status"),
        @Index(name = "idx_time_sync_run_started_at", columnList = "started_at")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TimeSyncRun {

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

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

    @Enumerated(EnumType.STRING)
    @JdbcTypeCode(SqlTypes.NAMED_ENUM)
    @Column(name = "run_type", columnDefinition = "TIME.time_sync_run_type")
    private TimeSyncRunType runType;

    @Column(name = "scope_key", nullable = false, length = 255)
    private String scopeKey;

    @Enumerated(EnumType.STRING)
    @JdbcTypeCode(SqlTypes.NAMED_ENUM)
    @Column(name = "status", columnDefinition = "TIME.time_sync_run_status")
    private TimeSyncRunStatus status;

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

    @Column(name = "finished_at")
    private OffsetDateTime finishedAt;

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

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

    @Builder.Default
    @Column(name = "rows_read", nullable = false)
    private Integer rowsRead = 0;

    @Builder.Default
    @Column(name = "rows_created", nullable = false)
    private Integer rowsCreated = 0;

    @Builder.Default
    @Column(name = "rows_updated", nullable = false)
    private Integer rowsUpdated = 0;

    @Builder.Default
    @Column(name = "rows_deactivated", nullable = false)
    private Integer rowsDeactivated = 0;

    @Builder.Default
    @Column(name = "rows_failed", nullable = false)
    private Integer rowsFailed = 0;

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

    @Column(name = "initiated_by", length = 120)
    private String initiatedBy;

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

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

    @PreUpdate
    protected void onUpdate() {
        if (finishedAt == null && status != TimeSyncRunStatus.RUNNING) {
            finishedAt = OffsetDateTime.now();
        }
    }
}
