<?php

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

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

        Schema::create('clc_birth_death_records', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id');
            $table->enum('record_type', ['birth', 'death']);
            $table->unsignedInteger('patient_id');
            $table->unsignedInteger('contact_id');
            $table->unsignedInteger('doctor_id')->nullable();
            $table->date('event_date');
            $table->time('event_time')->nullable();
            $table->decimal('weight_kg', 8, 2)->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();

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

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