<?php

namespace App\Http\Controllers;
use App\Models\AboutUs;
use App\Models\AboutUsGroup;
use App\Models\MenuProduct;
use App\Models\Product;
use App\Models\ContactInfo;
use App\Models\Gallery;
use App\Models\Banner;
use App\Models\Advertisement;
use App\Models\Team;
use App\Models\MenuCategory;
use App\Models\Service;
use App\Models\ServiceCategory;
use App\Models\Testimonial;
use App\Models\PageBanner;

use Illuminate\Support\Facades\Mail;
use App\Mail\AppointmentReceived;
use App\Mail\AppointmentConfirmation;
use Illuminate\Support\Str;
use Illuminate\Http\Request;




class UserController extends Controller
{
    public function index() {
    //     $contactInfo = ContactInfo::first();
    //     $about = AboutUs::first();
    //     $gallery = Gallery::all();
    //     $banner = Banner::all();
    //     $adv = Advertisement::all();
    //     $categories = MenuCategory::with('products')->whereNotIn('id', [10, 11])->get();
    //     $featuredproducts = MenuCategory::with('products')
    // ->whereIn('id', [10, 11])
    // ->get();
    // $serviceCategories = ServiceCategory::with('sproducts')->get();
    // return view('users.index', compact('about', 'contactInfo','gallery','categories','banner','adv','featuredproducts','serviceCategories'));
     $products = Product::all();
     $banner = Banner::all();
     $services = Service::latest()->get();
     $contactInfo = ContactInfo::first();
     $testimonials = Testimonial::latest()->get();
 
     $doctors = Team::where('team_type', 'Our_team')->latest()->take(10)->get();

     $glasses = Product::where('shop_category', 'water-technology')->take(8)->get();
     $sunglasses = Product::where('shop_category', 'pool-technology')->take(8)->get();
     $contacts = Product::where('shop_category', 'our-services')->take(8)->get();


    $adv = Advertisement::all();
    return view('users.index', compact('adv', 'products', 'glasses', 'sunglasses', 'contacts','banner', 'doctors','services', 'contactInfo', 'testimonials'));

    }
    public function show($id)
    {
        $product = Product::findOrFail($id);
        $contactInfo = ContactInfo::first();
        return view('users.pages.product-details', compact('product', 'contactInfo'));
    }
    public function showService($id)
    {
        $data = Service::findOrFail($id);
        $contactInfo = ContactInfo::first();
        return view('users.pages.services-details', compact('data', 'contactInfo'));
    }
   
    public function about() {
        $banner = PageBanner::where('page', 'about')->first();
        $groups = AboutUsGroup::with('aboutUsEntries')->get();
        $contactInfo = ContactInfo::first();
    
        return view('users.pages.about', compact('groups', 'contactInfo', 'banner'));
    }

    public function contact() {

        $contactInfo = ContactInfo::first();
        return view('users.pages.contact',compact ('contactInfo'));

    }
    public function glasses() {
        $contactInfo = ContactInfo::first();
        $data = Product::where('shop_category', 'glasses')->get();
       
            return view('users.pages.glasses', compact('data', 'contactInfo'));


    }
    public function sunGlassess() {
        $contactInfo = ContactInfo::first();
        $data = Product::where('shop_category', 'sunglasses')->get();
       
            return view('users.pages.sun-glasses', compact('data', 'contactInfo'));

    }
    public function contactLense() {
        $contactInfo = ContactInfo::first();
        $data = Product::where('shop_category', 'contacts')->get();
        return view('users.pages.contact-lense', compact('data', 'contactInfo'));

    }
    public function men() {
        $contactInfo = ContactInfo::first();
        $products = Product::where('category', 'men')->get();
        return view('users.pages.men', compact('products', 'contactInfo'));
    }
    
    public function women() {
        $contactInfo = ContactInfo::first();
        $products = Product::where('category', 'women')->get();
        return view('users.pages.women', compact('products', 'contactInfo'));
    }
    
    public function child() {
        $contactInfo = ContactInfo::first();
        $products = Product::where('category', 'Children')->get();
        return view('users.pages.child', compact('products', 'contactInfo'));
    }
    
    public function all() {
        $contactInfo = ContactInfo::first();
        $products = Product::all();
        return view('users.pages.all', compact('products', 'contactInfo'));
    }

    public function submitAppointment(Request $request)
    {
        $validated = $request->validate([
            'full_name' => 'required|string|max:255',
            'email' => 'required|email',
            'phone' => 'required',
            'date' => 'required|date',
            'time' => 'required',
            'message' => 'nullable|string'
        ]);
    
        // Send email to you (admin)
        Mail::to('pataneye@gmail.com')->send(new AppointmentReceived($validated));
    
        // Send confirmation email to user
        Mail::to($validated['email'])->send(new AppointmentConfirmation($validated));
    
        return back()->with('success', 'Appointment submitted! We’ll contact you soon.');
    }

}
