<?php

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

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

        // Laravel 11 native enum modification for subscriptions table
        Schema::table('subscriptions', function (Blueprint $table) use ($allowed_intervals) {
            $table->enum('interval', $allowed_intervals)->change();
        });

        // Laravel 11 native enum modification for subscription_preferences table
        Schema::table('subscription_preferences', function (Blueprint $table) use ($allowed_intervals) {
            $table->enum('interval', $allowed_intervals)->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // Revert to original intervals
        Schema::table('subscriptions', function (Blueprint $table) {
            $table->enum('interval', ['days', 'weeks', 'months'])->change();
        });

        Schema::table('subscription_preferences', function (Blueprint $table) {
            $table->enum('interval', ['days', 'weeks', 'months'])->change();
        });
    }
}



