<?php
declare(strict_types=1);

namespace App\Controller;

class FaqsController extends AppController
{
    public function initialize(): void
    {
        parent::initialize();
       
        $this->Authentication->allowUnauthenticated(['index']);
    }

    
    public function index()
    {
        $faqsTable = $this->fetchTable('Faqs');

        $faqs = $faqsTable->find()
            ->order([
                'category' => 'ASC',
                'display_order' => 'ASC'
            ])
            ->all();

        // GROUP
        $groupedFaqs = [];

        foreach ($faqs as $faq) {
            $groupedFaqs[$faq->category][] = $faq;
        }

        // Pass grouped FAQs only
        $this->set(compact('groupedFaqs'));
    }
}