<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Instalación — {{ config('app.name') }}</title>
    <style>
        :root { color-scheme: light dark; }
        body { font-family: system-ui, sans-serif; max-width: 32rem; margin: 2rem auto; padding: 0 1rem; line-height: 1.45; }
        h1 { font-size: 1.25rem; margin-bottom: 1rem; }
        label { display: block; margin-top: 0.75rem; font-weight: 600; font-size: 0.875rem; }
        input[type="text"], input[type="url"], input[type="password"], input[type="number"] {
            width: 100%; box-sizing: border-box; padding: 0.4rem 0.5rem; margin-top: 0.25rem;
        }
        .hint { font-size: 0.8rem; opacity: 0.85; margin-top: 0.15rem; }
        .err { color: #b91c1c; font-size: 0.875rem; margin-top: 0.25rem; }
        .global-err { background: #fef2f2; border: 1px solid #fecaca; padding: 0.75rem; border-radius: 6px; margin-bottom: 1rem; }
        button { margin-top: 1.25rem; padding: 0.5rem 1rem; font-weight: 600; cursor: pointer; }
        .check { margin-top: 0.75rem; display: flex; align-items: center; gap: 0.5rem; }
        .check input { width: auto; }
    </style>
</head>
<body>
    <h1>Instalación</h1>
    <p>Completá los datos. Se escribirá <code>.env</code>, se generará <code>APP_KEY</code> si falta, se ejecutarán migraciones y se marcará la app como instalada.</p>

    @if ($errors->has('install'))
        <div class="global-err">{{ $errors->first('install') }}</div>
    @endif

    <form method="post" action="{{ url('/install') }}">
        @csrf

        @if (!empty($requires_install_secret))
            <label for="install_secret">Clave de instalación</label>
            <input type="password" id="install_secret" name="install_secret" value="{{ old('install_secret') }}" autocomplete="off" required>
            @error('install_secret')<div class="err">{{ $message }}</div>@enderror
            <p class="hint">Definida en <code>INSTALL_SECRET</code> del <code>.env</code> actual.</p>
        @endif

        <label for="app_name">Nombre de la app</label>
        <input type="text" id="app_name" name="app_name" value="{{ old('app_name', $defaults['app_name'] ?? '') }}" required>

        <label for="app_url">APP_URL (público, con https en producción)</label>
        <input type="url" id="app_url" name="app_url" value="{{ old('app_url', $defaults['app_url'] ?? '') }}" required placeholder="https://ejemplo.com">

        <label for="frontend_url">FRONTEND_URL (opcional)</label>
        <input type="url" id="frontend_url" name="frontend_url" value="{{ old('frontend_url', $defaults['frontend_url'] ?? '') }}" placeholder="Si vacío, se usa APP_URL">
        <p class="hint">Se usa para Sanctum: <code>SANCTUM_STATEFUL_DOMAINS</code> se deriva del host de esta URL.</p>

        @php
            $dbgDefault = ($defaults['app_debug'] ?? false) ? '1' : '0';
        @endphp
        <div class="check">
            <input type="hidden" name="app_debug" value="0">
            <input type="checkbox" id="app_debug" name="app_debug" value="1" @checked(old('app_debug', $dbgDefault) === '1')>
            <label for="app_debug" style="margin:0;font-weight:500;">APP_DEBUG (solo desarrollo)</label>
        </div>

        <label for="db_host">MySQL host</label>
        <input type="text" id="db_host" name="db_host" value="{{ old('db_host', $defaults['db_host'] ?? '127.0.0.1') }}" required>

        <label for="db_port">Puerto</label>
        <input type="number" id="db_port" name="db_port" value="{{ old('db_port', $defaults['db_port'] ?? '3306') }}" required min="1" max="65535">

        <label for="db_database">Base de datos</label>
        <input type="text" id="db_database" name="db_database" value="{{ old('db_database', $defaults['db_database'] ?? '') }}" required>
        @error('db_database')<div class="err">{{ $message }}</div>@enderror

        <label for="db_username">Usuario</label>
        <input type="text" id="db_username" name="db_username" value="{{ old('db_username', $defaults['db_username'] ?? '') }}" required autocomplete="off">

        <label for="db_password">Contraseña</label>
        <input type="password" id="db_password" name="db_password" value="{{ old('db_password', $defaults['db_password'] ?? '') }}" autocomplete="off">

        <button type="submit">Instalar</button>
    </form>
</body>
</html>
