-- CY.CASH V7.2 MATURITY ENGINE
-- Import ONLY into the separate development database (for example fcaglobal_dev).
-- Do NOT run against the live database.

ALTER TABLE cy_investment_orders
  MODIFY status ENUM('processing','active','completed','failed') NOT NULL DEFAULT 'processing';

CREATE TABLE IF NOT EXISTS cy_investment_payouts (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id BIGINT NOT NULL,
  order_id BIGINT NOT NULL,
  deposit_id BIGINT NOT NULL,
  payout_type ENUM('earning','principal') NOT NULL,
  amount DECIMAL(20,10) NOT NULL,
  batch_code VARCHAR(64) NOT NULL,
  status ENUM('processing','credited','skipped','failed') NOT NULL DEFAULT 'processing',
  history_id BIGINT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  credited_at DATETIME NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_deposit_payout (deposit_id,payout_type),
  UNIQUE KEY uq_batch_code (batch_code),
  KEY user_status (user_id,status),
  KEY order_id (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
