<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Add status column if it doesn't exist
        if (!Schema::hasColumn('courierapis', 'status')) {
            Schema::table('courierapis', function (Blueprint $table) {
                $table->integer('status')->default(0)->after('token');
            });
        }

        // Seed RedX Courier record
        DB::table('courierapis')->updateOrInsert(
            ['type' => 'redx'],
            [
                'type' => 'redx',
                'api_key' => '',
                'secret_key' => '',
                'url' => '',
                'token' => '',
                'status' => 0,
                'created_at' => now(),
                'updated_at' => now(),
            ]
        );
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // Delete RedX record
        DB::table('courierapis')->where('type', 'redx')->delete();
    }
};
