<?php

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

/**
 * Galaxy S26: drop screen-cutout radius 64 → 0. Same bezel-undercut fix
 * as iPad Pro + Galaxy Tab S8 Ultra — the PNG corner curve is a squircle,
 * not a quarter-circle, so the screenshot's rounded clip falls short of
 * the bezel's actual cutout and leaves a visible white gap at each
 * corner. Radius 0 = rectangular screenshot; the bezel PNG sits on top
 * and its opaque pixels cover any corner overdraw. Matches the "let the
 * bezel decide" strategy.
 *
 * Measured corner curve on the S26 PNGs: ~152 px (the original 64 was
 * eyeballed). Going to 152 would also work but the zero-radius approach
 * is more robust against future PNG variations.
 */
return new class extends Migration
{
    private const KEYS = [
        'android-galaxy-s26',
        'android-galaxy-s26-landscape',
    ];

    public function up(): void
    {
        foreach (self::KEYS as $key) {
            $this->setRadiusFor($key, 0);
        }
    }

    public function down(): void
    {
        // Restore the pre-fix radius (eyeballed initial value, before the
        // squircle measurement landed).
        foreach (self::KEYS as $key) {
            $this->setRadiusFor($key, 64);
        }
    }

    private function setRadiusFor(string $key, int $r): void
    {
        $row = DB::table('device_presets')->where('key', $key)->first();
        if (! $row || ! is_string($row->real_frame_screen_rect)) {
            return;
        }
        $rect = json_decode($row->real_frame_screen_rect, true);
        if (! is_array($rect)) {
            return;
        }
        foreach (['portrait', 'landscape'] as $orient) {
            if (isset($rect[$orient]) && is_array($rect[$orient])) {
                $rect[$orient]['radius'] = $r;
            }
        }
        DB::table('device_presets')
            ->where('key', $key)
            ->update([
                'real_frame_screen_rect' => json_encode($rect),
                'updated_at' => now(),
            ]);
    }
};
