<?php

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

class RedesignVcMedicinesRemoveStock extends Migration
{
    public function up()
    {
        if (!Schema::hasTable('vc_medicines')) return;

        // Drop stock-tracking columns no longer needed
        $dropCols = [];
        foreach (['unit_packing', 'storage_location', 'min_stock_level', 'reorder_level'] as $col) {
            if (Schema::hasColumn('vc_medicines', $col)) {
                $dropCols[] = $col;
            }
        }
        if ($dropCols) {
            Schema::table('vc_medicines', function (Blueprint $table) use ($dropCols) {
                $table->dropColumn($dropCols);
            });
        }

        // Add product_id link to ERP products table
        Schema::table('vc_medicines', function (Blueprint $table) {
            if (!Schema::hasColumn('vc_medicines', 'product_id')) {
                $table->unsignedBigInteger('product_id')->nullable()->after('category_id');
            }
        });

        // Drop batch inventory table — stock managed by ERP
        if (Schema::hasTable('vc_medicine_batches')) {
            Schema::dropIfExists('vc_medicine_batches');
        }
    }

    public function down()
    {
        Schema::table('vc_medicines', function (Blueprint $table) {
            $table->string('unit_packing', 100)->nullable();
            $table->string('storage_location', 100)->nullable();
            $table->integer('min_stock_level')->default(0);
            $table->integer('reorder_level')->default(0);
        });
    }
}
