<?php

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

/**
 * Give a branch the three things a public website needs to present it and the
 * `branches` table never stored: a photo, and the window of the year it is
 * actually open for.
 *
 * The website builder's branch block already read `photo_path` — a column that
 * did not exist — so every site that enabled branch images silently rendered
 * none. This is the column it was always reaching for.
 *
 * The season window is nullable on purpose. A null window means "open all
 * year", which is what every existing branch is, so no backfill is required
 * and no live site changes appearance on deploy.
 */
return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('branches')) {
            return;
        }

        Schema::table('branches', function (Blueprint $table) {
            if (! Schema::hasColumn('branches', 'photo_path')) {
                $table->string('photo_path')->nullable()->after('longitude');
            }
            if (! Schema::hasColumn('branches', 'season_starts_on')) {
                $table->date('season_starts_on')->nullable()->after('photo_path');
            }
            if (! Schema::hasColumn('branches', 'season_ends_on')) {
                $table->date('season_ends_on')->nullable()->after('season_starts_on');
            }
        });
    }

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

        Schema::table('branches', function (Blueprint $table) {
            foreach (['season_ends_on', 'season_starts_on', 'photo_path'] as $column) {
                if (Schema::hasColumn('branches', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
