<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Announcement;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AnnouncementController extends Controller
{
    /**
     * Get active announcements for the user.
     */
    public function index(Request $request): JsonResponse
    {
        $announcements = Announcement::active()
            ->latest()
            ->get()
            ->map(function ($announcement) {
                return [
                    'id' => $announcement->id,
                    'title' => $announcement->title,
                    'message' => $announcement->message,
                    'type' => $announcement->type,
                    'created_at' => $announcement->created_at->toIso8601String(),
                ];
            });

        return response()->json([
            'announcements' => $announcements,
            'count' => $announcements->count(),
        ]);
    }
}
