<?php

/**
 * Migration 0032 — `deposit` column on services
 *
 * Per-service booking deposit (flat dollar amount). When set and the booking
 * settings require payment, the customer pays this amount upfront via Stripe to
 * hold the appointment; it is credited toward the service and forfeited on a
 * late cancellation / no-show. NULL or 0 = no deposit for that service.
 *
 * Idempotent via INFORMATION_SCHEMA guard, same pattern as 0030 / 0031.
 */

if ( ! defined( 'ABSPATH' ) ) exit;

return [
    'description' => 'Add `deposit` column to inst_instinctor_services',

    'up' => [],

    'php' => function () {
        $table = \Instinctor\Core\DB::prefix() . 'instinctor_services';

        $colExists = \Instinctor\Core\DB::get_var(
            \Instinctor\Core\DB::prepare(
                "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
                 WHERE TABLE_SCHEMA = DATABASE()
                   AND TABLE_NAME   = %s
                   AND COLUMN_NAME  = 'deposit'",
                $table
            )
        );
        if (!$colExists) {
            \Instinctor\Core\DB::query(
                "ALTER TABLE {$table}
                 ADD COLUMN deposit DECIMAL(10,2) DEFAULT NULL
                 AFTER price"
            );
            if (\Instinctor\Core\DB::last_error() !== '') {
                throw new \RuntimeException(
                    esc_html('deposit column add failed: '
                    . \Instinctor\Core\DB::last_error())
                );
            }
        }
    },

    'down' => [
        "ALTER TABLE {prefix}instinctor_services DROP COLUMN IF EXISTS deposit",
    ],
];
