<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Three things a football academy records about a player that had nowhere to go.
 *
 * `jersey_name` — the name printed on the back of the shirt. It is not the
 * player's name: "Mohamed Sherif Mikkawi" wears "Mikkawi", and in a roster of
 * 495 players not one shirt name equalled the full name. `product_customizations`
 * can already ask for it at the till, but that is the answer to one sale and
 * freezes onto that invoice; the club still needs to know what this player's
 * shirt says, before and after any particular kit is bought.
 *
 * `school_name` — asked at registration everywhere, and the thing an academy
 * uses to plan around term times and school fixtures.
 *
 * `previous_clubs` — free text on purpose. A transfer history is a sentence
 * ("Wadi Degla then Zed"), not a foreign key, and inventing a clubs table would
 * demand the desk resolve every spelling before it could record anything.
 *
 * All three are nullable and additive; nothing reads them until something writes
 * them, so every existing tenant is unaffected.
 */
return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('participants')) {
            return;
        }

        Schema::table('participants', function (Blueprint $table) {
            if (! Schema::hasColumn('participants', 'jersey_name')) {
                $table->string('jersey_name', 50)->nullable()->after('jersey_size');
            }

            if (! Schema::hasColumn('participants', 'school_name')) {
                $table->string('school_name', 150)->nullable()->after('jersey_name');
            }

            if (! Schema::hasColumn('participants', 'previous_clubs')) {
                $table->text('previous_clubs')->nullable()->after('school_name');
            }
        });
    }

    public function down(): void
    {
        if (! Schema::hasTable('participants')) {
            return;
        }

        Schema::table('participants', function (Blueprint $table) {
            foreach (['jersey_name', 'school_name', 'previous_clubs'] as $column) {
                if (Schema::hasColumn('participants', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
