package com.alterdekim.hearthhack.entity;

import com.alterdekim.hearthhack.dto.UserCardDTO;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "ucards")
@SqlResultSetMapping(
        name = "ucardEntityMapping",
        classes = {
                @ConstructorResult(
                        targetClass = UserCardDTO.class,
                        columns = {
                                @ColumnResult(name = "assetId", type = Long.class),
                                @ColumnResult(name = "isPremium", type = Boolean.class),
                                @ColumnResult(name = "count", type = Integer.class),
                                @ColumnResult(name = "hasSeen", type = Boolean.class)
                        }
                )
        }
)
@NamedNativeQuery(
        name = "UserCard.getCollectionOfUser",
        query = "SELECT u.asset_id as assetId, u.is_premium as isPremium, COUNT(u.id) as count, MIN(u.has_seen) as hasSeen FROM ucards u WHERE u.user_id = :userId GROUP BY u.asset_id, u.is_premium",
        resultSetMapping = "ucardEntityMapping"
)
public class UserCard {

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

    @Column(nullable=false)
    private Long userId;

    @Column(nullable = false)
    private Long assetId;

    @Column(nullable = false)
    private Boolean isPremium;

    @Column(nullable = false)
    private Boolean hasSeen = false;

    public UserCard(Long userId, Long assetId, Boolean isPremium, Boolean hasSeen) {
        this.userId = userId;
        this.assetId = assetId;
        this.isPremium = isPremium;
        this.hasSeen = hasSeen;
    }
}
