<?php

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

/**
 * Per-pet verification token printed on the vaccination certificate as a QR.
 * Scanning the QR opens a public page that confirms (without exposing the
 * full medical history) that the certificate is genuine and shows the last
 * few vaccinations.
 */
class AddCertVerifyTokenToClcPatients extends Migration
{
    public function up()
    {
        if (Schema::hasTable('clc_patients') && !Schema::hasColumn('clc_patients', 'cert_verify_token')) {
            Schema::table('clc_patients', function (Blueprint $table) {
                // clc_patients stores the photo column as "photo_path"; the after() hint
                // was wrong in the original migration and the table create errored.
                $table->string('cert_verify_token', 64)->nullable()->unique()->after('photo_path');
            });
        }
    }

    public function down()
    {
        if (Schema::hasTable('clc_patients') && Schema::hasColumn('clc_patients', 'cert_verify_token')) {
            Schema::table('clc_patients', function (Blueprint $table) {
                $table->dropUnique(['cert_verify_token']);
                $table->dropColumn('cert_verify_token');
            });
        }
    }
}
