<?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('artworks', function (Blueprint $table): void {
            if (! Schema::hasColumn('artworks', 'clip_tags_json')) {
                $table->json('clip_tags_json')->nullable()->after('thumb_ext');
            }

            if (! Schema::hasColumn('artworks', 'blip_caption')) {
                $table->text('blip_caption')->nullable()->after('clip_tags_json');
            }

            if (! Schema::hasColumn('artworks', 'yolo_objects_json')) {
                $table->json('yolo_objects_json')->nullable()->after('blip_caption');
            }

            if (! Schema::hasColumn('artworks', 'vision_metadata_updated_at')) {
                $table->timestamp('vision_metadata_updated_at')->nullable()->after('yolo_objects_json');
            }
        });
    }

    public function down(): void
    {
        Schema::table('artworks', function (Blueprint $table): void {
            $columns = [];

            foreach (['clip_tags_json', 'blip_caption', 'yolo_objects_json', 'vision_metadata_updated_at'] as $column) {
                if (Schema::hasColumn('artworks', $column)) {
                    $columns[] = $column;
                }
            }

            if ($columns !== []) {
                $table->dropColumn($columns);
            }
        });
    }
};
