CREATE TABLE IF NOT EXISTS disputes (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  deal_id BIGINT NOT NULL,
  opened_by_user_id BIGINT NOT NULL,
  status ENUM('open','evidence','mediation','resolved','cancelled') NOT NULL DEFAULT 'open',
  reason_code VARCHAR(64) NOT NULL,
  summary TEXT NOT NULL,
  requested_outcome VARCHAR(64) NOT NULL,
  final_decision VARCHAR(64) NULL,
  final_reason TEXT NULL,
  decided_by_user_id BIGINT NULL,
  decided_at TIMESTAMP NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (deal_id) REFERENCES deals(id),
  FOREIGN KEY (opened_by_user_id) REFERENCES users(id)
);

CREATE TABLE IF NOT EXISTS dispute_events (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  dispute_id BIGINT NOT NULL,
  event_type VARCHAR(64) NOT NULL,
  actor_user_id BIGINT NOT NULL,
  payload_json JSON NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (dispute_id) REFERENCES disputes(id),
  FOREIGN KEY (actor_user_id) REFERENCES users(id)
);