<?php

namespace App\Http\Controllers;

use App\Models\Certification;
use App\Models\Client;
use App\Models\ContactMessage;
use App\Models\Download;
use App\Models\HeroSlide;
use App\Models\HomepageStat;
use App\Models\Page;
use App\Models\ProcessStep;
use App\Models\Project;
use App\Models\Service;
use App\Models\Value;
use Illuminate\Http\Request;

class FrontController extends Controller
{
    public function home()
    {
        $services = Service::where('status', true)->orderBy('sort_order')->get();
        $projects = Project::where('status', true)->with('category')->orderBy('sort_order')->limit(6)->get();
        $stats = HomepageStat::where('is_active', true)->orderBy('sort_order')->get();
        $values = Value::where('is_active', true)->orderBy('sort_order')->get();
        $clients = Client::where('status', true)->orderBy('sort_order')->get();
        $steps = ProcessStep::where('is_active', true)->orderBy('sort_order')->get();
        $heroSlides = HeroSlide::where('is_active', true)->orderBy('sort_order')->get();

        return view('frontend.home', compact('services', 'projects', 'stats', 'values', 'clients', 'steps', 'heroSlides'));
    }

    public function about()
    {
        $values = Value::where('is_active', true)->orderBy('sort_order')->get();
        $stats = HomepageStat::where('is_active', true)->orderBy('sort_order')->get();
        $journey = \App\Models\JourneyItem::where('is_active', true)->orderBy('sort_order')->get();

        return view('frontend.about', compact('values', 'stats', 'journey'));
    }

    public function services()
    {
        $services = Service::where('status', true)->orderBy('sort_order')->get();

        return view('frontend.services', compact('services'));
    }

    public function projects()
    {
        $projects = Project::where('status', true)->with('category')->orderBy('sort_order')->paginate(12);
        $categories = \App\Models\ProjectCategory::where('is_active', true)->whereHas('projects', fn($q) => $q->where('status', true))->get();

        return view('frontend.projects', compact('projects', 'categories'));
    }

    public function projectShow(string $slug)
    {
        $project = Project::where('status', true)->with(['category', 'images'])->where('slug', $slug)->firstOrFail();

        return view('frontend.project-detail', compact('project'));
    }

    public function clients()
    {
        $clients = Client::where('status', true)->orderBy('sort_order')->get();

        return view('frontend.clients', compact('clients'));
    }

    public function certifications()
    {
        $certifications = Certification::where('status', true)->orderBy('sort_order')->get();

        return view('frontend.certifications', compact('certifications'));
    }

    public function downloads()
    {
        $downloads = Download::where('status', true)->orderBy('sort_order')->get();

        return view('frontend.downloads', compact('downloads'));
    }

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

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

        ContactMessage::create($validated);

        return back()->with('success', 'Thank you. Your message has been sent successfully.');
    }

    public function page(string $slug)
    {
        $page = Page::where('slug', $slug)->where('status', true)->firstOrFail();

        return view('frontend.page', compact('page'));
    }

    public function privacy()
    {
        $page = Page::where('slug', 'privacy')->first();

        if ($page) {
            return view('frontend.page', compact('page'));
        }

        return view('frontend.privacy-static');
    }
}
