package de.oaa.xxx.games.history;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Getter
@Setter
@Entity
@Table(name = "game_history")
public class GameHistoryEntity {

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

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 20)
    private GameType gameType;

    @Column(nullable = false)
    private LocalDateTime startTime;

    @Column(nullable = false)
    private LocalDateTime endTime;

    @Column
    private String gameName;

    @Column(nullable = false, columnDefinition = "BIGINT DEFAULT 0")
    private long durationMinutes;

    @OneToMany(mappedBy = "history", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    private List<GameHistoryParticipantEntity> participants = new ArrayList<>();

    public void addParticipant(UUID userId, GameRole role) {
        GameHistoryParticipantEntity p = new GameHistoryParticipantEntity();
        p.setUserId(userId);
        p.setRole(role);
        p.setHistory(this);
        participants.add(p);
    }
}
