<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('order_items', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('order_id'); // Sipariş ile ilişkilendirilecek
            $table->enum('type', ['product', 'domain', 'credit']); // Kalem türü (ürün veya domain)
            $table->string('name'); // Item name
            $table->unsignedBigInteger('product_id')->nullable();
            $table->string('domain_name')->nullable();
            $table->enum('domain_action', ['register', 'transfer', 'use', 'renew'])->nullable();
            $table->unsignedInteger('relation_id')->nullable(); // Ürün ID'si veya TLD ID'si
            $table->string('description'); // Kalem açıklaması
            $table->decimal('amount', 10, 2); // Birim fiyat
            $table->decimal('setup_amount', 10, 2)->default(0); // Kurulum ücreti
            $table->integer('quantity')->default(1); // Miktar
            $table->integer('period')->default(1); // Süre (ay/yıl)
            $table->string('period_type')->default('year'); // Süre tipi (month/year)
            $table->decimal('unit_price', 10, 2)->default(0);
            $table->decimal('total_price', 10, 2)->default(0);
            $table->json('config_options')->nullable();
            $table->json('custom_fields')->nullable();
            $table->json('nameservers')->nullable();
            $table->string('epp_code')->nullable();
            $table->decimal('total_amount', 10, 2); // Toplam tutar
            $table->unsignedInteger('currency_id')->nullable(); // Para birimi
            $table->json('options')->nullable(); // Ek seçenekler (domain transfer, DNS management vb.)
            $table->timestamps();

            $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
            $table->index(['order_id', 'type']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('order_items');
    }
};
