<?php

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

class RemoveKashflowColumns extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Remove Kashflow columns from clients table
        if (Schema::hasColumn('clients', 'kashflow_id')) {
            Schema::table('clients', function (Blueprint $table) {
                $table->dropColumn('kashflow_id');
            });
        }

        // Remove Kashflow columns from customers table
        if (Schema::hasColumn('customers', 'kashflow_id')) {
            Schema::table('customers', function (Blueprint $table) {
                $table->dropColumn('kashflow_id');
            });
        }

        // Remove Kashflow columns from invoice_transactions table
        if (Schema::hasColumn('invoice_transactions', 'kashflow_id')) {
            Schema::table('invoice_transactions', function (Blueprint $table) {
                $table->dropColumn('kashflow_id');
            });
        }

        // Remove Kashflow columns from stock_groups table
        if (Schema::hasColumn('stock_groups', 'kashflow_category_id')) {
            Schema::table('stock_groups', function (Blueprint $table) {
                $table->dropColumn('kashflow_category_id');
            });
        }
        if (Schema::hasColumn('stock_groups', 'kashflow_purchase_category_id')) {
            Schema::table('stock_groups', function (Blueprint $table) {
                $table->dropColumn('kashflow_purchase_category_id');
            });
        }

        // Remove Kashflow columns from stock table
        if (Schema::hasColumn('stock', 'kashflow_category_id')) {
            Schema::table('stock', function (Blueprint $table) {
                $table->dropColumn('kashflow_category_id');
            });
        }
        if (Schema::hasColumn('stock', 'kashflow_purchase_category_id')) {
            Schema::table('stock', function (Blueprint $table) {
                $table->dropColumn('kashflow_purchase_category_id');
            });
        }

        // Remove Kashflow columns from suppliers table
        if (Schema::hasColumn('suppliers', 'kashflow_id')) {
            Schema::table('suppliers', function (Blueprint $table) {
                $table->dropColumn('kashflow_id');
            });
        }

        // Remove Kashflow columns from stock_purchase_orders table
        if (Schema::hasColumn('stock_purchase_orders', 'kashflow_id')) {
            Schema::table('stock_purchase_orders', function (Blueprint $table) {
                $table->dropColumn('kashflow_id');
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // Add back Kashflow columns to clients table
        Schema::table('clients', function (Blueprint $table) {
            $table->unsignedBigInteger('kashflow_id')->nullable();
        });

        // Add back Kashflow columns to customers table
        Schema::table('customers', function (Blueprint $table) {
            $table->unsignedBigInteger('kashflow_id')->nullable();
        });

        // Add back Kashflow columns to invoice_transactions table
        Schema::table('invoice_transactions', function (Blueprint $table) {
            $table->unsignedBigInteger('kashflow_id')->nullable();
        });

        // Add back Kashflow columns to stock_groups table
        Schema::table('stock_groups', function (Blueprint $table) {
            $table->string('kashflow_category_id', 16)->nullable();
            $table->string('kashflow_purchase_category_id', 16)->nullable();
        });

        // Add back Kashflow columns to stock table
        Schema::table('stock', function (Blueprint $table) {
            $table->string('kashflow_category_id', 16)->nullable();
            $table->string('kashflow_purchase_category_id', 16)->nullable();
        });

        // Add back Kashflow columns to suppliers table
        Schema::table('suppliers', function (Blueprint $table) {
            $table->unsignedBigInteger('kashflow_id')->nullable();
        });

        // Add back Kashflow columns to stock_purchase_orders table
        Schema::table('stock_purchase_orders', function (Blueprint $table) {
            $table->unsignedBigInteger('kashflow_id')->nullable();
        });
    }
} 