<?php

/**
 * Migration 0035 — `guest_count` column on bookings
 *
 * Party size for restaurant reservations. A reservation for 4 consumes 4 of a
 * slot's capacity (covers), where appointments consume 1. Defaults to 1 so every
 * existing appointment booking is unchanged and the capacity math (now SUM of
 * guest_count vs the service capacity) behaves exactly as before for salons and
 * services.
 *
 * Idempotent via INFORMATION_SCHEMA guard, same pattern as 0030 / 0031.
 */

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

return [
    'description' => 'Add `guest_count` (party size) column to inst_instinctor_bookings',

    'up' => [],

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

        $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  = 'guest_count'",
                $table
            )
        );
        if (!$colExists) {
            \Instinctor\Core\DB::query(
                "ALTER TABLE {$table}
                 ADD COLUMN guest_count INT NOT NULL DEFAULT 1
                 AFTER end_time"
            );
            if (\Instinctor\Core\DB::last_error() !== '') {
                throw new \RuntimeException(
                    esc_html('guest_count column add failed: ' . \Instinctor\Core\DB::last_error())
                );
            }
        }
    },

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