<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class NotificationController extends Controller
{
    /**
     * Get all notifications for the authenticated user.
     */
    public function index()
    {
        try {
            $user = Auth::user();

            if (!$user) {
                return response()->json(['success' => false, 'message' => 'Unauthenticated'], 401);
            }

            $notifications = $user->notifications()->latest()->paginate(20);

            // Transform data to match frontend interface
            $data = $notifications->map(function ($notification) {
                return [
                    'id' => $notification->id,
                    'titulo' => $notification->data['title'] ?? 'Notificación',
                    'mensaje' => $notification->data['message'] ?? '',
                    'tipo' => $notification->data['type'] ?? 'info',
                    'leido' => !is_null($notification->read_at),
                    'created_at' => $notification->created_at,
                    'updated_at' => $notification->updated_at,
                ];
            });

            return response()->json([
                'success' => true,
                'data' => $data,
                'meta' => [
                    'current_page' => $notifications->currentPage(),
                    'last_page' => $notifications->lastPage(),
                    'total' => $notifications->total(),
                ]
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => true,
                'data' => [],
                'meta' => [
                    'current_page' => 1,
                    'last_page' => 1,
                    'total' => 0,
                ]
            ]);
        }
    }

    /**
     * Get unread notifications for the authenticated user.
     */
    public function unread()
    {
        try {
            $user = Auth::user();

            if (!$user) {
                return response()->json(['success' => false, 'message' => 'Unauthenticated'], 401);
            }

            // Verificar si existe la tabla de notificaciones
            try {
                $notifications = $user->unreadNotifications()->latest()->get();
            } catch (\Exception $e) {
                return response()->json([
                    'success' => true,
                    'data' => []
                ]);
            }

            $data = $notifications->map(function ($notification) {
                return [
                    'id' => $notification->id,
                    'titulo' => $notification->data['title'] ?? 'Notificación',
                    'mensaje' => $notification->data['message'] ?? '',
                    'tipo' => $notification->data['type'] ?? 'info',
                    'leido' => false,
                    'created_at' => $notification->created_at,
                    'updated_at' => $notification->updated_at,
                ];
            });

            return response()->json([
                'success' => true,
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => true,
                'data' => []
            ]);
        }
    }

    /**
     * Mark a specific notification as read.
     */
    public function markAsRead($id)
    {
        try {
            $user = Auth::user();

            if (!$user) {
                return response()->json(['success' => false, 'message' => 'Unauthenticated'], 401);
            }

            $notification = $user->notifications()->where('id', $id)->first();

            if ($notification) {
                $notification->markAsRead();
                return response()->json(['success' => true, 'message' => 'Notificación marcada como leída']);
            }

            return response()->json(['success' => false, 'message' => 'Notificación no encontrada'], 404);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => 'Error al marcar notificación como leída'], 500);
        }
    }

    /**
     * Mark all notifications as read.
     */
    public function markAllAsRead()
    {
        try {
            $user = Auth::user();

            if (!$user) {
                return response()->json(['success' => false, 'message' => 'Unauthenticated'], 401);
            }

            $user->unreadNotifications->markAsRead();

            return response()->json(['success' => true, 'message' => 'Todas las notificaciones marcadas como leídas']);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => 'Error al marcar notificaciones como leídas'], 500);
        }
    }

    /**
     * Delete a notification.
     */
    public function destroy($id)
    {
        try {
            $user = Auth::user();

            if (!$user) {
                return response()->json(['success' => false, 'message' => 'Unauthenticated'], 401);
            }

            $notification = $user->notifications()->where('id', $id)->first();

            if ($notification) {
                $notification->delete();
                return response()->json(['success' => true, 'message' => 'Notificación eliminada']);
            }

            return response()->json(['success' => false, 'message' => 'Notificación no encontrada'], 404);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => 'Error al eliminar notificación'], 500);
        }
    }
}
