<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Service;
use App\Models\ContactMessage;

class PageController extends Controller
{
    public function home()
    {
        $services = Service::where('is_active', true)->orderBy('order_index')->take(6)->get();
        $testimonials = \App\Models\Testimonial::where('is_active', true)->orderBy('order_index')->get();
        $features = \App\Models\Feature::where('is_active', true)->orderBy('order_index')->get();
        $settings = \App\Models\SiteSetting::where('key', 'like', 'home_%')->pluck('value', 'key')->toArray();
        $galleryItems = \App\Models\GalleryItem::where('is_active', true)->where('show_on_homepage', true)->orderBy('order_index')->take(6)->get();
        $blogs = \App\Models\Blog::where('is_published', true)->orderBy('created_at', 'desc')->take(3)->get();

        return view('home', compact('services', 'testimonials', 'features', 'settings', 'galleryItems', 'blogs'));
    }

    public function about()
    {
        $settings = \App\Models\SiteSetting::where('key', 'like', 'about_%')->pluck('value', 'key')->toArray();
        $testimonials = \App\Models\Testimonial::where('is_active', true)->orderBy('order_index')->get();
        return view('about', compact('settings', 'testimonials'));
    }

    public function services()
    {
        $services = Service::where('is_active', true)->orderBy('order_index')->get();
        return view('services', compact('services'));
    }

    public function serviceDetail($slug)
    {
        $service = Service::where('slug', $slug)->where('is_active', true)->firstOrFail();
        return view('service-details', compact('service'));
    }

    public function whyLaparoscopy()
    {
        return view('why-laparoscopy');
    }

    public function contact()
    {
        return view('contact');
    }

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

        ContactMessage::create($validated);

        return back()->with('success', 'Your message has been sent successfully. We will get back to you soon.');
    }

    public function submitTestimonial(Request $request)
    {
        $validated = $request->validate([
            'author_name' => 'required|string|max:255',
            'text' => 'required|string',
        ]);

        $validated['is_active'] = false; // Requires admin approval
        
        \App\Models\Testimonial::create($validated);

        return back()->with('success', 'Thank you! Your story has been submitted and is pending approval.');
    }

    public function gallery()
    {
        $galleryItems = \App\Models\GalleryItem::where('is_active', true)
                                ->orderBy('created_at', 'desc')
                                ->get();
        
        return view('gallery', compact('galleryItems'));
    }

    public function appointment()
    {
        $services = Service::where('is_active', true)->orderBy('order_index')->get();
        return view('appointment', compact('services'));
    }

    public function submitAppointment(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'phone' => 'required|string|max:20',
            'preferred_date' => 'nullable|date',
            'preferred_time' => 'nullable|string|max:255',
            'service' => 'nullable|string|max:255',
            'message' => 'nullable|string',
        ]);

        \App\Models\Appointment::create($validated);

        return back()->with('success', 'Your appointment request has been submitted successfully. We will contact you soon to confirm.');
    }
}
