package fr.titionfire.ffsaf.data.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;

import java.util.Date;
import java.util.Objects;

@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@RegisterForReflection

@Entity
@Table(name = "card")
public class CardModel {

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

    Long comb;
    Long match;
    Long category;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "competition", referencedColumnName = "id")
    CompetitionModel competition;

    @JsonProperty("competition")
    Long competitionId;

    CardType type;
    String reason;
    @CreationTimestamp
    Date date;

    @Column(nullable = false, columnDefinition = "boolean default false")
    boolean teamCard = false;

    public boolean hasEffect(MatchModel match) {
        return switch (this.type) {
            case BLUE -> false;
            case YELLOW -> Objects.equals(this.match, match.getId());
            case RED -> Objects.equals(this.category, match.getCategory().getId())
                    || Objects.equals(this.match, match.getId());
            case BLACK -> true;
        };
    }

    public enum CardType {
        BLUE,
        YELLOW,
        RED,
        BLACK
    }
}
