<?php

namespace App\Http\Controllers;

use App\Http\Traits\ResponseTrait;
use App\Http\Resources\NotificationResource;
use App\Models\Notification;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    use ResponseTrait;

    /**
     * Get user notifications
     */
    public function index(Request $request)
    {
        $user = $request->user();
        $perPage = $request->input('limit', 20);
        $perPage = min($perPage, 100); // Max 100 per page

        $query = Notification::where('user_id', $user->id)
            ->orderBy('created_at', 'desc');

        // Filter by read status
        if ($request->has('read')) {
            $read = filter_var($request->read, FILTER_VALIDATE_BOOLEAN);
            $query->where('read', $read);
        }

        // Filter by type
        if ($request->has('type')) {
            $query->ofType($request->type);
        }

        $notifications = $query->paginate($perPage);

        // Get unread count
        $unreadCount = Notification::where('user_id', $user->id)
            ->where('read', false)
            ->count();

        return $this->paginatedResponse(
            $notifications,
            'Notifications retrieved successfully',
            ['unread_count' => $unreadCount]
        );
    }

    /**
     * Mark notification as read
     */
    public function markAsRead(Request $request, $id)
    {
        $user = $request->user();

        $notification = Notification::where('id', $id)
            ->where('user_id', $user->id)
            ->first();

        if (!$notification) {
            return $this->notFoundResponse();
        }

        $notification->markAsRead();

        return response()->json([
            'code' => 200,
            'error' => false,
            'status' => 'success',
            'message' => 'Notification marked as read'
        ], 200);
    }

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

        $count = Notification::where('user_id', $user->id)
            ->where('read', false)
            ->update([
                'read' => true,
                'read_at' => now(),
            ]);

        return $this->successResponse([
            'marked_count' => $count,
        ], 'All notifications marked as read');
    }

    /**
     * Delete notification
     */
    public function destroy(Request $request, $id)
    {
        $user = $request->user();

        $notification = Notification::where('id', $id)
            ->where('user_id', $user->id)
            ->first();

        if (!$notification) {
            return $this->notFoundResponse();
        }

        $notification->delete();

        return response()->json([
            'code' => 200,
            'error' => false,
            'status' => 'success',
            'message' => 'Notification deleted successfully'
        ], 200);
    }

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

        $count = Notification::where('user_id', $user->id)
            ->where('read', false)
            ->count();

        return $this->successResponse([
            'count' => $count,
        ], 'Unread count retrieved successfully');
    }
}
