<?php

namespace App\Http\Controllers;

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

class AuthController extends Controller
{
    // =========================================
    // 1. SIGNUP / REGISTER LOGIC
    // =========================================
    public function register(Request $request)
    {
        // Data check karna (Validation)
        $request->validate([
            'name' => 'required|string|max:255',
            'phone' => 'required|string|unique:users|max:20', // Phone number dobara na aaye
            'password' => 'required|string|min:6',
            'role' => 'nullable|string'
        ]);

        // Database mein User save karna
        $user = User::create([
            'name' => $request->name,
            'phone' => $request->phone,
            'password' => Hash::make($request->password), // Password ko encrypt karna
            'role' => $request->role ?? 'user', // React se role aayega, warna default 'user'
            'status' => 'pending', // Naya account hamesha pending rahega
        ]);

        // Token banana (React isko localStorage mein save karega)
        $token = $user->createToken('auth_token')->plainTextToken;

        return response()->json([
            'message' => 'Account created successfully!',
            'user' => $user,
            'token' => $token
        ], 201);
    }

    // =========================================
    // 2. LOGIN LOGIC
    // =========================================
    public function login(Request $request)
    {
        $request->validate([
            'phone' => 'required|string',
            'password' => 'required|string',
        ]);

        // Phone number se user dhoondhna
        $user = User::where('phone', $request->phone)->first();

        // Agar user na mile ya password galat ho
        if (!$user || !Hash::check($request->password, $user->password)) {
            return response()->json([
                'message' => 'Galat phone number ya password!'
            ], 401);
        }

        // Agar account Admin ne Block kiya hua hai
        if ($user->role !== 'admin' && $user->status === 'blocked') {
            return response()->json([
                'message' => 'Aapka account block ho gaya hai. Admin se sampark karein.'
            ], 403);
        }

        // Agar account abhi Approve nahi hua hai
        if ($user->role !== 'admin' && $user->status === 'pending') {
            return response()->json([
                'message' => 'Account verify ho raha hai. Kripya Admin approval ka wait karein.'
            ], 403);
        }

        // Sab sahi hai toh naya Token banao
        $token = $user->createToken('auth_token')->plainTextToken;

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

    // =========================================
    // 3. LOGOUT LOGIC
    // =========================================
    public function logout(Request $request)
    {
        // User ka token delete kar do (Taala badal do)
        $request->user()->currentAccessToken()->delete();

        return response()->json([
            'message' => 'Logged out successfully'
        ], 200);
    }
}