<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class NotificationController extends Controller
{
    public function index(Request $request)
    {
        $user = auth()->user();
        abort_unless($user, 403);

        $notifications = $user->notifications()
            ->when($request->get('filter') === 'unread', fn ($q) => $q->whereNull('read_at'))
            ->latest()
            ->paginate(20)
            ->withQueryString();

        return view('notifications.index', [
            'notifications' => $notifications,
            'unreadCount' => $user->unreadNotifications()->count(),
            'filter' => $request->get('filter', 'all'),
        ]);
    }

    public function markAsRead(string $notification)
    {
        $user = auth()->user();
        abort_unless($user, 403);

        $item = $user->notifications()->where('id', $notification)->firstOrFail();
        if ($item->read_at === null) {
            $item->markAsRead();
        }

        if (request()->expectsJson()) {
            return response()->json(['success' => true]);
        }

        return redirect()->back()->with('success', 'Notification marked as read.');
    }

    public function markAllAsRead()
    {
        $user = auth()->user();
        abort_unless($user, 403);

        $user->unreadNotifications->markAsRead();

        if (request()->expectsJson()) {
            return response()->json(['success' => true]);
        }

        return redirect()->back()->with('success', 'All notifications marked as read.');
    }

    public function destroy(string $notification)
    {
        $user = auth()->user();
        abort_unless($user, 403);

        $item = $user->notifications()->where('id', $notification)->firstOrFail();
        $item->delete();

        if (request()->expectsJson()) {
            return response()->json(['success' => true]);
        }

        return redirect()->back()->with('success', 'Notification deleted.');
    }

    public function clearAll()
    {
        $user = auth()->user();
        abort_unless($user, 403);

        $user->notifications()->delete();

        if (request()->expectsJson()) {
            return response()->json(['success' => true]);
        }

        return redirect()->back()->with('success', 'All notifications cleared.');
    }

    public function unreadCount()
    {
        $user = auth()->user();
        abort_unless($user, 403);

        return response()->json([
            'success' => true,
            'count' => $user->unreadNotifications()->count(),
        ]);
    }

    public function updateSettings(Request $request)
    {
        $request->validate([
            'browser_notifications' => 'nullable|boolean',
            'email_notifications' => 'nullable|boolean',
            'sound_notifications' => 'nullable|boolean',
        ]);

        session([
            'notification_settings' => [
                'browser_notifications' => (bool) $request->boolean('browser_notifications'),
                'email_notifications' => (bool) $request->boolean('email_notifications'),
                'sound_notifications' => (bool) $request->boolean('sound_notifications'),
            ],
        ]);

        return redirect()->back()->with('success', 'Notification settings updated.');
    }
}
