<?php

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

/**
 * Obstetrics & Gynaecology 1:1 patient extension on the shared ClinicCore
 * patient: obstetric history (GPAL), menstrual cycle, and the current
 * pregnancy's LMP — from which gestational age and EDD are computed (Naegele).
 */
class CreateObgynPatientProfilesTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('obgyn_patient_profiles')) return;
        if (! Schema::hasTable('clc_patients')) return; // requires ClinicCore

        Schema::create('obgyn_patient_profiles', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('clc_patient_id');

            // Obstetric score (GPAL)
            $table->unsignedSmallInteger('gravida')->nullable();    // pregnancies
            $table->unsignedSmallInteger('para')->nullable();       // births ≥ viability
            $table->unsignedSmallInteger('abortions')->nullable();  // losses
            $table->unsignedSmallInteger('living')->nullable();     // living children

            // Menstrual / cycle
            $table->unsignedSmallInteger('menarche_age')->nullable();
            $table->date('lmp_date')->nullable();                   // last menstrual period
            $table->unsignedSmallInteger('cycle_length_days')->nullable();
            $table->boolean('cycle_regular')->default(true);
            $table->string('contraception', 60)->nullable();

            // State
            $table->boolean('is_pregnant')->default(false);
            $table->boolean('menopause')->default(false);
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->unique('clc_patient_id');
            $table->foreign('clc_patient_id')->references('id')->on('clc_patients')->onDelete('cascade');
        });
    }

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