<?php

namespace App\Http\Controllers\Api;

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

class AuthController extends Controller
{
   /* public function login(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);

        if (!Auth::attempt($request->only('email', 'password'))) {
            return response()->json([
                'message' => 'Identifiants incorrects'
            ], 401);
        }

        $user = Auth::user();

        return response()->json([
            'id'         => $user->id,
            'name'      => $user->name,
            'email'      => $user->email,
            'inspecteur' => [
                'id' => $user->inspecteur->id ?? null,
                'nomfr' => $user->inspecteur->nomfr ?? null,
                'nomar' => $user->inspecteur->nomar ?? null,
                'langue' => $user->inspecteur->discipline->langue ?? null,
            ],
            'created_at' => $user->created_at->format('d/m/Y H:i'),
            'token' => $user->createToken('mobile-app')->plainTextToken,
        ]);
    }*/
    public function login(Request $request)
{
    $request->validate([
        'email' => 'required|email',
        'password' => 'required',
    ]);

    $user = User::with(['inspecteur.pole', 'inspecteur.discipline'])->where('email', $request->email)->first();

    if (!$user || !Hash::check($request->password, $user->password)) {
        return response()->json([
            'message' => 'Email ou mot de passe incorrect'
        ], 401);
    }

    $token = $user->createToken('auth_token')->plainTextToken;

    return response()->json([
        'token' => $token,
        'user' => $user,
    ], 200);
}

    public function register(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|min:6',
            'inspecteur_id' => 'required|exists:inspecteurs,id',
        ]);

        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'inspecteur_id' => $data['inspecteur_id'],
        ]);


        $token = $user->createToken('mobile-app')->plainTextToken;

        return response()->json([
            'message' => 'Utilisateur créé avec succès',
            'id'         => $user->id,
            'name'      => $user->name,
            'email'      => $user->email,
            'inspecteur' => [
                'id' => $user->inspecteur->id ?? null,
                'nomfr' => $user->inspecteur->nomfr ?? null,
                'nomar' => $user->inspecteur->nomar ?? null,
            ],
            'created_at' => $user->created_at->format('d/m/Y H:i'),
            'user' => $user,
            'token' => $token
        ], 201);
    }

    public function me(Request $request)
    {
        return response()->json($request->user());
    }
    
}
