<?php

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

/**
 * Galaxy Tab S8 Ultra: drop the screen-cutout radius to 0. The PNG's bezel
 * curve isn't a perfect quarter-circle (measured ~28-32 px depth at the
 * corner, but the visible curve uses a squircle / super-ellipse shape).
 * Any non-zero radius produces a visible gap between the screenshot's
 * rounded clip and the bezel's actual cutout edge (radius 64 was too
 * round — bezel-through; radius 30 was too square — white-gap).
 *
 * Radius 0 = rectangular screenshot + rectangular screen fill. The bezel
 * PNG sits on top and its opaque pixels cover any overdraw at the corner.
 * This matches the "let bezel decide" strategy.
 */
return new class extends Migration
{
    public function up(): void
    {
        foreach (['android-galaxy-tab-s8-ultra', 'android-galaxy-tab-s8-ultra-landscape'] as $key) {
            $row = DB::table('device_presets')->where('key', $key)->first();
            if (! $row || ! is_string($row->real_frame_screen_rect)) {
                continue;
            }
            $rect = json_decode($row->real_frame_screen_rect, true);
            if (! is_array($rect)) {
                continue;
            }
            foreach (['portrait', 'landscape'] as $orient) {
                if (isset($rect[$orient]) && is_array($rect[$orient])) {
                    $rect[$orient]['radius'] = 0;
                }
            }
            DB::table('device_presets')
                ->where('key', $key)
                ->update([
                    'real_frame_screen_rect' => json_encode($rect),
                    'updated_at' => now(),
                ]);
        }
    }

    public function down(): void
    {
        foreach (['android-galaxy-tab-s8-ultra', 'android-galaxy-tab-s8-ultra-landscape'] as $key) {
            $row = DB::table('device_presets')->where('key', $key)->first();
            if (! $row || ! is_string($row->real_frame_screen_rect)) {
                continue;
            }
            $rect = json_decode($row->real_frame_screen_rect, true);
            if (! is_array($rect)) {
                continue;
            }
            // Squashed (2026-05-26): the intermediate `radius = 30`
            // migration was deleted as dead-on-arrival. Roll back to the
            // original manifest radius (64) instead, matching what
            // `backfill_real_frame_from_manifest` seeded before any of
            // the corner-radius fixes landed.
            foreach (['portrait', 'landscape'] as $orient) {
                if (isset($rect[$orient]) && is_array($rect[$orient])) {
                    $rect[$orient]['radius'] = 64;
                }
            }
            DB::table('device_presets')
                ->where('key', $key)
                ->update([
                    'real_frame_screen_rect' => json_encode($rect),
                    'updated_at' => now(),
                ]);
        }
    }
};
