<?php

/**
 * Migration 0055 — customers.email nullable, so a phone-only client can exist
 *
 * The free booking form asks for a name and a phone and nothing else (Sergei
 * 2026-07-26: "Salon owners will NOT use emails for appointment confirmations").
 * The customers table never caught up: email is NOT NULL with a GLOBAL UNIQUE
 * index, so every phone-only booking tried to insert email = ''.
 *
 * The first such booking on the whole install succeeded. Every one after it,
 * on any tenant, hit "Duplicate entry '' for key 'email'", the insert returned
 * false, insert_id() returned 0, and the booking was written with
 * customer_id = 0. The owner's dashboard then showed "Customer #0" instead of
 * the person's name, and their phone number was lost with the failed row.
 *
 * MySQL permits many NULLs in a UNIQUE index, so making the column nullable
 * restores uniqueness where an email actually exists and gets out of the way
 * where one does not. Found 2026-07-27 when the recording's closing shot,
 * which follows a booking into the dashboard, showed "Customer #0".
 *
 * NOTE, deliberately not fixed here: the unique index is global, not per
 * owner, so two tenants still cannot each hold a client with the same real
 * email. That wants a composite (owner_id, email) index and a dedupe pass
 * over existing rows, which is a bigger change than this one.
 *
 * @since 2.1.2596
 */

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

return [
    'description' => 'instinctor_customers.email NULL-able (phone-only clients)',

    'up' => [
        "ALTER TABLE {prefix}instinctor_customers
            MODIFY email varchar(255) NULL DEFAULT NULL",

        // Rows already written as '' are indistinguishable from "no email given".
        // Only one can exist per install (that is the bug), so this touches at
        // most a single row and frees the value for everyone else.
        "UPDATE {prefix}instinctor_customers SET email = NULL WHERE email = ''",
    ],
];
