<?php

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

/**
 * PRC-1143 — konsolide şema: `client_licenses` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('client_licenses', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('client_product_id');
            $t->unsignedBigInteger('client_id');
            $t->string('product_label', 255)->nullable();
            $t->string('module', 64);
            $t->string('vendor_ref', 255)->nullable();
            $t->boolean('issued')->default(0);
            $t->timestamp('issued_at')->nullable();
            $t->timestamp('expires_at')->nullable();
            $t->timestamp('revoked_at')->nullable();
            $t->string('license_key', 255)->nullable();
            $t->json('module_data')->nullable();
            $t->text('last_error')->nullable();
            $t->timestamp('last_attempt_at')->nullable();
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index('client_id', 'cl_client_idx');
            $t->index('client_product_id', 'cl_cp_idx');
            $t->index('expires_at', 'cl_expires_idx');
            $t->index(['issued', 'expires_at'], 'cl_issued_expires_idx');
            $t->foreign('client_id', 'cl_client_idx')->references('id')->on('clients')->onDelete('cascade');
            $t->foreign('client_product_id', 'cl_cp_idx')->references('id')->on('client_products')->onDelete('cascade');
        });
    }

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