<?php

namespace App\Http\Controllers;

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

class FanController extends Controller
{
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('fans.createaccount');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http
     */
    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'phone' => 'required|unique:users|string|max:255',
            'password' => 'required|string|min:8',
        ]);

        $user = User::create([
            'phone' => $validatedData['phone'],
            'password_hash' => Hash::make($validatedData['password']),
            'role' => 'fan',
        ]);

        Auth::login($user);

        return redirect()->route('fans.profile');
    }

    /**
     * Display the fan's profile.
     *
     * @return \Illuminate\Http\Response
     */
    public function profile()
    {
        return view('fans.index');
    }
}
