<?php

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

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

        Schema::create('clc_prescription_items', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('prescription_id');
            $table->unsignedInteger('medicine_id')->nullable();
            $table->string('medicine_name', 150);
            $table->string('dosage', 100)->nullable();
            $table->string('frequency', 100)->nullable();
            $table->unsignedSmallInteger('duration_days')->nullable();
            $table->text('instructions')->nullable();
            $table->unsignedSmallInteger('quantity')->default(1);
            $table->timestamps();

            $table->foreign('prescription_id')->references('id')->on('clc_prescriptions')->onDelete('cascade');
            $table->index('prescription_id');
        });
    }

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