<?php

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

class CreateRecurringAppointmentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('recurring_appointments', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->string('original_appointment_id');
            $table->string('patients_id');
            $table->unsignedBigInteger('users_id');
            $table->string('especiality', 5);
            $table->integer('days_interval')->comment('Número de días para la próxima cita');
            $table->timestamp('next_appointment_date');
            $table->string('description')->nullable();
            $table->boolean('is_active')->default(true);
            $table->boolean('auto_schedule')->default(true)->comment('Si es true, se creará automáticamente la cita');
            $table->integer('reminder_days')->default(5)->comment('Días antes para notificar del retorno');
            $table->boolean('notification_sent')->default(false);
            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->nullable();
            
            $table->foreign('original_appointment_id')->references('id')->on('appointments')->onDelete('cascade');
            $table->foreign('patients_id')->references('id')->on('patients')->onDelete('cascade');
            $table->foreign('users_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('especiality')->references('id')->on('especiality');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('recurring_appointments');
    }
}
