<?php

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

/**
 * The plan's marketing copy, moved onto the family it describes.
 *
 * `audience` and `note` were a hardcoded array in LandingController, keyed on
 * exactly the four family keys the seed migration created — a plan created
 * under any other key rendered on the public page with no copy at all. They
 * belong on the family for the same reason the recommendation mark does (see
 * the previous migration): "who this plan is for" does not change with a
 * price, so it must not be frozen per version.
 *
 * Backfilled from the controller's own strings in the same statement that
 * adds the columns, so the public page prints exactly what it printed a
 * moment ago, sourced from the console instead of from code.
 */
return new class extends Migration
{
    /** The exact copy LandingController::COPY held before this migration. */
    private const COPY = [
        'start' => ['audience' => 'Für kleine Teams', 'note' => 'Einstieg für Büros, die heute noch Dateien per Mail schicken.'],
        'team' => ['audience' => 'Für wachsende Teams', 'note' => 'Der Regelfall — Einschulung des gesamten Teams inklusive.'],
        'business' => ['audience' => 'Für dokumentenlastige Betriebe', 'note' => 'Mehr Speicher, längere Aufbewahrung, bevorzugte Reaktionszeit.'],
        'enterprise' => ['audience' => 'Individuell für Ihr Unternehmen', 'note' => 'Umfang, Einrichtung und Reaktionszeiten nach Vereinbarung.'],
    ];

    public function up(): void
    {
        Schema::table('plan_families', function (Blueprint $table) {
            $table->string('audience', 255)->nullable()->after('is_recommended');
            $table->string('note', 500)->nullable()->after('audience');
        });

        foreach (self::COPY as $key => $copy) {
            DB::table('plan_families')->where('key', $key)->update([
                'audience' => $copy['audience'],
                'note' => $copy['note'],
            ]);
        }
    }

    public function down(): void
    {
        Schema::table('plan_families', function (Blueprint $table) {
            $table->dropColumn(['audience', 'note']);
        });
    }
};
