<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\HomeBanner;
use App\Models\AboutUs;
use App\Models\Chatbot;
use App\Models\Experience;
use App\Models\Project;

class FrontendController extends Controller
{
    public function index(){
        $banners  = HomeBanner::latest()->first();
        $about    = AboutUs::latest()->first();
        $chatbots = Chatbot::where('status', 1)
                           ->orderBy('sort_order', 'ASC')
                           ->get();
        $experiences = Experience::latest()->get();
        
        // Fetching all active projects with their associated technologies
        $projects = Project::with('technologies')
                           ->where('status', 'active')
                           ->orWhere('status', 'Active')
                           ->latest()
                           ->get();

        // Pre-mapped for safe @json() usage in Blade (no arrow functions in template)
        $chatbotsJson = $chatbots->map(function ($q) {
            return [
                'id'          => $q->id,
                'question'    => $q->question,
                'answer'      => $q->answer,
                'action_text' => $q->action_text,
                'action_url'  => $q->action_url,
            ];
        })->values();

        return view('frontend.index', compact('banners', 'about', 'chatbots', 'chatbotsJson', 'experiences', 'projects'));
    }
}
