<?php

namespace App\Http\Controllers\Api\V1\Client;

use App\Http\Controllers\Controller;
use App\Services\ModalIndexQuey;
use App\Traits\Formatter;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    use Formatter;

    /**
     * @OA\Get(
     *     path="/api/v1/client/notifications",
     *     summary="Get authenticated user's notifications",
     *     tags={"Notifications"},
     *     security={{"bearerAuth":{}}},
     *
     *     @OA\Response(
     *         response=200,
     *         description="User notifications fetched successfully",
     *
     *         @OA\JsonContent(
     *
     *             @OA\Property(property="statusCode", type="integer", example=200),
     *             @OA\Property(property="data", type="array", @OA\Items(type="object"))
     *         )
     *     ),
     *
     *     @OA\Response(
     *         response=401,
     *         description="Unauthorized",
     *
     *         @OA\JsonContent(
     *
     *             @OA\Property(property="statusCode", type="integer", example=401),
     *             @OA\Property(property="message", type="string", example="Unauthorized")
     *         )
     *     )
     * )
     */
    public function index()
    {
        /**
         * @var \App\Models\User $user
         */
        $user = auth('client')->user();

        $query = $user->notifications();

        $unread = request('unread');

        if ($unread) {
            $query->where('read_at', null);
        }

        $notifications = ModalIndexQuey::get($query);

        if ($unread) {
            return $this->withSuccess(['unread_count' => $query->count(), 'notifications' => $notifications]);
        }

        return $this->withSuccess($notifications);
    }

    /**
     * @OA\Delete(
     *     path="/api/v1/client/notifications/{id}",
     *     summary="Delete a specific notification",
     *     tags={"Notifications"},
     *     security={{"bearerAuth":{}}},
     *
     *     @OA\Parameter(
     *         name="id",
     *         in="path",
     *         description="Notification ID",
     *         required=true,
     *
     *         @OA\Schema(type="string")
     *     ),
     *
     *     @OA\Response(
     *         response=200,
     *         description="Notification deleted successfully",
     *
     *         @OA\JsonContent(
     *
     *             @OA\Property(property="statusCode", type="integer", example=200),
     *             @OA\Property(property="message", type="string", example="Notification deleted successfully")
     *         )
     *     ),
     *
     *     @OA\Response(
     *         response=404,
     *         description="Notification not found",
     *
     *         @OA\JsonContent(
     *
     *             @OA\Property(property="statusCode", type="integer", example=404),
     *             @OA\Property(property="message", type="string", example="Notification not found")
     *         )
     *     )
     * )
     */
    public function destroy($id)
    {
        /**
         * @var \App\Models\User $user
         */
        $user = auth('client')->user();

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

        $notification->delete();

        return response()->json([
            'statusCode' => 200,
            'message' => 'Notification deleted successfully',
        ]);
    }

    /**
     * @OA\Post(
     *     path="/api/v1/client/notifications",
     *     summary="read all notifications for the authenticated user",
     *     tags={"Notifications"},
     *     security={{"bearerAuth":{}}},
     *
     *     @OA\Response(
     *         response=200,
     *         description="All notifications deleted successfully",
     *
     *         @OA\JsonContent(
     *
     *             @OA\Property(property="statusCode", type="integer", example=200),
     *             @OA\Property(property="message", type="string", example="All notifications deleted successfully")
     *         )
     *     ),
     *
     *     @OA\Response(
     *         response=401,
     *         description="Unauthorized",
     *
     *         @OA\JsonContent(
     *
     *             @OA\Property(property="statusCode", type="integer", example=401),
     *             @OA\Property(property="message", type="string", example="Unauthorized")
     *         )
     *     )
     * )
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'ids' => 'required|array',
            'ids.*' => 'exists:notifications,id',
        ]);
        /**
         * @var \App\Models\User $user
         */
        $user = auth('client')->user();

        $user->notifications()->whereIn('id', $validated['ids'])->update(['read_at' => now()]);

        return response()->json([
            'statusCode' => 200,
        ]);
    }

    /**
     * @OA\Delete(
     *     path="/api/v1/client/notifications",
     *     summary="Delete all notifications for the authenticated user",
     *     tags={"Notifications"},
     *     security={{"bearerAuth":{}}},
     *
     *     @OA\Response(
     *         response=200,
     *         description="All notifications deleted successfully",
     *
     *         @OA\JsonContent(
     *
     *             @OA\Property(property="statusCode", type="integer", example=200),
     *             @OA\Property(property="message", type="string", example="All notifications deleted successfully")
     *         )
     *     ),
     *
     *     @OA\Response(
     *         response=401,
     *         description="Unauthorized",
     *
     *         @OA\JsonContent(
     *
     *             @OA\Property(property="statusCode", type="integer", example=401),
     *             @OA\Property(property="message", type="string", example="Unauthorized")
     *         )
     *     )
     * )
     */
    public function destroyAll()
    {
        /**
         * @var \App\Models\User $user
         */
        $user = auth('client')->user();

        $user->notifications()->delete();

        return response()->json([
            'statusCode' => 200,
            'message' => 'All notifications deleted successfully',
        ]);
    }
}
