-- Game tables: creature cards, the shared map, battle history, and the season.

CREATE TABLE cards (
  token_id TEXT PRIMARY KEY,
  owner TEXT NOT NULL,
  element TEXT NOT NULL,        -- 'fire' | 'water' | 'grass'
  power INTEGER NOT NULL,       -- 1..9
  name TEXT NOT NULL,
  block_height INTEGER NOT NULL
);
CREATE INDEX cards_owner_index ON cards(owner);

CREATE TABLE map_cells (
  x INTEGER NOT NULL,
  y INTEGER NOT NULL,
  token_id TEXT NOT NULL UNIQUE REFERENCES cards(token_id),
  owner TEXT NOT NULL,
  placed_at_height INTEGER NOT NULL,
  PRIMARY KEY (x, y)
);

CREATE TABLE battle_log (
  id SERIAL PRIMARY KEY,
  x INTEGER NOT NULL,
  y INTEGER NOT NULL,
  attacker TEXT NOT NULL,
  defender TEXT NOT NULL,
  attacker_token TEXT NOT NULL,
  defender_token TEXT NOT NULL,
  attacker_total REAL NOT NULL,
  defender_total REAL NOT NULL,
  attacker_won BOOLEAN NOT NULL,
  block_height INTEGER NOT NULL
);

-- Single-row table: the current season (set from the Midnight counter).
CREATE TABLE season (
  id INTEGER PRIMARY KEY CHECK (id = 1),
  element TEXT NOT NULL,        -- 'none' | 'fire' | 'water' | 'grass'
  counter_value INTEGER NOT NULL,
  block_height INTEGER NOT NULL
);
INSERT INTO season (id, element, counter_value, block_height) VALUES (1, 'none', 0, 0);
