class_name CardInstance
extends RefCounted

## CardInstance - Runtime instance of a card in the game
## Represents a specific card in play with its current state

# Reference to card definition
var card_data: CardDatabase.CardData

# Unique instance ID
var instance_id: int = 0

# Current state
var state: Enums.CardState = Enums.CardState.ACTIVE
var current_power: int = 0
var damage_received: int = 0

# Owner and controller
var owner_index: int = 0  # Player who owns this card (0 or 1)
var controller_index: int = 0  # Player who currently controls this card

# Current zone
var zone_type: Enums.ZoneType = Enums.ZoneType.DECK

# Temporary effects (cleared at end of turn)
var power_modifiers: Array[int] = []
var temporary_abilities: Array = []

# Turn tracking
var turns_on_field: int = 0
var attacked_this_turn: bool = false

# Static counter for unique IDs
static var _next_id: int = 1

func _init(data: CardDatabase.CardData = null, owner: int = 0) -> void:
	card_data = data
	owner_index = owner
	controller_index = owner
	instance_id = _next_id
	_next_id += 1

	if data:
		current_power = data.power

## Get the card's current power (base + modifiers)
func get_power() -> int:
	var total = current_power
	for mod in power_modifiers:
		total += mod
	return max(0, total)

## Check if this is a Forward
func is_forward() -> bool:
	return card_data and card_data.type == Enums.CardType.FORWARD

## Check if this is a Backup
func is_backup() -> bool:
	return card_data and card_data.type == Enums.CardType.BACKUP

## Check if this is a Summon
func is_summon() -> bool:
	return card_data and card_data.type == Enums.CardType.SUMMON

## Check if the card is active (not dull)
func is_active() -> bool:
	return state == Enums.CardState.ACTIVE

## Check if the card is dull
func is_dull() -> bool:
	return state == Enums.CardState.DULL

## Dull this card
func dull() -> void:
	state = Enums.CardState.DULL

## Activate this card
func activate() -> void:
	state = Enums.CardState.ACTIVE

## Check if this card can attack
func can_attack() -> bool:
	if not is_forward():
		return false
	if is_dull():
		return false
	if attacked_this_turn:
		return false
	# Must have been on field since start of turn (or have Haste)
	if turns_on_field < 1 and not has_haste():
		return false
	return true

## Check if this card can block
func can_block() -> bool:
	if not is_forward():
		return false
	if is_dull():
		return false
	return true

## Check if this card can use dull abilities
func can_use_dull_ability() -> bool:
	# Must have been on field since start of turn (or have Haste)
	# Monsters are exception
	if card_data.type == Enums.CardType.MONSTER:
		return true
	if turns_on_field < 1 and not has_haste():
		return false
	return true

## Check if card has Haste (from abilities)
func has_haste() -> bool:
	if not card_data:
		return false
	for ability in card_data.abilities:
		if ability.type == Enums.AbilityType.FIELD:
			if "haste" in ability.effect.to_lower():
				return true
	return false

## Check if card has Brave (from abilities)
func has_brave() -> bool:
	if not card_data:
		return false
	for ability in card_data.abilities:
		if ability.type == Enums.AbilityType.FIELD:
			if "brave" in ability.effect.to_lower():
				return true
	return false

## Check if card has First Strike
func has_first_strike() -> bool:
	if not card_data:
		return false
	for ability in card_data.abilities:
		if ability.type == Enums.AbilityType.FIELD:
			if "first strike" in ability.effect.to_lower():
				return true
	return false

## Get primary element
func get_element() -> Enums.Element:
	if card_data:
		return card_data.get_primary_element()
	return Enums.Element.FIRE

## Get all elements
func get_elements() -> Array[Enums.Element]:
	if card_data:
		return card_data.elements
	return []

## Check if card is Light or Dark element
func is_light_or_dark() -> bool:
	for element in get_elements():
		if Enums.is_light_or_dark(element):
			return true
	return false

## Apply damage to this Forward
func apply_damage(amount: int) -> bool:
	if not is_forward():
		return false
	damage_received += amount
	# Check if broken (damage >= power)
	return damage_received >= get_power()

## Reset temporary effects at end of turn
func end_turn_cleanup() -> void:
	power_modifiers.clear()
	temporary_abilities.clear()
	damage_received = 0
	attacked_this_turn = false

## Called when card enters field
func entered_field() -> void:
	turns_on_field = 0
	attacked_this_turn = false
	damage_received = 0

## Called at start of each turn
func start_turn() -> void:
	turns_on_field += 1

## Get a display string for this card
func get_display_name() -> String:
	if card_data:
		return card_data.name
	return "Unknown Card"

func _to_string() -> String:
	return "[CardInstance: %s (%s)]" % [get_display_name(), instance_id]
