<?php

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

/**
 * PRC-1143 — konsolide şema: `client_contract_acceptances` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('client_contract_acceptances', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('client_id');
            $t->unsignedBigInteger('contract_id');
            $t->string('ip_address', 45)->nullable();
            $t->text('user_agent')->nullable();
            $t->char('content_hash', 64)->nullable();
            $t->string('acceptance_context', 32)->nullable();
            $t->unsignedBigInteger('order_id')->nullable();
            $t->string('locale', 5)->nullable();
            $t->timestamp('accepted_at');
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index(['client_id', 'contract_id', 'accepted_at'], 'cca_client_contract_accepted_idx');
            $t->index('contract_id', 'client_contract_acceptances_contract_id_foreign');
            $t->index('order_id', 'client_contract_acceptances_order_id_foreign');
            $t->foreign('client_id', 'client_contract_acceptances_client_id_foreign')->references('id')->on('clients')->onDelete('cascade');
            $t->foreign('contract_id', 'client_contract_acceptances_contract_id_foreign')->references('id')->on('contracts')->onDelete('cascade');
            $t->foreign('order_id', 'client_contract_acceptances_order_id_foreign')->references('id')->on('orders')->onDelete('set null');
        });
    }

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