<?php

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

return new class extends Migration
{
    /**
     * Android preset set matching the iOS headcount (5 phones + 5 tablets).
     * Keys are prefixed `android-` so the LayerNode bezel branch can key off
     * the slug.
     *
     * Google Play is range-based (1080–7680 px) without tier equivalents, so
     * `apple_tier` is null. Safe-area top covers status bar + punch-hole on
     * phones; tablets expose a zeroed inset (no cross-device hole to guard).
     */
    public function up(): void
    {
        $hasTier = Schema::hasColumn('device_presets', 'apple_tier');
        $hasFactor = Schema::hasColumn('device_presets', 'form_factor');

        $now = now();
        $rows = [];
        foreach ($this->presets() as $p) {
            $row = [
                'key' => $p['key'],
                'label' => $p['label'],
                'platform' => 'android',
                'category' => $p['category'],
                'export_w' => $p['export_w'],
                'export_h' => $p['export_h'],
                'frame_svg_path' => null,
                'safe_area' => json_encode($p['safe_area']),
                'is_active' => true,
                'created_at' => $now,
                'updated_at' => $now,
            ];
            if ($hasTier) {
                $row['apple_tier'] = null;
            }
            if ($hasFactor) {
                $row['form_factor'] = $p['category'] === 'tablet'
                    ? 'android_tablet_portrait'
                    : 'android_phone_portrait';
            }
            $rows[] = $row;
        }
        $updateCols = ['label', 'platform', 'category', 'export_w', 'export_h', 'safe_area', 'is_active', 'updated_at'];
        if ($hasTier) {
            $updateCols[] = 'apple_tier';
        }
        if ($hasFactor) {
            $updateCols[] = 'form_factor';
        }
        DB::table('device_presets')->upsert($rows, ['key'], $updateCols);
    }

    public function down(): void
    {
        DB::table('device_presets')
            ->whereIn('key', array_column($this->presets(), 'key'))
            ->delete();
    }

    /** @return array<int, array<string, mixed>> */
    private function presets(): array
    {
        return [
            // Phones — punch-hole devices, modest top safe-area inset.
            [
                'key' => 'android-pixel-9',
                'label' => 'Pixel 9',
                'category' => 'phone',
                'export_w' => 1080,
                'export_h' => 2424,
                'safe_area' => ['top' => 80, 'right' => 0, 'bottom' => 48, 'left' => 0],
            ],
            [
                'key' => 'android-pixel-9-pro',
                'label' => 'Pixel 9 Pro',
                'category' => 'phone',
                'export_w' => 1280,
                'export_h' => 2856,
                'safe_area' => ['top' => 80, 'right' => 0, 'bottom' => 48, 'left' => 0],
            ],
            [
                'key' => 'android-galaxy-s24',
                'label' => 'Samsung Galaxy S24',
                'category' => 'phone',
                'export_w' => 1080,
                'export_h' => 2340,
                'safe_area' => ['top' => 80, 'right' => 0, 'bottom' => 48, 'left' => 0],
            ],
            [
                'key' => 'android-galaxy-s24-ultra',
                'label' => 'Samsung Galaxy S24 Ultra',
                'category' => 'phone',
                'export_w' => 1440,
                'export_h' => 3120,
                'safe_area' => ['top' => 80, 'right' => 0, 'bottom' => 48, 'left' => 0],
            ],
            [
                'key' => 'android-oneplus-12',
                'label' => 'OnePlus 12',
                'category' => 'phone',
                'export_w' => 1440,
                'export_h' => 3168,
                'safe_area' => ['top' => 80, 'right' => 0, 'bottom' => 48, 'left' => 0],
            ],

            // Tablets — no punch-hole, plain bezel.
            [
                'key' => 'android-pixel-tablet',
                'label' => 'Pixel Tablet',
                'category' => 'tablet',
                'export_w' => 1600,
                'export_h' => 2560,
                'safe_area' => ['top' => 0, 'right' => 0, 'bottom' => 0, 'left' => 0],
            ],
            [
                'key' => 'android-galaxy-tab-s10',
                'label' => 'Samsung Galaxy Tab S10+',
                'category' => 'tablet',
                'export_w' => 1752,
                'export_h' => 2800,
                'safe_area' => ['top' => 0, 'right' => 0, 'bottom' => 0, 'left' => 0],
            ],
            [
                'key' => 'android-galaxy-tab-s9',
                'label' => 'Samsung Galaxy Tab S9',
                'category' => 'tablet',
                'export_w' => 1600,
                'export_h' => 2560,
                'safe_area' => ['top' => 0, 'right' => 0, 'bottom' => 0, 'left' => 0],
            ],
            [
                'key' => 'android-oneplus-pad-2',
                'label' => 'OnePlus Pad 2',
                'category' => 'tablet',
                'export_w' => 2120,
                'export_h' => 3000,
                'safe_area' => ['top' => 0, 'right' => 0, 'bottom' => 0, 'left' => 0],
            ],
            [
                'key' => 'android-lenovo-tab-p12',
                'label' => 'Lenovo Tab P12',
                'category' => 'tablet',
                'export_w' => 1840,
                'export_h' => 2944,
                'safe_area' => ['top' => 0, 'right' => 0, 'bottom' => 0, 'left' => 0],
            ],
        ];
    }
};
