<?php

namespace App\Http\Controllers;

use App\Models\ConsultationRequest;
use App\Models\Faq;
use App\Models\PartnerUniversity;
use App\Models\NewsletterSubscription;
use App\Models\Scholarship;
use App\Models\Service;
use App\Models\Setting;
use App\Models\Testimonial;
use App\Models\University;
use Illuminate\Http\Request;

class ContactController extends Controller
{
    /**
     * Show the contact form.
     */
    public function showContact()
    {
        $settings = [
            'contact_email' => Setting::getValue('contact_email', 'info@insightglobalconsultant.com'),
            'contact_phone' => Setting::getValue('contact_phone', '+12 (123) 456 78900'),
            'address' => Setting::getValue('address', '123 Scholar Street, Education City'),
        ];

        return view('pages.contact', compact('settings'));
    }

    /**
     * Show the About page.
     */
  public function showAbout()
{
    $testimonials = Testimonial::approved()->latest()->take(6)->get();
    $services = Service::orderBy('order')->take(4)->get();
    $partners = PartnerUniversity::orderBy('order')->take(6)->get();
    $stats = [
        'students' => Setting::getValue('stats_students_placed', 1500),
        'visa' => Setting::getValue('stats_visa_approval_rate', 98),
        'partners' => Setting::getValue('stats_partner_universities', University::count()),
        'countries' => Setting::getValue('stats_countries', \App\Models\Country::count()),
        'scholarships' => Scholarship::published()->count(),
    ];

    return view('pages.about', compact('testimonials', 'services', 'partners', 'stats'));
}
    /**
     * Show the FAQ page.
     */
    public function showFaq()
    {
        $faqs = Faq::orderBy('order')->get();
        return view('pages.faq', compact('faqs'));
    }

    /**
     * Show a search page (could redirect to scholarship list with search query).
     */
    public function search(Request $request)
    {
        // This is a dedicated search page; you can forward to the scholarship search
        $search = $request->input('query');
        return redirect()->route('scholarships.index', ['search' => $search]);
    }

    /**
     * Handle contact form submission.
     */
    public function submit(Request $request)
    {
        $validated = $request->validate([
            'name'             => 'required|min:3',
            'email'            => 'required|email',
            'phone'            => 'nullable|string|max:20',
            'message'          => 'required|min:10',
            'preferred_date'   => 'nullable|date',
        ]);

        ConsultationRequest::create($validated + ['status' => 'pending']);

        return back()->with('success', 'Thank you! We will contact you shortly.');
    }

    /**
     * Subscribe a newsletter email.
     */
    public function subscribeToNewsletter(Request $request)
    {
        $request->validate([
            'email' => 'required|email|unique:newsletter_subscriptions,email',
        ]);

        // If you have a NewsletterSubscription model, store it
       NewsletterSubscription::create(['email' => $request->email]);

        return back()->with('message', 'Successfully subscribed!');
    }
}
