SET @listing_in_transaction_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'rental_listings'
    AND column_name = 'in_transaction'
);

SET @add_listing_in_transaction_sql := IF(
  @listing_in_transaction_exists = 0,
  'ALTER TABLE rental_listings ADD COLUMN in_transaction TINYINT(1) NOT NULL DEFAULT 0 AFTER deposit_amount',
  'SELECT 1'
);
PREPARE add_listing_in_transaction_stmt FROM @add_listing_in_transaction_sql;
EXECUTE add_listing_in_transaction_stmt;
DEALLOCATE PREPARE add_listing_in_transaction_stmt;

SET @listing_price_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'rental_listings'
    AND column_name = 'price'
);

SET @add_listing_price_sql := IF(
  @listing_price_exists = 0,
  'ALTER TABLE rental_listings ADD COLUMN price DECIMAL(12,2) NOT NULL DEFAULT 0.00 AFTER owner_id',
  'SELECT 1'
);
PREPARE add_listing_price_stmt FROM @add_listing_price_sql;
EXECUTE add_listing_price_stmt;
DEALLOCATE PREPARE add_listing_price_stmt;

UPDATE rental_listings
SET price = CASE
  WHEN price_daily > 0 THEN price_daily
  WHEN price_hourly > 0 THEN price_hourly * 24
  WHEN price_weekly > 0 THEN price_weekly / 7
  ELSE price
END
WHERE price <= 0;

SET @order_rented_at_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'rental_orders'
    AND column_name = 'rented_at'
);

SET @add_order_rented_at_sql := IF(
  @order_rented_at_exists = 0,
  'ALTER TABLE rental_orders ADD COLUMN rented_at DATETIME NULL AFTER renter_id',
  'SELECT 1'
);
PREPARE add_order_rented_at_stmt FROM @add_order_rented_at_sql;
EXECUTE add_order_rented_at_stmt;
DEALLOCATE PREPARE add_order_rented_at_stmt;

SET @order_estimated_duration_exists := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'rental_orders'
    AND column_name = 'estimated_duration_hours'
);

SET @add_order_estimated_duration_sql := IF(
  @order_estimated_duration_exists = 0,
  'ALTER TABLE rental_orders ADD COLUMN estimated_duration_hours INT NOT NULL DEFAULT 24 AFTER rented_at',
  'SELECT 1'
);
PREPARE add_order_estimated_duration_stmt FROM @add_order_estimated_duration_sql;
EXECUTE add_order_estimated_duration_stmt;
DEALLOCATE PREPARE add_order_estimated_duration_stmt;

UPDATE rental_orders
SET
  rented_at = COALESCE(rented_at, rent_start_at),
  estimated_duration_hours = CASE
    WHEN estimated_duration_hours > 0 THEN estimated_duration_hours
    WHEN rent_hours > 0 THEN rent_hours
    ELSE 24
  END
WHERE rented_at IS NULL OR estimated_duration_hours <= 0;

SET @order_rented_at_index_exists := (
  SELECT COUNT(*)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'rental_orders'
    AND index_name = 'idx_rental_orders_rented_at'
);

SET @add_order_rented_at_index_sql := IF(
  @order_rented_at_index_exists = 0,
  'ALTER TABLE rental_orders ADD KEY idx_rental_orders_rented_at (status, rented_at)',
  'SELECT 1'
);
PREPARE add_order_rented_at_index_stmt FROM @add_order_rented_at_index_sql;
EXECUTE add_order_rented_at_index_stmt;
DEALLOCATE PREPARE add_order_rented_at_index_stmt;

INSERT INTO system_configs (`key`, `value`, description)
VALUES ('order.pending_payment_timeout_minutes', '15', '订单待支付超时取消分钟数')
ON DUPLICATE KEY UPDATE `key` = VALUES(`key`);

CREATE TABLE IF NOT EXISTS order_checkouts (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  order_id BIGINT UNSIGNED NOT NULL,
  initiated_by BIGINT UNSIGNED NOT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'submitted',
  rent_amount DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  deposit_amount DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  consumable_amount DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  coin_consumed_m DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  other_amount DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  deposit_deduct_amount DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  renter_refund_amount DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  owner_income_amount DECIMAL(12,2) NOT NULL DEFAULT 0.00,
  content TEXT NULL,
  evidence_urls JSON NULL,
  owner_adjustment_reason TEXT NULL,
  owner_adjusted_at DATETIME NULL,
  renter_confirmed_at DATETIME NULL,
  renter_rejected_at DATETIME NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY idx_order_checkouts_order_id (order_id),
  KEY idx_order_checkouts_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
