package com.hiveops.aria.entity;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import java.time.Instant;
import java.util.Map;
import java.util.UUID;

@Entity
@Table(name = "threat_event")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ThreatEvent {

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

    @Column(name = "device_id")
    private Long deviceId;

    @Column(name = "device_agent_id")
    private String deviceAgentId;

    @Enumerated(EnumType.STRING)
    @Column(name = "event_type", nullable = false, columnDefinition = "threat_event_type")
    private EventType eventType;

    @Column(name = "signal_key", nullable = false)
    private String signalKey;

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "raw_payload", columnDefinition = "jsonb")
    private Map<String, Object> rawPayload;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "matched_ioc_id")
    private ThreatIoc matchedIoc;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, columnDefinition = "threat_severity")
    private ThreatSeverity severity;

    @Column(name = "detected_at", nullable = false)
    @Builder.Default
    private Instant detectedAt = Instant.now();

    @Enumerated(EnumType.STRING)
    @Column(name = "attack_phase", columnDefinition = "attack_phase")
    private AttackPhase attackPhase;

    @Column(name = "sequence_id")
    private UUID sequenceId;

    @Column(name = "institution_key")
    private String institutionKey;

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

    public enum EventType {
        PHYSICAL, OS_EVENT, FILE_SYSTEM, COMPOSITE
    }

    public enum ThreatSeverity {
        CRITICAL, HIGH, MEDIUM, LOW, INFO
    }

    public enum AttackPhase {
        PHYSICAL_ACCESS, MALWARE_STAGING, MALWARE_EXECUTION, PERSISTENCE, CLEANUP
    }
}
