<?php

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

/**
 * PRC-1143 — konsolide şema: `cancellation_requests` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('cancellation_requests', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('client_id');
            $t->unsignedBigInteger('client_product_id')->nullable();
            $t->unsignedBigInteger('client_domain_id')->nullable();
            $t->enum('type', ['product','domain']);
            $t->enum('reason', ['not_satisfied','too_expensive','found_better_alternative','no_longer_needed','technical_issues','poor_support','moved_to_competitor','business_closed','other'])->nullable();
            $t->enum('refund_type', ['immediate_refund','end_of_period']);
            $t->text('description')->nullable();
            $t->enum('status', ['pending','approved','rejected','cancelled'])->default('pending');
            $t->text('admin_notes')->nullable();
            $t->unsignedBigInteger('processed_by')->nullable();
            $t->timestamp('processed_at')->nullable();
            $t->timestamp('effective_date')->nullable();
            $t->decimal('refund_amount', 10, 2)->nullable();
            $t->string('refund_method', 255)->nullable();
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index('client_domain_id', 'cancellation_requests_client_domain_id_foreign');
            $t->index(['client_id', 'status'], 'cancellation_requests_client_id_status_index');
            $t->index('client_product_id', 'cancellation_requests_client_product_id_foreign');
            $t->index('created_at', 'cancellation_requests_created_at_index');
            $t->index('processed_by', 'cancellation_requests_processed_by_foreign');
            $t->index(['type', 'status'], 'cancellation_requests_type_status_index');
            $t->foreign('client_domain_id', 'cancellation_requests_client_domain_id_foreign')->references('id')->on('client_domains')->onDelete('cascade');
            $t->foreign('client_id', 'cancellation_requests_client_id_foreign')->references('id')->on('clients')->onDelete('cascade');
            $t->foreign('client_product_id', 'cancellation_requests_client_product_id_foreign')->references('id')->on('client_products')->onDelete('cascade');
            $t->foreign('processed_by', 'cancellation_requests_processed_by_foreign')->references('id')->on('admins')->onDelete('set null');
        });
    }

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