<?php

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

class CreatePrPurchaseSharesTable extends Migration
{
    public function up()
    {
        if (!Schema::hasTable('pr_purchase_shares')) {
            Schema::create('pr_purchase_shares', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('business_id')->unsigned();
                $table->integer('transaction_id')->unsigned()->unique();
                $table->json('pr_request_ids');               // array of PR request IDs
                $table->string('share_token', 100)->unique(); // public share token
                $table->timestamps();

                $table->foreign('transaction_id')
                      ->references('id')->on('transactions')
                      ->onDelete('cascade');
            });
        }
    }

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