<?php

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

/**
 * The billing address, as fields rather than as a paragraph.
 *
 * It was one textarea, so what landed in it was whatever somebody typed: a
 * postcode on the street line, a city with no postcode at all, an address with
 * the country missing. An invoice has to name a recipient precisely enough to be
 * a document, and a free-text block cannot be checked, cannot be sorted, and
 * cannot be handed to anything that expects a country.
 *
 * ## The old column stays, and stays authoritative for what is in it
 *
 * `billing_address` is not dropped and not parsed. Guessing which line of an
 * existing block is the street would put a postcode where a street belongs on a
 * document nobody can correct afterwards — the same reason IssueInvoice carries
 * it as lines today. It becomes the COMPOSED form instead: whenever the fields
 * are saved, the block is rewritten from them, so invoices, the console's
 * customer page and everything else that reads it keep working untouched. A
 * record nobody has re-saved keeps the block it always had.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('customers', function (Blueprint $table) {
            $table->string('billing_street')->nullable()->after('billing_address');
            $table->string('billing_postcode', 32)->nullable()->after('billing_street');
            $table->string('billing_city')->nullable()->after('billing_postcode');
            $table->string('billing_country')->nullable()->after('billing_city');
        });
    }

    public function down(): void
    {
        Schema::table('customers', function (Blueprint $table) {
            $table->dropColumn(['billing_street', 'billing_postcode', 'billing_city', 'billing_country']);
        });
    }
};
