<?php

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

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

        Schema::create('vc_medicine_batches', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('medicine_id');
            $table->unsignedInteger('business_id');
            $table->string('batch_no', 50)->nullable();
            $table->date('manufactured_date')->nullable();
            $table->date('expiry_date')->nullable();
            $table->unsignedInteger('quantity_received')->default(0);
            $table->integer('quantity_remaining')->default(0);
            $table->decimal('purchase_price', 15, 4)->default(0);
            $table->decimal('selling_price', 15, 4)->default(0);
            $table->string('supplier_name', 100)->nullable();
            $table->timestamps();

            $table->foreign('medicine_id')->references('id')->on('vc_medicines')->onDelete('cascade');
            $table->foreign('business_id')->references('id')->on('business')->onDelete('cascade');
            $table->index(['medicine_id', 'business_id']);
        });
    }

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