<?php

namespace App\Http\Controllers;

use App\Helpers\CMail;
use Illuminate\Http\Request;
use Artesaos\SEOTools\Facades\SEOTools;
use Artesaos\SEOTools\Facades\SEOMeta;


class ContactController extends Controller
{
    public function contactUs()
    {
        $title = 'Contact Us | Polytechnic College of Davao del Sur';
        $description = isset(settings()->site_meta_description) ? settings()->site_meta_description : '';
        $imgURL = isset(settings()->site_logo) ? asset('/images/site/' . settings()->site_logo) : '';
        $keywords = isset(settings()->site_meta_keywords) ? settings()->site_meta_keywords : '';
        $currentUrl = url()->current();
        /** Meta SEO*/
        SEOTools::setTitle($title, false);
        SEOTools::setDescription($description);
        SEOMeta::setKeywords($keywords);
        /** Open Graph*/
        SEOTools::opengraph()->setUrl($currentUrl);
        SEOTools::opengraph()->addImage($imgURL);
        SEOTools::opengraph()->addProperty('type', 'articles');

        SEOTools::twitter()->addImage($imgURL);
        SEOTools::twitter()->setUrl($currentUrl);
        SEOTools::twitter()->setSite('@larablog');

        $data = [
            'pageTitle' => $title
        ];

        return view('front.pages.contact', $data);
    }

    public function contactPost(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email',
            'phone' => 'nullable|string|max:20',
            'message' => 'required|string|max:2000',
        ]);

        $config = [
            'subject' => 'New Contact Form Submission',
            'recipient_address' => 'brigolemacky75@gmail.com',
            'body' => view('email-templates.contact-template', [
                'name' => $request->name,
                'email' => $request->email,
                'phone' => $request->phone,
                'message_content' => $request->message,
            ])->render(),
            'from_address' => $request->email,
            'from_name' => $request->name,
        ];

        // ❗You forgot to actually call CMail::send() — fix that here:
        $mailSent = CMail::send($config);

        if ($mailSent) {
            return back()->with('success', 'Your message has been sent successfully!');
        } else {
            return back()->withErrors(['mail' => 'There was a problem sending your message. Please try again later.']);
        }
    }
}
