<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Domain\Notifications\Models\PanelNotification;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

/**
 * Hem admin hem customer paneli için ortak.
 */
class NotificationController extends Controller
{
    public function index(Request $request): Response
    {
        $user = $request->user();
        $notifications = PanelNotification::where('user_id', $user->id)
            ->where(fn ($q) => $q->whereNull('expires_at')->orWhere('expires_at', '>', now()))
            ->latest()
            ->paginate(20);

        return Inertia::render('Notifications/Index', [
            'notifications' => $notifications->through(fn ($n) => $this->present($n)),
            'unread_count' => PanelNotification::where('user_id', $user->id)->whereNull('read_at')->count(),
        ]);
    }

    /**
     * Header'daki dropdown için — son 10, JSON.
     */
    public function recent(Request $request): JsonResponse
    {
        $user = $request->user();
        $recent = PanelNotification::where('user_id', $user->id)
            ->where(fn ($q) => $q->whereNull('expires_at')->orWhere('expires_at', '>', now()))
            ->latest()
            ->take(10)
            ->get()
            ->map(fn ($n) => $this->present($n));

        return response()->json([
            'notifications' => $recent,
            'unread_count' => PanelNotification::where('user_id', $user->id)->whereNull('read_at')->count(),
        ]);
    }

    public function markRead(Request $request, PanelNotification $notification): RedirectResponse
    {
        if ($notification->user_id !== $request->user()->id) {
            abort(403);
        }
        $notification->markAsRead();

        return back();
    }

    public function markAllRead(Request $request): RedirectResponse
    {
        PanelNotification::where('user_id', $request->user()->id)
            ->whereNull('read_at')
            ->update(['read_at' => now()]);

        return back()->with('success', 'Tüm bildirimler okundu olarak işaretlendi.');
    }

    public function destroy(Request $request, PanelNotification $notification): RedirectResponse
    {
        if ($notification->user_id !== $request->user()->id) {
            abort(403);
        }
        $notification->delete();

        return back();
    }

    public function destroyAll(Request $request): RedirectResponse
    {
        PanelNotification::where('user_id', $request->user()->id)->delete();

        return back()->with('success', 'Tüm bildirimler silindi.');
    }

    private function present(PanelNotification $n): array
    {
        return [
            'id' => $n->id,
            'category' => $n->category,
            'priority' => $n->priority,
            'title' => $n->title,
            'body' => $n->body,
            'icon' => $n->icon,
            'link' => $n->link,
            'read_at' => $n->read_at?->format('Y-m-d H:i'),
            'created_at' => $n->created_at->format('Y-m-d H:i'),
            'created_at_diff' => $n->created_at->diffForHumans(),
            'is_read' => $n->isRead(),
        ];
    }
}
