<?php

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

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

        Schema::create('vc_medicines', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id');
            $table->unsignedInteger('category_id')->nullable();
            $table->string('name', 150);
            $table->string('generic_name', 150)->nullable();
            $table->string('company', 100)->nullable();
            $table->string('unit', 50)->nullable();
            $table->string('unit_packing', 50)->nullable();
            $table->string('storage_location', 100)->nullable();
            $table->unsignedSmallInteger('min_stock_level')->default(0);
            $table->unsignedSmallInteger('reorder_level')->default(0);
            $table->text('description')->nullable();
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->timestamps();

            $table->foreign('business_id')->references('id')->on('business')->onDelete('cascade');
            $table->foreign('category_id')->references('id')->on('vc_medicine_categories')->onDelete('set null');
            $table->index(['business_id', 'status']);
        });
    }

    public function down()
    {
        Schema::dropIfExists('vc_medicines');
    }
}
