<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\DwNotification;
use App\Models\Client;
use App\Services\Notifications\NotificationCenterService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class NotificationCenterController extends Controller
{
    public function __construct(protected NotificationCenterService $notifications)
    {
    }

    public function index(Request $request): View
    {
        $query = DwNotification::query()->with('client')->visible()->latest();

        if ($request->filled('level')) {
            $query->where('level', $request->string('level'));
        }

        if ($request->filled('category')) {
            $query->where('category', $request->string('category'));
        }

        if ($request->filled('client_id')) {
            $query->where('client_id', (int) $request->client_id);
        }

        if ($request->get('status') === 'unread') {
            $query->unread();
        }

        if ($request->get('status') === 'read') {
            $query->whereNotNull('read_at');
        }

        return view('admin.notifications.index', [
            'notifications' => $query->paginate(25)->withQueryString(),
            'clients' => class_exists(Client::class) ? Client::query()->orderBy('name')->get(['id', 'name']) : collect(),
            'unreadCount' => $this->notifications->unreadCount(),
            'summary' => [
                'danger' => DwNotification::query()->visible()->whereIn('level', ['danger', 'critical', 'error'])->count(),
                'warning' => DwNotification::query()->visible()->where('level', 'warning')->count(),
                'unread' => $this->notifications->unreadCount(),
                'total' => DwNotification::query()->visible()->count(),
            ],
        ]);
    }

    public function show(DwNotification $notification): View
    {
        $notification->markRead();

        return view('admin.notifications.show', [
            'notification' => $notification->load('client'),
        ]);
    }

    public function dropdown(): View
    {
        return view('admin.notifications.dropdown', [
            'notifications' => $this->notifications->recent(8),
            'unreadCount' => $this->notifications->unreadCount(),
        ]);
    }

    public function build(Request $request): RedirectResponse
    {
        $result = $this->notifications->buildFromSystem(
            days: (int) $request->input('days', 7),
            clientId: $request->filled('client_id') ? (int) $request->client_id : null,
        );

        $total = array_sum($result);

        return back()->with('success', $total.' bildirim kontrol edildi / oluşturuldu.');
    }

    public function markRead(DwNotification $notification): RedirectResponse
    {
        $notification->markRead();

        return back()->with('success', 'Bildirim okundu olarak işaretlendi.');
    }

    public function markAllRead(Request $request): RedirectResponse
    {
        $count = $this->notifications->markAllRead($request->filled('client_id') ? (int) $request->client_id : null);

        return back()->with('success', $count.' bildirim okundu olarak işaretlendi.');
    }

    public function dismiss(DwNotification $notification): RedirectResponse
    {
        $notification->dismiss();

        return back()->with('success', 'Bildirim gizlendi.');
    }

    public function destroy(DwNotification $notification): RedirectResponse
    {
        $notification->delete();

        return back()->with('success', 'Bildirim silindi.');
    }
}
