<?php

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

class CreateSiteSyncOutboxesTable extends Migration
{
    public function up()
    {
        Schema::create('site_sync_outboxes', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('company_id');
            $table->unsignedBigInteger('building_id');
            $table->string('operation', 64);
            $table->string('delivery_token', 64)->nullable();
            $table->longText('payload');
            $table->string('status');
            $table->unsignedInteger('retry_count')->default(0);
            $table->text('last_error')->nullable();
            $table->timestamp('next_retry_at')->nullable();
            $table->timestamp('processing_started_at')->nullable();
            $table->timestamp('synced_at')->nullable();
            $table->timestamps();

            $table->unique(
                ['company_id', 'building_id', 'operation'],
                'site_sync_outboxes_company_building_operation_unique'
            );
        });
    }

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