<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'nev' => 'required|string',
            'email' => 'required|email|unique:felhasznalok',
            'felh_nev' => 'required|string|unique:felhasznalok',
            'jelszo' => 'required|string|min:6',
            'is_admin' => 'boolean'
        ]);

        try {
            $felhasznalo = User::create([
                'nev' => $validatedData['nev'],
                'email' => $validatedData['email'],
                'felh_nev' => $validatedData['felh_nev'],
                'jelszo' => $validatedData['jelszo'],
                'is_admin' => $validatedData['is_admin'] ?? false
            ]);

            return response()->json([
                'message' => 'Sikeres rögzítés',
                'data' => $felhasznalo
            ], 201);
        } catch (\Exception $e) {
            return response()->json([
                'message' => 'Hiba történt a mentés során',
                'error' => $e->getMessage()
            ], 500);
        }
    }
}