<?php

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

/**
 * PRC-619 — Birleşik iletişim şablonları.
 *
 * email_templates / sms_templates / notification_templates (+3 içerik
 * tablosu) tek çifte indirildi. `channel` discriminator: email | sms |
 * slack | telegram | discord | whatsapp | pushover. E-postaya özgü
 * kolonlar (subject içerikte, from_* / copy / bcc / is_html burada) diğer
 * kanallarda NULL kalır — zorunluluk app-katmanı validation'dadır.
 * Eski tablolar dev aşamasında veri taşınmadan kaldırıldı
 * (migrate:fresh --seed esastır).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('communication_templates', function (Blueprint $table) {
            $table->id();
            $table->string('channel', 20)->index();
            $table->string('type', 50)->default('general')->index();
            $table->string('name');
            $table->string('label')->nullable();
            $table->enum('status', ['active', 'inactive'])->default('active');
            $table->boolean('is_system')->default(true);
            $table->enum('message_class', ['transactional', 'marketing'])->default('transactional');
            $table->string('from_name')->nullable();
            $table->string('from_address')->nullable();
            $table->string('copy_address')->nullable();
            $table->string('bcc_address')->nullable();
            $table->boolean('is_html')->nullable();
            $table->timestamps();

            $table->unique(['channel', 'name']);
        });

        Schema::create('communication_template_contents', function (Blueprint $table) {
            $table->id();
            $table->foreignId('template_id')->constrained('communication_templates')->cascadeOnDelete();
            $table->string('language', 5)->default('tr');
            $table->string('subject')->nullable();
            $table->text('content');
            $table->timestamps();

            $table->unique(['template_id', 'language']);
        });
    }

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