<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use MtcPharmacy\Subscriptions\Classes\Subscription;
use Illuminate\Support\Facades\DB;


class UpdateSubscriptionStatusesCancelled extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Validate enum values are safe (alphanumeric + hyphen only)
        $allowed_statuses = Subscription::get_allowed_statuses();
        foreach ($allowed_statuses as $status) {
            if (!preg_match('/^[a-z-]+$/', $status)) {
                throw new \Exception("Invalid status value: {$status}");
            }
        }

        // Laravel 11 native enum modification (no doctrine/dbal needed)
        Schema::table('subscriptions', function (Blueprint $table) use ($allowed_statuses) {
            $table->enum('status', $allowed_statuses)
                ->default('pending')
                ->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // Revert to original statuses with 'cancelled' status
        Schema::table('subscriptions', function (Blueprint $table) {
            $table->enum('status', ['pending', 'active', 'paused', 'expired', 'payment-failed', 'cancelled'])
                ->default('pending')
                ->change();
        });
    }
}

