<?php

namespace App\Http\Controllers;

use App\Models\Notification;
use App\Models\ForumNotification;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;

class NotificationController extends Controller
{
    // Get notifications for authenticated user
    public function index(Request $request): View
    {
        if (!auth()->check()) {
            return redirect()->route('login');
        }

        $notifications = Notification::where('user_id', auth()->id())
            ->latest()
            ->paginate(20);

        return view('notifications.index', compact('notifications'));
    }

    // Get unread notifications count (AJAX) - includes both regular and forum notifications
    public function unreadCount(): JsonResponse
    {
        if (!auth()->check()) {
            return response()->json(['count' => 0]);
        }

        $regularCount = Notification::where('user_id', auth()->id())
            ->where('is_read', false)
            ->count();
        
        $forumCount = ForumNotification::unreadCount(auth()->id());
        
        $totalCount = $regularCount + $forumCount;

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

    // Get latest notifications (AJAX - for dropdown)
    public function latest(): JsonResponse
    {
        if (!auth()->check()) {
            return response()->json(['notifications' => []]);
        }

        $notifications = Notification::where('user_id', auth()->id())
            ->latest()
            ->take(10)
            ->get()
            ->map(function ($notification) {
                return [
                    'id' => $notification->id,
                    'title' => $notification->title,
                    'message' => $notification->message,
                    'link' => $notification->link,
                    'is_read' => $notification->is_read,
                    'created_at' => $notification->created_at->diffForHumans(),
                ];
            });

        return response()->json(['notifications' => $notifications]);
    }

    // Mark notification as read
    public function markAsRead(Notification $notification): JsonResponse
    {
        if ($notification->user_id !== auth()->id()) {
            return response()->json(['error' => 'Unauthorized'], 403);
        }

        $notification->markAsRead();

        return response()->json(['success' => true]);
    }

    // Mark all notifications as read
    public function markAllAsRead(): JsonResponse
    {
        if (!auth()->check()) {
            return response()->json(['error' => 'Unauthorized'], 403);
        }

        Notification::where('user_id', auth()->id())
            ->where('is_read', false)
            ->update(['is_read' => true]);

        return response()->json(['success' => true]);
    }

    // Delete notification
    public function destroy(Notification $notification): JsonResponse
    {
        if ($notification->user_id !== auth()->id()) {
            return response()->json(['error' => 'Unauthorized'], 403);
        }

        $notification->delete();

        return response()->json(['success' => true]);
    }
}
