<?php

namespace App\Http\Controllers;

use App\Models\SiteSetting;
use App\Models\HeroSlide;
use App\Models\Faq;
use App\Models\Division;
use App\Models\PortfolioItem;
use App\Models\TeamMember;
use App\Models\Testimonial;
use App\Models\Post;
use App\Models\Partner;

class FrontendController extends Controller
{
    private function getSettings()
    {
        return SiteSetting::pluck('setting_value', 'setting_key')->toArray();
    }

    public function home()
    {
        return view('pages.home', [
            'settings' => $this->getSettings(),
            'slides' => HeroSlide::where('is_active', 1)->orderBy('sort_order')->get(),
            'faqs' => Faq::where('is_active', 1)->orderBy('sort_order')->get(),
            'divisions' => Division::all(),
            'portfolio_items' => PortfolioItem::with('portfolioCategory')->latest()->take(6)->get(),
            'team_members' => TeamMember::all(),
            'testimonials' => Testimonial::latest()->get(),
            'partners' => Partner::where('is_active', true)->orderBy('sort_order')->get(),
        ]);
    }

    public function about()
    {
        return view('pages.about', [
            'settings' => $this->getSettings(),
            'team_members' => TeamMember::all(),
            'division_count' => Division::count(),
            'testimonials' => Testimonial::latest()->get(),
        ]);
    }

    public function services()
    {
        return view('pages.services', [
            'settings' => $this->getSettings(),
            'divisions' => Division::all(),
        ]);
    }

    public function contact()
    {
        return view('pages.contact', [
            'settings' => $this->getSettings(),
            'departments' => Division::all(),
        ]);
    }

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

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

        return redirect()->route('contact')->with('success', true);
    }

    public function division($id)
    {
        $division = Division::findOrFail($id);
        return view('pages.divisions.show', [
            'settings' => $this->getSettings(),
            'division' => $division,
        ]);
    }

    public function portfolio($id)
    {
        $item = PortfolioItem::findOrFail($id);
        return view('pages.portfolio.show', [
            'settings' => $this->getSettings(),
            'item' => $item,
        ]);
    }

    public function news()
    {
        $posts = Post::where('is_published', true)->orderBy('published_at', 'desc')->paginate(9);
        return view('pages.news', [
            'settings' => $this->getSettings(),
            'posts' => $posts,
        ]);
    }

    public function newsSingle($slug)
    {
        $post = Post::where('slug', $slug)->where('is_published', true)->firstOrFail();
        
        $recentPosts = Post::where('is_published', true)
                           ->where('id', '!=', $post->id)
                           ->orderBy('published_at', 'desc')
                           ->take(5)
                           ->get();

        return view('pages.news-single', [
            'settings' => $this->getSettings(),
            'post' => $post,
            'recentPosts' => $recentPosts,
        ]);
    }
}
