<?php

namespace App\Http\Controllers;

use App\Models\Broadcast;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class BroadcastPopupController extends Controller
{
    public function latest()
    {
        // Ambil broadcast terakhir
        $last = Cache::remember('last_broadcast', 2, function () {
            return Broadcast::latest()->first();
        });

        if (!$last) {
            return response()->json([]);
        }

        return response()->json([
            'id' => $last->id,
            'title' => $last->title,
            'message' => $last->message,
            'time' => $last->created_at->diffForHumans(),
        ]);
    }
    public function list()
{
    $broadcasts = \App\Models\Broadcast::latest()->take(10)->get();

    if ($broadcasts->isEmpty()) {
        return response()->json([]);
    }

    return response()->json($broadcasts->map(function ($b) {
        return [
            'id' => $b->id,
            'title' => $b->title,
            'message' => $b->message,
            'time' => $b->created_at->diffForHumans(),
        ];
    }));
}

}
