<?php

namespace App\Http\Controllers;

use App\Models\About;
use App\Models\Achievement;
use App\Models\AchievementCategory;
use App\Models\Album;
use App\Models\Blog;
use App\Models\Contact;
use App\Models\FuturePlan;
use App\Models\ImageGallery;
use App\Models\Policy;
use App\Models\PolicyCategory;
use App\Models\PressRelease;
use App\Models\seo;
use App\Models\VideoGallery;
use App\Models\VisionMission;
use Illuminate\Http\Request;

class Frontend extends Controller
{
    public function __construct()
    {
        // Share the contact information with all views
        $contact = Contact::first();
        view()->share('contact', $contact);
    }

    public function home()
    {
        $seo = seo::where('page_name', 'home')->first();
        $about = About::first();
        $achievements = Achievement::orderby('created_at', 'desc')->take(4)->get();
        $news = PressRelease::orderby('created_at', 'desc')->take(3)->get();
        $policies = Policy::orderby('created_at', 'desc')->take(4)->get();
        $futureplans = FuturePlan::orderby('created_at', 'desc')->get();
        $videos = VideoGallery::orderby('created_at', 'desc')->take(3)->get();
        return view('home.home', compact('seo', 'about', 'achievements', 'news', 'policies', 'futureplans', 'videos'));
    }

    public function about()
    {
        $item = About::first();
        return view('pages.about', compact('item'));
    }

    public function visionMission()
    {
        $item = VisionMission::first();
        return view('pages.vision-mission', compact('item'));
    }

    public function achievement()
    {
        $seo = seo::where('page_name', 'achievement')->first();
        $categories = AchievementCategory::with('achievements')->get();
        return view('pages.achievement', compact('seo', 'categories'));
    }

    public function policy()
    {
        $seo = seo::where('page_name', 'policy')->first();
        $categories = PolicyCategory::with('policies')->get();
        return view('pages.policy', compact('seo', 'categories'));
    }

    public function blogList()
    {
        $seo = seo::where('page_name', 'blog')->first();
        $blogs = Blog::orderby('created_at', 'desc')->get();
        return view('pages.blog-list', compact('seo', 'blogs'));
    }

    public function blogDetail($slug)
    {
        $blog = Blog::where('slug', $slug)->firstOrFail();
        return view('pages.blog-detail', compact('blog'));
    }

    public function albums()
    {
        $seo = seo::where('page_name', 'image')->first();
        $albums = Album::orderby('created_at', 'desc')->get();
        return view('pages.album', compact('seo', 'albums'));
    }

    public function photos($id)
    {
        $album = Album::findOrFail($id);
        $photos = ImageGallery::where('album_id', $id)->orderby('created_at', 'desc')->get();
        return view('pages.photo', compact('album', 'photos'));
    }

    public function videos()
    {
        $seo = seo::where('page_name', 'video')->first();
        $videos = VideoGallery::orderby('created_at', 'desc')->get();
        return view('pages.video', compact('seo', 'videos'));
    }

    public function pressRelease()
    {
        $seo = seo::where('page_name', 'news')->first();
        $news = PressRelease::orderby('created_at', 'desc')->get();
        return view('pages.press-release', compact('seo', 'news'));
    }

    public function contact()
    {
        $seo = seo::where('page_name', 'contact')->first();
        return view('pages.contact', compact('seo'));
    }
}
