<?php

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

/**
 * Phase 2 of the controlled-drug register: vets authorised to dispense
 * controlled substances under a regulator-issued permit.
 *
 *   clc_authorized_vets.schedules : JSON list of controlled schedules the vet
 *       may dispense (subset of ClcMedicine::schedules() keys).
 *   .valid_until : permit expiry; expired permits block dispensing and raise
 *       a warning on the register page (and within 30 days = orange flag).
 */
class CreateClcAuthorizedVetsTable extends Migration
{
    public function up()
    {
        if (!Schema::hasTable('clc_authorized_vets')) {
            Schema::create('clc_authorized_vets', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('business_id')->index();
                $table->unsignedInteger('doctor_id')->index();
                $table->json('schedules');                          // ["narcotic","schedule_1",...]
                $table->string('permit_no', 80)->nullable();
                $table->date('valid_until')->nullable()->index();
                $table->string('document_path', 191)->nullable();   // optional scanned permit
                $table->text('notes')->nullable();
                $table->unsignedInteger('created_by')->nullable();
                $table->timestamps();
                $table->unique(['business_id', 'doctor_id']);
            });
        }
    }

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