<?php

namespace App\Http\Controllers\Frontend;

use App\Helpers\CoreConstants;
use App\Http\Controllers\Controller;
use App\Models\Message;
use Illuminate\Http\Request;

class MessageController extends Controller
{
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|string',
            'email' => 'email',
            'phone' => 'required|string',
            'message' => 'required',
        ]);
        $message = new Message();
        $message->name = $request->name;
        $message->email = $request->email;
        $message->phone = $request->phone;
        $message->message = $request->message;
        $message->save();

        return redirect()->route('contact-us')->with('success', 'Your message has been sent successfully!');
    }

    public function index()
    {
        $userMessageShowClass = 'show';
        $messages = Message::all();
        return view('backend.user-message', compact('messages', 'userMessageShowClass'));
    }
    public function destroy($id)
    {
        $message = Message::findOrfail($id);
        $message->delete();
        return redirect()->route('admin.message')->with('success', 'Message deleted successfully!');
    }
}
