<?php

namespace App\Livewire\Home;

use App\Models\Announcement;
use App\Models\News;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Beranda')]
class Index extends Component
{
    public function render()
    {
        $news = News::published()
            ->latest('published_at')
            ->take(3)
            ->get()
            ->map(function ($news) {
                $news->thumbnail = $news->getFirstMediaUrl('news') ?: 'https://images.pexels.com/photos/12199409/pexels-photo-12199409.jpeg';
                $news->formatted_date = $news->published_at?->translatedFormat('d F Y') ?: '-';
                $news->excerpt = str($news->content)->stripTags()->limit(100);

                return $news;
            });

        $announcements = Announcement::public()
            ->latest()
            ->take(3)
            ->get()
            ->map(function ($announcement) {
                $announcement->formatted_date = $announcement->created_at?->translatedFormat('d F Y') ?: '-';
                $announcement->excerpt = str($announcement->content)->stripTags()->limit(200);

                return $announcement;
            });

        return view('livewire.home.index', [
            'news' => $news,
            'announcements' => $announcements,
        ]);
    }
}
