<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('pengajuan_surats', function (Blueprint $table) {
            $table->id();
            $table->string('nama_lengkap');
            $table->text('nik'); // Encrypted text
            $table->string('email')->nullable();
            $table->string('telepon', 20);
            $table->string('jenis_surat');
            $table->text('keterangan');
            $table->string('lampiran')->nullable();
            $table->string('nomor_tiket', 20)->unique();
            $table->enum('status', ['Pending', 'Diproses', 'Selesai'])->default('Pending');
            $table->softDeletes();
            $table->timestamps();

            // Indexing for performance
            $table->index('nomor_tiket');
            $table->index('status');
            $table->index('created_at');
        });

        // Migrate existing pengajuan_surat data from pengaduans to pengajuan_surats
        if (Schema::hasTable('pengaduans')) {
            $existingRequests = DB::table('pengaduans')
                ->where('jenis', 'pengajuan_surat')
                ->get();

            foreach ($existingRequests as $req) {
                DB::table('pengajuan_surats')->insert([
                    'id' => $req->id,
                    'nama_lengkap' => $req->nama_lengkap,
                    'nik' => $req->nik,
                    'email' => $req->email,
                    'telepon' => $req->telepon,
                    'jenis_surat' => $req->kategori, // kategori maps to jenis_surat
                    'keterangan' => $req->isi_pengaduan, // isi_pengaduan maps to keterangan
                    'lampiran' => $req->lampiran,
                    'nomor_tiket' => $req->nomor_tiket,
                    'status' => $req->status,
                    'deleted_at' => $req->deleted_at ?? null,
                    'created_at' => $req->created_at,
                    'updated_at' => $req->updated_at,
                ]);
            }

            // Remove migrated records from pengaduans table
            DB::table('pengaduans')
                ->where('jenis', 'pengajuan_surat')
                ->delete();
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Restore data from pengajuan_surats to pengaduans if table down is called
        if (Schema::hasTable('pengaduans')) {
            $existingRequests = DB::table('pengajuan_surats')->get();

            foreach ($existingRequests as $req) {
                DB::table('pengaduans')->insert([
                    'id' => $req->id,
                    'jenis' => 'pengajuan_surat',
                    'nama_lengkap' => $req->nama_lengkap,
                    'nik' => $req->nik,
                    'email' => $req->email,
                    'telepon' => $req->telepon,
                    'kategori' => $req->jenis_surat,
                    'isi_pengaduan' => $req->keterangan,
                    'lampiran' => $req->lampiran,
                    'nomor_tiket' => $req->nomor_tiket,
                    'status' => $req->status,
                    'deleted_at' => $req->deleted_at ?? null,
                    'created_at' => $req->created_at,
                    'updated_at' => $req->updated_at,
                ]);
            }
        }

        Schema::dropIfExists('pengajuan_surats');
    }
};
