<?php

namespace App\Http\Controllers\Api;

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

class AuthController extends Controller
{

    /* public function __construct()
    {
        $this->middleware('check.login')->only('login');
    } */
    
    public function signup(Request $request)
    {
        $request->validate([
            'username' => 'required|string|unique:users',
            'email'    => 'required|email|unique:users',
            'password' => 'required|string|min:6',
        ]);

        $user = User::create([
            'username' => $request->username,
            'email'    => $request->email,
            'password' => Hash::make($request->password),
            'otp'      => rand(1000, 9999), 
        ]);

        return response()->json([
            'success' => true,
            'message' => 'Signup successful', 'user' => $user]);
    }

    
    public function login(Request $request)
    {
        $request->validate([
            'email'    => 'required_without:username|email',
            'username' => 'required_without:email|string',
            'password' => 'required|string',
        ]);

        $credentials = $request->only('email', 'password');
        if (!$request->has('email')) {
            $credentials = ['username' => $request->username, 'password' => $request->password];
        }
        if (!Auth::attempt($credentials)) {
            return response()->json([
                'success' => false,
                'message' => 'Invalid credentials',
                'error' => 'Invalid credentials'], 401);
        }

        $token = Auth::user()->createToken('auth_token')->plainTextToken;
        $user = Auth::user();
        
        return response()->json([
            'success' => true,
            'message' => 'Login successful',
            'token' => $token,
            'user'=> Auth::user()
        ]);
    }


    public function sendOtp(Request $request)
    {
        $request->validate(['email' => 'required|email']);

        $user = User::where('email', $request->email)->first();
        if (!$user) {
            return response()->json([
                'success' => false,
                'message' => 'No user found'
            ], 200);
        }

        /* $otp = rand(1000, 9999); */
        $otp = 1234;
        $user->update(['otp' => $otp]);
        return response()->json([
            'success' => true,
            'message' => 'OTP generated successfully', 'otp' => $otp]);
    }

    
    public function verifyOtp(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            'otp'   => 'required|string',
        ]);

        $user = User::where('email', $request->email)->first();
        if (!$user || $user->otp !== $request->otp) {
            return response()->json([
                'success' => false,
                'message' => 'Invalid OTP',
                'error' => 'Invalid OTP'], 401);
        }

        return response()->json([
            'success' => true,
            'message' => 'OTP Verification Successful']);
    }

    
    public function resetPassword(Request $request)
    {
        $request->validate([
            'email'       => 'required|email',
            'newpassword' => 'required|string|min:6',
        ]);

        $user = User::where('email', $request->email)->first();
        if (!$user) {
            return response()->json(['error' => 'No user found'], 200);
        }

        $user->update(['password' => Hash::make($request->newpassword)]);

        return response()->json([
            'success' => true,
            'message' => 'Password updated successfully']);
    }

    
    public function logout(Request $request)
    {
        $request->user()->tokens()->delete();
        return response()->json([
            'success' => true,
            'message' => 'Logged out successfully']);
    }
}
