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

            if (! Schema::hasColumn('event_packages', 'extra_photos')) {
                $table->integer('extra_photos')->default(0)->after('used_photos');
            }

            if (! Schema::hasColumn('event_packages', 'extra_guests')) {
                $table->integer('extra_guests')->default(0)->after('used_guests');
            }

            if (! Schema::hasColumn('event_packages', 'extra_gallery_days')) {
                $table->integer('extra_gallery_days')->default(0)->after('gallery_expires_at');
            }
        });

        // Backfill snapshots from current package limits where missing.
        if (Schema::hasTable('event_packages') && Schema::hasTable('packages')) {
            DB::table('event_packages')
                ->whereNull('limits_snapshot')
                ->orderBy('id')
                ->chunkById(200, function ($rows) {
                    foreach ($rows as $row) {
                        $package = DB::table('packages')
                            ->where('id', $row->package_id)
                            ->first([
                                'max_photos',
                                'max_guests',
                                'gallery_days',
                                'max_tasks',
                                'max_events_per_year',
                            ]);

                        if (! $package) {
                            continue;
                        }

                        $snapshot = array_filter([
                            'max_photos' => $package->max_photos,
                            'max_guests' => $package->max_guests,
                            'gallery_days' => $package->gallery_days,
                            'max_tasks' => $package->max_tasks,
                            'max_events_per_year' => $package->max_events_per_year,
                        ], fn ($value) => $value !== null);

                        if ($snapshot === []) {
                            continue;
                        }

                        DB::table('event_packages')
                            ->where('id', $row->id)
                            ->update(['limits_snapshot' => json_encode($snapshot)]);
                    }
                });
        }
    }

    public function down(): void
    {
        Schema::table('event_packages', function (Blueprint $table) {
            if (Schema::hasColumn('event_packages', 'limits_snapshot')) {
                $table->dropColumn('limits_snapshot');
            }
            if (Schema::hasColumn('event_packages', 'extra_photos')) {
                $table->dropColumn('extra_photos');
            }
            if (Schema::hasColumn('event_packages', 'extra_guests')) {
                $table->dropColumn('extra_guests');
            }
            if (Schema::hasColumn('event_packages', 'extra_gallery_days')) {
                $table->dropColumn('extra_gallery_days');
            }
        });
    }
};
