<?php

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

/**
 * Restore the `ipad-pro-11` portrait preset (deleted by the 2026-05-22
 * appscreens-parity migration) and align both orientations to the M5
 * screen tier (1668 × 2420 portrait / 2420 × 1668 landscape — Apple
 * `apple-screenshot-specs.md`).
 *
 * The 2026-05-22 alignment migration trimmed the device list to match
 * appscreens.com's tier set, hard-deleting `ipad-pro-11` portrait while
 * leaving the landscape row at the older M4 dims (2388 × 1668). The
 * landscape row's `real_frame_screen_rect` already carries both
 * orientations' coords from the photoreal-frame manifest, so we just
 * copy that block + the color options onto the restored portrait row.
 *
 * `family_id` stays NULL — iPad Pro 11" is the "pro" tier per the
 * manifest, separate from the `pro_max` family already wired across
 * iPhone 17 Pro Max / iPad Pro 13" / Pixel 10 Pro / Galaxy S26 /
 * Galaxy Tab S8 Ultra. No cross-device family exists for `pro` yet.
 */
return new class extends Migration
{
    public function up(): void
    {
        $now = now();

        // (1) Pull the photoreal real_frame fields off the landscape row so
        //     the restored portrait row inherits identical coords + colors
        //     (the manifest seeds both orientations into each row already).
        $landscape = DB::table('device_presets')
            ->where('key', 'ipad-pro-11-landscape')
            ->first();
        $realFrameRect = $landscape?->real_frame_screen_rect;
        $realFrameColors = $landscape?->real_frame_color_options;
        $hasRealFrame = (bool) ($landscape?->has_real_frame ?? false);

        // (2) Re-insert the portrait row at M5 dims (1668 × 2420).
        DB::table('device_presets')->updateOrInsert(
            ['key' => 'ipad-pro-11'],
            [
                'label' => 'iPad Pro 11" (M5)',
                'platform' => 'ios',
                'category' => 'tablet',
                'form_factor' => 'ipad_portrait',
                'apple_tier' => 'ipad-11',
                'export_w' => 1668,
                'export_h' => 2420,
                'safe_area' => json_encode(['top' => 24, 'right' => 0, 'bottom' => 0, 'left' => 0]),
                'is_active' => true,
                'has_real_frame' => $hasRealFrame,
                'real_frame_color_options' => $realFrameColors,
                'real_frame_screen_rect' => $realFrameRect,
                'frame_svg_path' => null,
                'family_id' => null,
                'created_at' => $now,
                'updated_at' => $now,
            ],
        );

        // (3) Bump the landscape row's export dims from the M4 spec
        //     (2388 × 1668) to the M5 spec (2420 × 1668). The
        //     real_frame_screen_rect coords already reflect M5 — only the
        //     export_w / export_h fields lagged behind.
        DB::table('device_presets')
            ->where('key', 'ipad-pro-11-landscape')
            ->update([
                'export_w' => 2420,
                'export_h' => 1668,
                'updated_at' => $now,
            ]);
    }

    public function down(): void
    {
        $now = now();

        // Reverse: restore landscape's older M4 dims and delete the
        // portrait row again (matches the post-2026-05-22 state).
        DB::table('device_presets')
            ->where('key', 'ipad-pro-11-landscape')
            ->update([
                'export_w' => 2388,
                'export_h' => 1668,
                'updated_at' => $now,
            ]);
        DB::table('device_presets')
            ->where('key', 'ipad-pro-11')
            ->delete();
    }
};
