<?php

namespace App\Http\Controllers\Api\V1;

use App\Services\NotificationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

class NotificationController extends BaseController
{
    public function __construct(
        private NotificationService $notificationService
    ) {}

    /**
     * Get user notifications
     */
    public function index(Request $request): JsonResponse
    {
        try {
            $user = Auth::user();
            $filters = $request->only(['type', 'unread_only', 'recent_days']);
            $perPage = $request->get('per_page', 15);

            $notifications = $this->notificationService->getUserNotifications($user->id, $filters, $perPage);

            return $this->successResponse($notifications, 'Notifications retrieved successfully');

        } catch (\Exception $e) {
            return $this->serverErrorResponse('Failed to retrieve notifications: ' . $e->getMessage());
        }
    }

    /**
     * Get unread notifications count
     */
    public function getUnreadCount(): JsonResponse
    {
        try {
            $user = Auth::user();
            $count = $this->notificationService->getUnreadCount($user->id);

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

        } catch (\Exception $e) {
            return $this->serverErrorResponse('Failed to get unread count: ' . $e->getMessage());
        }
    }

    /**
     * Mark notification as read
     */
    public function markAsRead(Request $request, int $id): JsonResponse
    {
        try {
            $user = Auth::user();
            $success = $this->notificationService->markAsRead($id, $user->id);

            if (!$success) {
                return $this->notFoundResponse('Notification not found');
            }

            return $this->successResponse([], 'Notification marked as read successfully');

        } catch (\Exception $e) {
            return $this->serverErrorResponse('Failed to mark notification as read: ' . $e->getMessage());
        }
    }

    /**
     * Mark all notifications as read
     */
    public function markAllAsRead(): JsonResponse
    {
        try {
            $user = Auth::user();
            $markedCount = $this->notificationService->markAllAsRead($user->id);

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

        } catch (\Exception $e) {
            return $this->serverErrorResponse('Failed to mark all notifications as read: ' . $e->getMessage());
        }
    }

    /**
     * Delete notification
     */
    public function destroy(int $id): JsonResponse
    {
        try {
            $user = Auth::user();
            $success = $this->notificationService->deleteNotification($id, $user->id);

            if (!$success) {
                return $this->notFoundResponse('Notification not found');
            }

            return $this->successResponse([], 'Notification deleted successfully');

        } catch (\Exception $e) {
            return $this->serverErrorResponse('Failed to delete notification: ' . $e->getMessage());
        }
    }

    /**
     * Get notification statistics
     */
    public function getStatistics(): JsonResponse
    {
        try {
            $user = Auth::user();
            $stats = $this->notificationService->getNotificationStatistics($user->id);

            return $this->successResponse($stats, 'Notification statistics retrieved successfully');

        } catch (\Exception $e) {
            return $this->serverErrorResponse('Failed to get statistics: ' . $e->getMessage());
        }
    }
}