<?php

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

/**
 * Trim the device preset catalog to the iPhone / iPad tiers that match the
 * industry standard (appscreens.com's selection, which mirrors App Store
 * Connect's current submission slots):
 *
 *   iPhone tiers: 5.5" · 6.1" · 6.3" · 6.5" · 6.9"
 *   iPad tiers:   12.9" · 13"
 *
 * Changes:
 *   1. Re-tier iPhone 16 from iphone-6.3 → iphone-6.1. Its 1179×2556 dims
 *      are Apple's 6.1" tier; only Pro / Pro Max bumped to 6.3" / 6.9" in
 *      the iPhone 16 cycle.
 *   2. Add iPhone 11 Pro Max (6.5", 1242×2688) — canonical 6.5" device.
 *   3. Delete iPhone SE 3 (4.7"). Apple auto-scales 4.7" from 5.5" anyway.
 *   4. Delete iPad Pro 11" — not part of the appscreens-aligned set.
 *
 * Pre-launch deletions are hard deletes — no production projects reference
 * these IDs yet (CLAUDE.md "still in development; no migration-of-old-
 * projects concerns" for the dev window).
 */
return new class extends Migration
{
    public function up(): void
    {
        // (1) iPhone 16 → 6.1" tier.
        DB::table('device_presets')
            ->where('key', 'iphone-16')
            ->update([
                'apple_tier' => 'iphone-6.1',
                'label' => 'iPhone 16 (6.1")',
                'updated_at' => now(),
            ]);

        // (2) Add iPhone 11 Pro Max — canonical 6.5" tier device. No real
        // frame bundle yet (renderer falls back to the procedural bezel).
        DB::table('device_presets')->insert([
            'key' => 'iphone-11-pro-max',
            'label' => 'iPhone 11 Pro Max (6.5")',
            'platform' => 'apple',
            'category' => 'phone',
            'form_factor' => 'iphone_portrait',
            'apple_tier' => 'iphone-6.5',
            'export_w' => 1242,
            'export_h' => 2688,
            'safe_area' => json_encode(['top' => 44, 'right' => 0, 'bottom' => 34, 'left' => 0]),
            'is_active' => true,
            'has_real_frame' => false,
            'real_frame_color_options' => null,
            'real_frame_screen_rect' => null,
            'frame_svg_path' => null,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        // (3) Delete iPhone SE 3 (4.7" — dropped per appscreens parity).
        DB::table('device_presets')->where('key', 'iphone-se-3')->delete();

        // (4) Delete iPad Pro 11" (also dropped per appscreens parity).
        DB::table('device_presets')->where('key', 'ipad-pro-11')->delete();
    }

    public function down(): void
    {
        // Best-effort restore of the pre-trim state. Pre-launch, no production
        // data depends on these IDs — but keep down() correct so dev `migrate:
        // fresh` and CI tear-downs round-trip.
        DB::table('device_presets')
            ->where('key', 'iphone-16')
            ->update([
                'apple_tier' => 'iphone-6.3',
                'label' => 'iPhone 16 (6.3")',
                'updated_at' => now(),
            ]);

        DB::table('device_presets')->where('key', 'iphone-11-pro-max')->delete();

        DB::table('device_presets')->insert([
            'key' => 'iphone-se-3',
            'label' => 'iPhone SE (3rd gen, 4.7")',
            'platform' => 'apple',
            'category' => 'phone',
            'form_factor' => 'iphone_portrait',
            'apple_tier' => 'iphone-4.7',
            'export_w' => 750,
            'export_h' => 1334,
            'safe_area' => json_encode(['top' => 20, 'right' => 0, 'bottom' => 0, 'left' => 0]),
            'is_active' => true,
            'has_real_frame' => false,
            'real_frame_color_options' => null,
            'real_frame_screen_rect' => null,
            'frame_svg_path' => null,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('device_presets')->insert([
            'key' => 'ipad-pro-11',
            'label' => 'iPad Pro 11" (M5)',
            'platform' => 'apple',
            'category' => 'tablet',
            'form_factor' => 'ipad_portrait',
            'apple_tier' => 'ipad-11',
            'export_w' => 1668,
            'export_h' => 2388,
            'safe_area' => json_encode(['top' => 24, 'right' => 0, 'bottom' => 0, 'left' => 0]),
            'is_active' => true,
            'has_real_frame' => false,
            'real_frame_color_options' => null,
            'real_frame_screen_rect' => null,
            'frame_svg_path' => null,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
};
