<?php

namespace App\Http\Controllers;

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

class UserDeleteController extends Controller
{
    public function index()
    {
        return view('front.delete-account');
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|email|exists:users,email',
        ]);
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        $user = User::where('email', $request->email)->first();

        if ($user) {
            $user->delete();
            return redirect()->route('user.index')->with('success', 'User deleted successfully.');
        }

        return redirect()->back()->with('error', 'Invalid email.');
    }
}
