<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('wisatas', function (Blueprint $table): void {
            if (Schema::hasColumn('wisatas', 'nama_wisata')) {
                $table->renameColumn('nama_wisata', 'title');
            }

            if (Schema::hasColumn('wisatas', 'lokasi')) {
                $table->renameColumn('lokasi', 'location');
            }

            if (Schema::hasColumn('wisatas', 'jarak')) {
                $table->renameColumn('jarak', 'distance');
            }

            if (Schema::hasColumn('wisatas', 'elevasi')) {
                $table->renameColumn('elevasi', 'elevation');
            }

            if (Schema::hasColumn('wisatas', 'deskripsi_singkat')) {
                $table->renameColumn('deskripsi_singkat', 'short_description');
            }

            if (Schema::hasColumn('wisatas', 'gambar_wisata')) {
                $table->renameColumn('gambar_wisata', 'image');
            }

            if (Schema::hasColumn('wisatas', 'model_3d')) {
                $table->renameColumn('model_3d', 'model_path');
            }

            if (Schema::hasColumn('wisatas', 'video_wisata')) {
                $table->renameColumn('video_wisata', 'video_path');
            }
        });

        Schema::table('wisatas', function (Blueprint $table): void {
            if (! Schema::hasColumn('wisatas', 'is_video')) {
                $table->boolean('is_video')->default(false)->after('video_path');
            }
        });
    }

    public function down(): void
    {
        Schema::table('wisatas', function (Blueprint $table): void {
            if (Schema::hasColumn('wisatas', 'title')) {
                $table->renameColumn('title', 'nama_wisata');
            }

            if (Schema::hasColumn('wisatas', 'location')) {
                $table->renameColumn('location', 'lokasi');
            }

            if (Schema::hasColumn('wisatas', 'distance')) {
                $table->renameColumn('distance', 'jarak');
            }

            if (Schema::hasColumn('wisatas', 'elevation')) {
                $table->renameColumn('elevation', 'elevasi');
            }

            if (Schema::hasColumn('wisatas', 'short_description')) {
                $table->renameColumn('short_description', 'deskripsi_singkat');
            }

            if (Schema::hasColumn('wisatas', 'image')) {
                $table->renameColumn('image', 'gambar_wisata');
            }

            if (Schema::hasColumn('wisatas', 'model_path')) {
                $table->renameColumn('model_path', 'model_3d');
            }

            if (Schema::hasColumn('wisatas', 'video_path')) {
                $table->renameColumn('video_path', 'video_wisata');
            }

            if (Schema::hasColumn('wisatas', 'is_video')) {
                $table->dropColumn('is_video');
            }
        });
    }
};
