<?php

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

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

        Schema::create('vc_preferred_doctor_history', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id')->index();
            $table->unsignedInteger('contact_id')->index();
            // null pet_id = owner-level general preferred doctor; otherwise per-pet override
            $table->unsignedInteger('pet_id')->nullable()->index();
            $table->unsignedInteger('old_doctor_id')->nullable();
            $table->unsignedInteger('new_doctor_id')->nullable();
            $table->unsignedInteger('changed_by')->nullable();
            // manual = edited on the owner/pet form; doctor_change_request = via an approved request
            $table->enum('source', ['manual', 'doctor_change_request'])->default('manual');
            $table->unsignedInteger('appointment_id')->nullable();
            $table->string('reason', 500)->nullable();
            $table->timestamps();
        });
    }

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