<?php

namespace App\Livewire\Pages\Profile;

use Illuminate\View\View;
use Livewire\Component;

class Preferences extends Component
{
    public string $locale = 'de';

    public string $theme = 'dark';

    public function mount(): void
    {
        $this->locale = auth()->user()->locale ?? 'de';
        $this->theme = auth()->user()->theme ?? 'dark';
    }

    public function save(): void
    {
        $this->validate([
            'locale' => 'required|in:de,en',
            'theme' => 'required|in:dark,light,system',
        ]);

        auth()->user()->update([
            'locale' => $this->locale,
            'theme' => $this->theme,
        ]);

        $this->dispatch('theme-changed-server', theme: $this->theme);
        session()->flash('status', 'Preferences saved.');
    }

    public function render(): View
    {
        return view('livewire.pages.profile.preferences')
            ->layout('layouts.nimuli-app', ['title' => 'Preferences']);
    }
}
