<?php

namespace App\Http\Controllers\Auth;

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

class RegisterController extends Controller
{
    public function show()
    {
        return view('login.register');
    }

   public function store(Request $request)
   {
    $validated = $request->validate([
        'username' => 'required|min:4|max:20|unique:users,name',
        'password' => 'required|confirmed|min:6',
        'mobile' => 'required|digits:10|unique:users,mobile',
        'country_code' => 'required|in:IN,US',
        'referral_code' => 'nullable|alpha_num|max:10',
    ]);

    User::create([
        'name' => $validated['username'],
        'password' => Hash::make($validated['password']),
        'mobile' => $validated['mobile'],
        'country_code' => $validated['country_code'],
        'referral_code' => $validated['referral_code'] ?? null,
    ]);

    return redirect()->route('register')->with('success', 'Registration successful!');
   }
}
