<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Reservation;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    /**
     * Get user's notifications
     */
    public function index(Request $request): JsonResponse
    {
        $notifications = $request->user()
            ->notifications()
            ->latest()
            ->paginate(20);

        $reservationIds = $notifications->getCollection()
            ->pluck('data.reservation_id')
            ->filter()
            ->unique()
            ->values();

        $reservationStatuses = [];
        if ($reservationIds->isNotEmpty()) {
            $reservationStatuses = Reservation::whereIn('id', $reservationIds)
                ->pluck('status', 'id')
                ->map(fn ($s) => $s->value)
                ->toArray();
        }

        $notifications->getCollection()->transform(function ($notification) use ($reservationStatuses) {
            $data = $notification->data;
            if (isset($data['reservation_id']) && isset($reservationStatuses[$data['reservation_id']])) {
                $data['reservation_status'] = $reservationStatuses[$data['reservation_id']];
                $notification->data = $data;
            }
            return $notification;
        });

        return response()->json($notifications);
    }

    /**
     * Get unread notification count
     */
    public function unreadCount(Request $request): JsonResponse
    {
        $count = $request->user()->unreadNotifications()->count();

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

    /**
     * Mark notification as read
     */
    public function markAsRead(Request $request, string $id): JsonResponse
    {
        $notification = $request->user()
            ->notifications()
            ->where('id', $id)
            ->first();

        if (!$notification) {
            return response()->json([
                'message' => 'Bildirim bulunamadı.',
            ], 404);
        }

        $notification->markAsRead();

        return response()->json([
            'message' => 'Bildirim okundu olarak işaretlendi.',
        ]);
    }

    /**
     * Mark all notifications as read
     */
    public function markAllAsRead(Request $request): JsonResponse
    {
        $request->user()->unreadNotifications->markAsRead();

        return response()->json([
            'message' => 'Tüm bildirimler okundu olarak işaretlendi.',
        ]);
    }

    /**
     * Delete a notification
     */
    public function destroy(Request $request, string $id): JsonResponse
    {
        $notification = $request->user()
            ->notifications()
            ->where('id', $id)
            ->first();

        if (!$notification) {
            return response()->json([
                'message' => 'Bildirim bulunamadı.',
            ], 404);
        }

        $notification->delete();

        return response()->json([
            'message' => 'Bildirim silindi.',
        ]);
    }
}
