<?php

namespace App\Http\Controllers;

use App\Models\Device;
use Illuminate\Http\Request;

class DeviceController extends Controller
{
    /**
     * Show the device settings form.
     */
    public function index()
    {
        $device = Device::sole();
        return view('device.index', compact('device'));
    }

    /**
     * Update device settings.
     */
    public function update(Request $request)
    {
        $validated = $request->validate([
            'name'       => ['required', 'string', 'max:100'],
            'goal'       => ['nullable', 'string', 'max:255'],
            'weather'    => ['nullable', 'string', 'max:50'],
            'quote'      => ['nullable', 'string', 'max:255'],
            'brightness' => ['required', 'integer', 'min:0', 'max:100'],
            'animation'  => ['required', 'string', 'in:happy,sad,idle,excited,sleepy'],
            'refresh'    => ['required', 'integer', 'min:5', 'max:3600'],
        ]);

        $device = Device::sole();
        $device->update($validated);

        return redirect()->route('device.index')
            ->with('success', 'Device settings saved successfully!');
    }
}
