-- Comercio de PD (/trade-points): códigos de 1 solo uso PD<->oro.
-- BD: django_wow (DB.default). Ver lib/trade.ts.
CREATE TABLE IF NOT EXISTS home_tradecode (
  id                 BIGINT      NOT NULL AUTO_INCREMENT,
  code               VARCHAR(25) NOT NULL,
  creator_account    INT UNSIGNED NOT NULL,
  creator_character  VARCHAR(12) NOT NULL,   -- personaje del creador que recibe el oro
  points             INT         NOT NULL,   -- valor en PD
  gold               INT         NOT NULL,   -- coste en oro
  status             ENUM('active','redeemed','expired','cancelled') NOT NULL DEFAULT 'active',
  redeemer_account   INT UNSIGNED NULL,
  redeemer_character VARCHAR(12) NULL,        -- personaje del comprador que paga el oro
  created_at         DATETIME    NOT NULL,
  expires_at         DATETIME    NOT NULL,
  redeemed_at        DATETIME    NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_code (code),
  KEY idx_creator_status (creator_account, status),
  KEY idx_redeemer (redeemer_account, redeemed_at),
  KEY idx_status_expires (status, expires_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
