<?php

namespace Database\Seeders;

use App\Models\Server;
use Illuminate\Database\Seeder;

/**
 * Seeds a single REAL demo server from environment variables (so no fake fleet
 * and no credentials live in source). Set in .env (gitignored):
 *
 *   CLUSEV_DEMO_SSH_HOST=10.0.0.10
 *   CLUSEV_DEMO_SSH_USER=ops
 *   CLUSEV_DEMO_SSH_PASSWORD=...   (or CLUSEV_DEMO_SSH_KEY for a private key)
 *
 * With no CLUSEV_DEMO_SSH_HOST set, the fleet starts empty — servers are added
 * at runtime. The credential is stored encrypted via the SshCredential cast.
 */
class FleetSeeder extends Seeder
{
    public function run(): void
    {
        $host = env('CLUSEV_DEMO_SSH_HOST');

        if (! $host) {
            return;
        }

        $server = Server::updateOrCreate(['ip' => $host], [
            'name' => env('CLUSEV_DEMO_SSH_NAME', 'demo-01'),
            'ssh_port' => (int) env('CLUSEV_DEMO_SSH_PORT', 22),
            'status' => 'online',
        ]);

        $key = env('CLUSEV_DEMO_SSH_KEY');

        $server->credential()->updateOrCreate([], [
            'username' => env('CLUSEV_DEMO_SSH_USER', 'root'),
            'auth_type' => $key ? 'key' : 'password',
            'secret' => $key ?: env('CLUSEV_DEMO_SSH_PASSWORD', ''),
        ]);
    }
}
