-- Checkout idempotency: aynı Idempotency-Key ile tekrarlanan istekler aynı siparişi döner.
-- Idempotent; mevcut şemayı bozmaz. Paket A (P0).

CREATE TABLE IF NOT EXISTS ecommerce_checkout_idempotency (
  idempotency_key VARCHAR(64) NOT NULL,
  cart_fingerprint CHAR(64) NOT NULL,
  order_id BIGINT UNSIGNED NOT NULL,
  checkout_url TEXT NULL DEFAULT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  expires_at TIMESTAMP NOT NULL,
  PRIMARY KEY (idempotency_key),
  KEY idx_checkout_idem_expires (expires_at),
  KEY idx_checkout_idem_fingerprint_created (cart_fingerprint, created_at),
  CONSTRAINT fk_checkout_idem_order FOREIGN KEY (order_id) REFERENCES ecommerce_orders(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
