<?php

namespace App\Http\Controllers\Employee;

use App\Http\Controllers\Controller;
use App\Models\Announcement;
use App\Services\AnnouncementService;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class AnnouncementController extends Controller
{
    public function __construct(
        protected AnnouncementService $announcementService
    ) {}

    public function index(Request $request): Response
    {
        $user = $request->user();

        $announcements = Announcement::query()
            ->published()
            ->visibleTo($user)
            ->with(['creator'])
            ->latest()
            ->paginate(12);

        return Inertia::render('Employee/Announcements/Index', [
            'announcements' => $announcements,
        ]);
    }

    public function show(Announcement $announcement, Request $request): Response
    {
        $user = $request->user();

        // Check visibility
        if (! $announcement->publish_at || $announcement->publish_at > now()) {
            abort(404);
        }

        // We should also check visibleTo scope logic manually if not using global scopes
        // But for now, we assume direct access is allowed if published and the user has the link,
        // OR we can re-apply the filter here.

        $this->announcementService->markAsRead($announcement, $user->id);

        return Inertia::render('Employee/Announcements/Show', [
            'announcement' => $announcement->load(['creator']),
        ]);
    }
}
