CREATE TABLE IF NOT EXISTS payment_requests (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  telegram_id BIGINT NOT NULL,
  chat_id BIGINT NOT NULL,
  username VARCHAR(255) NULL,
  first_name VARCHAR(255) NULL,
  amount DECIMAL(12,2) NOT NULL,
  currency VARCHAR(10) NOT NULL DEFAULT 'EUR',
  method VARCHAR(50) NOT NULL DEFAULT 'paypal_card',
  status VARCHAR(50) NOT NULL DEFAULT 'created',
  paypal_order_id VARCHAR(255) NULL,
  paypal_capture_id VARCHAR(255) NULL,
  payer_email VARCHAR(255) NULL,
  raw_response MEDIUMTEXT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  paid_at TIMESTAMP NULL,
  INDEX idx_telegram_id (telegram_id),
  INDEX idx_paypal_order_id (paypal_order_id),
  INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS bot_sessions (
  telegram_id BIGINT PRIMARY KEY,
  chat_id BIGINT NOT NULL,
  step VARCHAR(100) NOT NULL,
  amount DECIMAL(12,2) NULL,
  currency VARCHAR(10) DEFAULT 'EUR',
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS paypal_webhook_logs (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  event_id VARCHAR(255) NULL,
  event_type VARCHAR(255) NULL,
  paypal_order_id VARCHAR(255) NULL,
  payload MEDIUMTEXT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uniq_event_id (event_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
