<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasColumn('attachments', 'crop_horizontal')) {
            Schema::table('attachments', function (Blueprint $table): void {
                $table->json('crop_horizontal')->nullable()->after('size');
            });
        }

        if (! Schema::hasColumn('attachments', 'crop_vertical')) {
            Schema::table('attachments', function (Blueprint $table): void {
                $table->json('crop_vertical')->nullable()->after('crop_horizontal');
            });
        }

        $hasLegacyHorizontal = Schema::hasColumn('attachments', 'crop_horizontal_start_percent');
        $hasLegacyVertical = Schema::hasColumn('attachments', 'crop_vertical_start_percent');

        if ($hasLegacyHorizontal || $hasLegacyVertical) {
            $columns = array_values(array_filter([
                'id',
                $hasLegacyHorizontal ? 'crop_horizontal_start_percent' : null,
                $hasLegacyVertical ? 'crop_vertical_start_percent' : null,
            ]));

            DB::table('attachments')
                ->select($columns)
                ->orderBy('id')
                ->chunkById(100, function ($attachments) use ($hasLegacyHorizontal, $hasLegacyVertical): void {
                    foreach ($attachments as $attachment) {
                        $updates = [];

                        if ($hasLegacyHorizontal && $attachment->crop_horizontal_start_percent !== null) {
                            $updates['crop_horizontal'] = json_encode([
                                'start_percentage' => (float) $attachment->crop_horizontal_start_percent,
                                'end_percentage' => 100,
                            ]);
                        }

                        if ($hasLegacyVertical && $attachment->crop_vertical_start_percent !== null) {
                            $updates['crop_vertical'] = json_encode([
                                'start_percentage' => (float) $attachment->crop_vertical_start_percent,
                                'end_percentage' => 100,
                            ]);
                        }

                        if ($updates !== []) {
                            DB::table('attachments')->where('id', $attachment->id)->update($updates);
                        }
                    }
                });

            Schema::table('attachments', function (Blueprint $table) use ($hasLegacyHorizontal, $hasLegacyVertical): void {
                if ($hasLegacyHorizontal) {
                    $table->dropColumn('crop_horizontal_start_percent');
                }

                if ($hasLegacyVertical) {
                    $table->dropColumn('crop_vertical_start_percent');
                }
            });
        }
    }

    public function down(): void
    {
        if (! Schema::hasColumn('attachments', 'crop_horizontal_start_percent')) {
            Schema::table('attachments', function (Blueprint $table): void {
                $table->decimal('crop_horizontal_start_percent', 7, 4)->nullable()->after('size');
            });
        }

        if (! Schema::hasColumn('attachments', 'crop_vertical_start_percent')) {
            Schema::table('attachments', function (Blueprint $table): void {
                $table->decimal('crop_vertical_start_percent', 7, 4)
                    ->nullable()
                    ->after('crop_horizontal_start_percent');
            });
        }

        DB::table('attachments')
            ->select(['id', 'crop_horizontal', 'crop_vertical'])
            ->orderBy('id')
            ->chunkById(100, function ($attachments): void {
                foreach ($attachments as $attachment) {
                    $horizontal = json_decode($attachment->crop_horizontal ?? '', true);
                    $vertical = json_decode($attachment->crop_vertical ?? '', true);

                    DB::table('attachments')->where('id', $attachment->id)->update([
                        'crop_horizontal_start_percent' => $horizontal['start_percentage'] ?? null,
                        'crop_vertical_start_percent' => $vertical['start_percentage'] ?? null,
                    ]);
                }
            });

        Schema::table('attachments', function (Blueprint $table): void {
            $table->dropColumn(['crop_horizontal', 'crop_vertical']);
        });
    }
};
