<?php

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

/**
 * Phase 3b — tamper-evident daily seal for the controlled-drug register.
 *
 * Once a business-day is sealed, that day's movement rows become locked-in:
 * the seal stores SHA-256 of (previous_seal_hash + serialised row payload).
 * Any later edit of a sealed row makes the chain re-verify mismatch, which
 * the register page surfaces as a red "INTEGRITY BREACH" banner.
 */
class CreateVcControlledDailySealsTable extends Migration
{
    public function up()
    {
        if (!Schema::hasTable('vc_controlled_daily_seals')) {
            Schema::create('vc_controlled_daily_seals', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('business_id')->index();
                $table->date('seal_date');
                $table->unsignedInteger('movement_count')->default(0);
                $table->unsignedInteger('first_movement_id')->nullable();
                $table->unsignedInteger('last_movement_id')->nullable();
                $table->string('prev_hash', 64)->nullable();
                $table->string('hash', 64);
                $table->timestamp('sealed_at')->nullable();
                $table->unsignedInteger('sealed_by')->nullable();
                $table->timestamps();
                $table->unique(['business_id', 'seal_date']);
            });
        }
    }

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