<?php

namespace App\Controllers\user;

use App\Models\UserModel;
use App\Models\BeritaModel;
use App\Models\GalleryModel;
use App\Models\KontakModel;
use App\Models\ProfileModel;
use App\Models\AgendaModel;
use App\Controllers\BaseController;


class HomeController extends BaseController
{
    public function index()
    {
        $userModel = new UserModel();

        $kontakModel = new KontakModel();
        $beritaModel = new BeritaModel();
        $galleryModel = new GalleryModel();
        $profileModel = new ProfileModel();
        $agendaModel = new AgendaModel();

        // Ambil data
        $user = $userModel->findAll();
        $profileData = $profileModel->first();
        $kontakData = $kontakModel->first();
        $carosal = $beritaModel->orderBy('id_berita', 'DESC')->limit(3)->find();
        $beritaBaru = $beritaModel->orderBy('id_berita', 'DESC')->first();
        $agendaBaru = $agendaModel->orderBy('id_agenda', 'DESC')->first();
        $beritaUmum = $beritaModel->where('kategori', 'Umum')->orderBy('id_berita', 'DESC')->limit(3)->find();
        $beritaLokal = $beritaModel->where('kategori', 'Lokal')->orderBy('id_berita', 'DESC')->limit(3)->find();
        $beritaDaerah = $beritaModel->where('kategori', 'Daerah')->orderBy('id_berita', 'DESC')->limit(3)->find();
        $beritaLain = $beritaModel->orderBy('id_berita', 'DESC')->limit(5)->find();
        $agendaLain = $agendaModel->orderBy('id_agenda', 'DESC')->limit(5)->find();
        $gallery = $galleryModel->orderBy('id_galeri', 'DESC')->limit(6)->find();

        // Tampilkan view dengan data profile dan user
        return view('user/home/index', [
            'profileData' => $profileData,
            'kontakData' => $kontakData,
            'carosal' => $carosal,
            'beritaBaru' => $beritaBaru,
            'agendaBaru' => $agendaBaru,
            'beritaUmum' => $beritaUmum,
            'beritaLokal' => $beritaLokal,
            'beritaDaerah' => $beritaDaerah,
            'beritaLain' => $beritaLain,
            'agendaLain' => $agendaLain,
            'gallery' => $gallery,
            'user' => $user,
        ]);
    }
}