<?php

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

/**
 * Galaxy Tab S8 Ultra real-frame seed + 7" Android tablet form-factor
 * retirement.
 *
 * AppScreens / Play Store both auto-scale a single tablet submission to
 * every device size, so we collapse the dual-tier (7" + 10") Android
 * tablet split back to a single tier — `android_10_tablet_portrait`
 * (canonical Samsung Galaxy Tab S8 Ultra, 14.6", 1848×2960). The
 * `android_7_tablet_*` form factor is dropped from the FormFactor union
 * in the same commit; here we deactivate every device_preset row pointing
 * at it so the gallery + frame picker drop the deactivated devices, while
 * historical projects that already reference them via `devicePresetId`
 * keep resolving (row stays present, just `is_active = false`).
 *
 * Real-frame flip: Galaxy Tab S8 Ultra rows already exist in both
 * orientations. We set `has_real_frame = true` + populate the 5 color
 * options + screen-rect geometry measured from the source PNGs at
 * 1950×3030 native (cutout 1819×2913, ~15px bezel asymmetry — same
 * scaling tolerance Pixel 10 Pro uses).
 */
return new class extends Migration
{
    public function up(): void
    {
        DB::transaction(function () {
            // 1. Retire the 7" Android tablet submission tier — deactivate
            //    every preset rather than delete so historical projects /
            //    kits with `devicePresetId` referencing these rows still
            //    resolve to a known device on read.
            DB::table('device_presets')
                ->whereIn('form_factor', [
                    'android_7_tablet_portrait',
                    'android_7_tablet_landscape',
                ])
                ->update([
                    'is_active' => false,
                    'updated_at' => now(),
                ]);

            // 2. Galaxy Tab S8 Ultra real-frame metadata. Color options +
            //    screen-rect geometry mirror `public/device-frames/manifest.json`.
            $colors = [
                ['slug' => 'black',      'label' => 'Black',      'swatch' => '#0E0E10'],
                ['slug' => 'real-light', 'label' => 'Real Light', 'swatch' => '#E7E2D9'],
                ['slug' => 'real-gold',  'label' => 'Real Gold',  'swatch' => '#C8B68C'],
                ['slug' => 'light',      'label' => 'Light',      'swatch' => '#DCDCDC'],
                ['slug' => 'dark',       'label' => 'Dark',       'swatch' => '#393B3F'],
            ];

            $screenRectPortrait = [
                'portrait' => [
                    'x' => 63,
                    'y' => 59,
                    'width' => 1819,
                    'height' => 2913,
                    'radius' => 64,
                    'frame_w' => 1950,
                    'frame_h' => 3030,
                ],
                'landscape' => [
                    'x' => 58,
                    'y' => 63,
                    'width' => 2913,
                    'height' => 1819,
                    'radius' => 64,
                    'frame_w' => 3030,
                    'frame_h' => 1950,
                ],
            ];

            $screenRectLandscape = $screenRectPortrait;

            DB::table('device_presets')
                ->where('key', 'android-galaxy-tab-s8-ultra')
                ->update([
                    'has_real_frame' => true,
                    'real_frame_color_options' => json_encode($colors),
                    'real_frame_screen_rect' => json_encode($screenRectPortrait),
                    'updated_at' => now(),
                ]);

            DB::table('device_presets')
                ->where('key', 'android-galaxy-tab-s8-ultra-landscape')
                ->update([
                    'has_real_frame' => true,
                    'real_frame_color_options' => json_encode($colors),
                    'real_frame_screen_rect' => json_encode($screenRectLandscape),
                    'updated_at' => now(),
                ]);
        });
    }

    public function down(): void
    {
        DB::transaction(function () {
            DB::table('device_presets')
                ->whereIn('form_factor', [
                    'android_7_tablet_portrait',
                    'android_7_tablet_landscape',
                ])
                ->update([
                    'is_active' => true,
                    'updated_at' => now(),
                ]);

            DB::table('device_presets')
                ->whereIn('key', ['android-galaxy-tab-s8-ultra', 'android-galaxy-tab-s8-ultra-landscape'])
                ->update([
                    'has_real_frame' => false,
                    'real_frame_color_options' => null,
                    'real_frame_screen_rect' => null,
                    'updated_at' => now(),
                ]);
        });
    }
};
