<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ContactUs;
use Illuminate\Support\Facades\Mail;
use App\Mail\ContactUsConfirmation;

class ContactUsController extends Controller
{
    public function index()
    {
        $messages = ContactUs::orderBy('created_at', 'desc')->paginate(15);
        return view('Backend.ContactUs.contact-us', [
            'messages' => $messages,
        ]);
    }

    public function markAsRead($id)
    {
        ContactUs::find($id)->update(['is_read' => true]);
        return back()
            ->with('success', 'Message marked as read')
            ->with('toast', true);
    }

    public function destroy($id)
    {
        ContactUs::find($id)->delete();
        return back()
            ->with('success', 'Message deleted successfully')
            ->with('toast', true);
    }

    public function contactUs(Request $request)
    {
        $request->validate([
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required|email',
            'phone' => 'required',
            'subject' => 'required',
            'message' => 'required',
        ]);

        $contactUs = ContactUs::create($request->all());

        // Send confirmation email
        // try {
        //     // Send confirmation email to the user
        //     Mail::to($request->email)->send(new ContactUsConfirmation($request->all(), false));

        //     // Send notification email to the admin
        //     Mail::to('ishtiaqueferdous109@gmail.com')->send(new ContactUsConfirmation($request->all(), true));
        // } catch (\Exception $e) {
        //     return sendErrorResponse('Failed to send contact us confirmation email: ' . $e->getMessage(), 500);
        // }

        return sendSuccessResponse('Contact us successful', 200);
    }
}
