package de.oaa.xxx.games.common.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;

import java.util.Base64;
import java.util.UUID;

import de.oaa.xxx.games.common.aufgaben.Toy;

@Getter
@Setter
@Entity
@Table(name = "toy")
public class ToyEntity {

    @Id
    @Column
    private UUID toyId;
    @Column
    private String name;
    @Column
    private String beschreibung;
    @Column
    private UUID userId;
    @Lob
    @Column(columnDefinition = "BLOB")
    private byte[] bild;

    @Override
    public String toString() {
        return "ToyEntity[toyId=" + toyId + ", name=" + name + ", userId=" + userId + "]";
    }

    public Toy toToy() {
        Toy toy = new Toy();
        toy.setToyId(toyId);
        toy.setName(name);
        toy.setBeschreibung(beschreibung);
        toy.setUserId(userId);
        toy.setBild(bild != null ? Base64.getEncoder().encodeToString(bild) : null);
        return toy;
    }

    public static ToyEntity create(Toy toy) {
        ToyEntity entity = new ToyEntity();
        entity.setToyId(UUID.randomUUID());
        entity.setName(toy.getName());
        entity.setBeschreibung(toy.getBeschreibung());
        entity.setUserId(toy.getUserId());
        entity.setBild(toy.getBild() != null ? Base64.getDecoder().decode(toy.getBild()) : null);
        return entity;
    }
}
