<?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
{
    /**
     * Group presets by Apple's App Store Connect display-size tier. The tier
     * is what Apple's submission UI uses to slot screenshots — multiple device
     * presets can share a tier when they have identical pixel dimensions.
     * Exposing the tier lets the ZIP organizer dedupe by tier instead of by
     * per-device folder, matching what the reviewer actually sees.
     */
    public function up(): void
    {
        Schema::table('device_presets', function (Blueprint $table) {
            $table->string('apple_tier', 32)->nullable()->after('category')->index();
        });

        foreach ($this->tierAssignments() as $key => $tier) {
            DB::table('device_presets')
                ->where('key', $key)
                ->update(['apple_tier' => $tier, 'updated_at' => now()]);
        }
    }

    public function down(): void
    {
        Schema::table('device_presets', function (Blueprint $table) {
            $table->dropIndex(['apple_tier']);
            $table->dropColumn('apple_tier');
        });
    }

    /** @return array<string,string> */
    private function tierAssignments(): array
    {
        return [
            'iphone-16-pro-max' => 'iphone-6.9',
            'iphone-16-plus' => 'iphone-6.9',
            'iphone-15-pro-max' => 'iphone-6.9',
            'iphone-16-pro' => 'iphone-6.3',
            'iphone-16' => 'iphone-6.3',
            'iphone-15-pro' => 'iphone-6.3',
            'iphone-se-3' => 'iphone-4.7',
            'ipad-pro-13' => 'ipad-13',
            'ipad-air-13' => 'ipad-13',
            'ipad-pro-11' => 'ipad-11',
        ];
    }
};
