# src/core/card.gd
# Pure data — no Godot dependencies

class_name Card
extends RefCounted

const MIN_ID := 0
const MAX_ID := 107

var card_id: int
var original_id: int
var _suit: int
var _rank: int

static func create(original_id: int, suit: int, rank: int) -> Card:
    var c := Card.new()
    c.original_id = original_id
    c._suit = suit
    c._rank = rank
    c.card_id = original_id
    return c

static func make_full_deck_ids() -> Array[int]:
    var ids: Array[int] = []
    for i in range(108):
        ids.append(i)
    return ids

static func card_id_from_deck(original_id: int, deck_index: int) -> int:
    return original_id + deck_index * 54

func suit() -> int:
    return _suit

func rank() -> int:
    return _rank

func card_value() -> int:
    return _suit * 20 + _rank

func is_joker() -> bool:
    return _rank == 15 or _rank == 16

func is_heart() -> bool:
    return _suit == 1

func matches(other: Card) -> bool:
    return card_value() == other.card_value()

func equals(other: Card) -> bool:
    return card_id == other.card_id

func compare_to(other: Card) -> int:
    var r := _rank - other._rank
    if r != 0:
        return r
    return _suit - other._suit

func to_packed() -> int:
    return (_suit << 8) | (_rank & 0xFF)

static func from_packed(packed: int) -> Card:
    var suit := (packed >> 8) & 0xFF
    var rank := packed & 0xFF
    return Card.create(0, suit, rank)

func _to_string() -> String:
    var suits := ["S", "H", "C", "D", "SJ", "BJ"]
    var ranks := ["", "", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "SJ", "BJ"]
    return "%s%s" % [suits[_suit], ranks[_rank]]
