<?php

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

/**
 * When the customer asked for the contract to end.
 *
 * A cancellation wrote three columns on the INSTANCE — `cancellation_scheduled`,
 * `cancel_requested_at`, `service_ends_at` — and not one word on the contract the
 * money runs through. So nothing in the billing half of the application could
 * tell a contract somebody had cancelled from one nobody had touched: not the
 * console, not the register, and above all not the code that decides whether a
 * charge arriving from Stripe is one this customer still owes.
 *
 * `cancel_requested_at` and not a status, deliberately. A cancelled package is
 * still a PAYING customer until the term they bought runs out — that distinction
 * is the whole of App\Actions\EndInstanceService and of clupilot:end-due-services
 * — so the contract stays `active`, and the moment it genuinely ends is written
 * by `customer.subscription.deleted` arriving from Stripe into `cancelled_at`
 * beside it. Two columns, two different facts: when it was asked for, and when it
 * happened.
 *
 * No backfill. There is no record anywhere of when an existing cancellation was
 * requested — the instance's own `cancel_requested_at` is the closest thing, and
 * copying it across would state on a contract a fact that was never recorded
 * against it. NULL is the honest answer for the ones that came before this.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('subscriptions', function (Blueprint $table) {
            $table->timestamp('cancel_requested_at')->nullable()->after('stripe_event_rank');
        });
    }

    public function down(): void
    {
        Schema::table('subscriptions', function (Blueprint $table) {
            $table->dropColumn('cancel_requested_at');
        });
    }
};
