SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS listing_payment_links (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  listing_id INT UNSIGNED NOT NULL,
  business_user_id INT UNSIGNED DEFAULT NULL,
  admin_id INT UNSIGNED DEFAULT NULL,
  contact_email VARCHAR(255) DEFAULT NULL,
  addon_keys_csv VARCHAR(120) NOT NULL,
  addon_amounts_json TEXT DEFAULT NULL,
  custom_total_cents INT UNSIGNED NOT NULL DEFAULT 0,
  currency CHAR(3) NOT NULL DEFAULT 'eur',
  token VARCHAR(64) NOT NULL UNIQUE,
  status ENUM('pending','checkout_started','paid','cancelled','expired') NOT NULL DEFAULT 'pending',
  stripe_checkout_session_id VARCHAR(255) DEFAULT NULL,
  stripe_subscription_id VARCHAR(255) DEFAULT NULL,
  paid_at DATETIME DEFAULT NULL,
  expires_at DATETIME DEFAULT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY idx_listing_payment_links_listing (listing_id, created_at),
  KEY idx_listing_payment_links_status (status),
  CONSTRAINT fk_listing_payment_links_listing FOREIGN KEY (listing_id) REFERENCES listings(id) ON DELETE CASCADE,
  CONSTRAINT fk_listing_payment_links_business FOREIGN KEY (business_user_id) REFERENCES business_users(id) ON DELETE SET NULL,
  CONSTRAINT fk_listing_payment_links_admin FOREIGN KEY (admin_id) REFERENCES admins(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
