<?php

namespace App\Http\Controllers;

use App\Models\About;
use App\Models\Bestsellers;
use App\Models\BookOffers;
use App\Models\Catagory;
use App\Models\Featured;
use App\Models\LatestArticles;
use App\Models\MemoirsPersonal;
use App\Models\Popular;
use App\Models\Setting;
use App\Models\Slider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PublicController extends Controller
{
    public function index(){
          $features = Featured::select('id','title','image','alt_text','price','author','amazon_link','slug','priority','status',)
            ->where('status', 1)
            ->orderBy('priority')
            ->limit(3)
            ->get();
        $sliders = Slider::select('id','title','image','alt_text','description','amazon_link','author','priority','status',)
            ->where('status', 1)
            ->orderBy('priority')
            ->limit(4)
            ->get();
        $catagories = Catagory::select('id','title','image','amazon_link','alt_text','priority','status',)
            ->where('status', 1)
            ->orderBy('priority')
            ->limit(4)
            ->get();
        $populars = Popular::select('id','title','image','alt_text','price','author','amazon_link','category_id','slug','priority','status',)
            ->where('status', 1)
            ->orderBy('priority')

            ->get();
        $latestArticles = LatestArticles::select('id','title','image','alt_text','date','category_id','amazon_link','slug','priority','status',)
            ->where('status', 1)
            ->orderBy('priority')
            ->limit(3)
            ->get();
        $bookOffers = BookOffers::select('id','title','image','alt_text','author','amazon_link','category_id','slug','priority','status',)
            ->where('status', 1)
            ->orderBy('priority')
            ->limit(4)
            ->get();



        $memoirsPersonals = MemoirsPersonal::select('id','title','image','alt_text','author','amazon_link','category_id','slug','priority','status',)
            ->where('status', 1)
            ->orderBy('priority')
            ->limit(4)
            ->get();
        $bestsellers = Bestsellers::select('id','image','alt_text','description','title','price','amazon_link','author','quta_description','quta_title','quta_author')
            ->first();


        return view('frontend.pages.index', compact('memoirsPersonals','features', 'catagories','populars', 'bestsellers','sliders','bookOffers','latestArticles'));
    }



    public function master(){

        return view('backend.pages.master');
    }

     public function features(Request $request){

     $query = Featured::select('id','title','image','alt_text','price','author','amazon_link','slug','priority','status',)->where('status', 1);

        if ($request->filled('search')) {
            $search = $request->search;
            $query->where(function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%");

            });
        }

        $features = $query->orderBy('priority')->paginate(8);
        return view('frontend.pages.features', compact('features'));


    }

       public function cart(){

        return view('frontend.pages.cart');
    }
    public function about(){
         $about = About::select('id','image','alt_text','description','title','author','amazon_link')
            ->first();

        return view('frontend.pages.about', compact('about'));
    }



    public function catagory(Request $request){
            $query = Catagory::select('id','title','image','amazon_link','alt_text','priority','status',)->where('status', 1);

        if ($request->filled('search')) {
            $search = $request->search;
            $query->where(function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%");

            });
        }

        $catagories = $query->orderBy('priority')->paginate(8);
        return view('frontend.pages.catagory', compact('catagories'));


        return view('frontend.pages.catagory');
    }

     public function popular(Request $request)
{
    $query = Popular::select(
        'id','title','image','alt_text','price','author',
        'amazon_link','category_id','slug','priority','status'
    )->where('status', 1);

    // Search
    if ($request->filled('search')) {
        $search = $request->search;

        $query->where(function ($q) use ($search) {
            $q->where('title', 'like', "%{$search}%");
        });
    }

    // Categories
    $catagories = Catagory::select(
        'id','title','image','amazon_link','alt_text','priority','status'
    )
    ->where('status', 1)
    ->orderBy('priority')
    ->get();

    // Popular Books
    $populars = $query->orderBy('created_at', 'DESC')->paginate(10);

    return view('frontend.pages.popular', compact('populars','catagories'));
}

    public function contact(){
          $setting = Setting::select('id','header_logo','footer_logo','footer_title','contact_title','address','phone','email','google_link')
            ->first();

        return view('frontend.pages.contact');
    }
   public function bookOffers(Request $request)
    {
        $query = BookOffers::select(
            'id','title','image','alt_text','author',
            'amazon_link','category_id','slug','priority','status'
        )->where('status', 1);

        // Search
        if ($request->filled('search')) {
            $search = $request->search;

            $query->where(function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%");
            });
        }

        // Categories
        $catagories = Catagory::select(
               'id','title','image','amazon_link','alt_text','priority','status'
        )
        ->where('status', 1)
        ->orderBy('priority')
        ->get();

    // Popular Books
    $bookOffers = $query->orderBy('created_at', 'DESC')->paginate(8);

    return view('frontend.pages.bookOffers', compact('bookOffers','catagories'));
}

public function latestArticles(Request $request)
    {
        $query = LatestArticles::select(
            'id','title','image','alt_text','date','category_id','amazon_link','slug','priority','status',
        )->where('status', 1);

        // Search
        if ($request->filled('search')) {
            $search = $request->search;
            $query->where(function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%");
            });
        }

        // Categories
        $catagories = Catagory::select(
              'id','title','image','amazon_link','alt_text','priority','status',
        )
        ->where('status', 1)
        ->orderBy('priority')
        ->get();

    // Popular Books
    $latestArticles = $query->orderBy('created_at', 'DESC')->paginate(6);

    return view('frontend.pages.articles', compact('latestArticles','catagories'));
}

public function logout(Request $request)
{
    Auth::logout();
    $request->session()->invalidate();
    $request->session()->regenerateToken();

    return redirect('/login');
}


public function memoirsPersonals(Request $request)
    {
        $query = MemoirsPersonal::select(
            'id','title','image','alt_text','author',
            'amazon_link','category_id','slug','priority','status'
        )->where('status', 1);

        // Search
        if ($request->filled('search')) {
            $search = $request->search;

            $query->where(function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%");
            });
        }

        // Categories
        $catagories = Catagory::select(
               'id','title','image','amazon_link','alt_text','priority','status'
        )
        ->where('status', 1)
        ->orderBy('priority')
        ->get();

    // Popular Books
    $memoirsPersonals = $query->orderBy('created_at', 'DESC')->paginate(8);

    return view('frontend.pages.memoirsPersonals', compact('memoirsPersonals','catagories'));
}
public function author(){

    return view('frontend.pages.author');

}

public function booksAuthor(){

    return view('frontend.pages.booksAuthor');

}



}


