<?php

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

/**
 * PRC-1143 — konsolide şema: `client_ssl_certificates` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('client_ssl_certificates', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('client_product_id');
            $t->unsignedBigInteger('client_id');
            $t->string('common_name', 255);
            $t->json('alt_names')->nullable();
            $t->string('module', 64);
            $t->boolean('installed')->default(0);
            $t->timestamp('installed_at')->nullable();
            $t->timestamp('expires_at')->nullable();
            $t->timestamp('revoked_at')->nullable();
            $t->json('installation_input')->nullable();
            $t->string('issuer', 255)->nullable();
            $t->text('csr')->nullable();
            $t->text('certificate')->nullable();
            $t->text('certificate_chain')->nullable();
            $t->text('private_key')->nullable();
            $t->boolean('private_key_downloaded')->default(0);
            $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', 'cssl_client_idx');
            $t->index('client_product_id', 'cssl_cp_idx');
            $t->index('expires_at', 'cssl_expires_idx');
            $t->index(['installed', 'expires_at'], 'cssl_installed_expires_idx');
            $t->foreign('client_id', 'cssl_client_idx')->references('id')->on('clients')->onDelete('cascade');
            $t->foreign('client_product_id', 'cssl_cp_idx')->references('id')->on('client_products')->onDelete('cascade');
        });
    }

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