<?php

namespace App\Http\Controllers\Api\v2;

use App\Models\Notification;
use App\Models_v2\DatabaseNotification;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NotificationController extends BaseController
{
    //
    /**
     * @api {get} /list Lấy danh sách thông báo của user đang đăng nhập
     * @apiName list
     * @apiGroup notification
     * @apiParam {Number} limit Mặc định 10
     * @apiParam {Number} offset Mặc định 0
     * @apiDescription Api Lấy danh sách thông báo của user đang đăng nhập
     * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
     * @apiSuccess {String} message  Tin nhắn hệ thống.
     * @apiSuccess {String} data
     */
    public function getNotifications(Request $request)
    {
        $limit = $request->input('limit', 10);
        $offset = $request->input('offset', 0);


        $user = $this->user;

        $notifications = $user->notifications()
            ->limit($limit)
            ->offset($offset)
            ->get()->map(function ($item) {
                $data = $item->data;
                $readAt = $item->read_at;
                if (!empty($readAt)) {
                    $readAt = $item->read_at->format('d/m/Y H:i:s');
                }

                if (isset($data['body'])) {
                    if (!isset($data['text'])) {
                        $data['text'] = $data['body'];
                    }
                }

                return [
                    'id' => $item->id,
                    'read_at' => $readAt,
                    'created_at' => $item->created_at->format('d/m/Y H:i:s'),
                    'data' => $data,
                    'status' => $item->status,
                    'find_session_id' => $item->find_session_id
                ];
            });

        return response([
            'status' => 1,
            'data' => $notifications
        ]);
    }

    /**
     * @api {post} /update-status Cập nhật status
     * @apiName update-status
     * @apiGroup notification
     * @apiParam {Number} status 2 là bỏ qua. 1 là tạo hội thoại
     * @apiParam {String} id
     * @apiDescription Api Cập nhật status
     * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
     * @apiSuccess {String} message  Tin nhắn hệ thống.
     * @apiSuccess {String} data
     */
    public function updateStatusNotification(Request $request)
    {
        $status = $request->input('status');
        $id = $request->input('id');
        $idV1 = $request->input('id_v1');

        if (!empty($id)) {
            DatabaseNotification::find($id)
                ->forceFill(['status' => $status])->save();
        }

        if (!empty($idV1)) {
            $notification = Notification::find($idV1);
            if ($notification) {
                $payload = json_decode($notification->payload, true);
                $payload['status'] = $status;
                $notification->payload = json_encode($payload);
                $notification->save();

                if (isset($payload['id'])) {
                    DatabaseNotification::query()
                        ->where('find_session_id', $payload['id'])
                        ->update([
                            'status' => $status
                        ]);
                }
            }
        }

        return response([
            'status' => 1,
            'message' => 'Thành công'
        ]);
    }

    /**
     * @api {get} /count-unread-notification Đếm thông báo chưa đọc
     * @apiName count-unread-notification
     * @apiGroup notification
     * @apiDescription Api Đếm thông báo chưa đọc
     * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
     * @apiSuccess {String} message  Tin nhắn hệ thống.
     * @apiSuccess {String} data
     */
    public function countUnreadNotification(Request $request)
    {
        $user = $this->user;
        $unreadNotifications = $user->unreadNotifications()
            ->count();

        return response([
            'status' => 1,
            'data' => $unreadNotifications
        ]);
    }

    /**
     * @api {get} /mark-read-notification Đánh dấu thông báo là đã đọc
     * @apiName mark-read-notification
     * @apiGroup notification
     * @apiDescription Api Đánh dấu thông báo là đã đọc
     * @apiParam {String} id Nếu không gửi lên hoặc gửi rỗng lên có nghĩa đánh dấu tất cả là đã đọc
     * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
     * @apiSuccess {String} message  Tin nhắn hệ thống.
     * @apiSuccess {String} data
     */
    public function markReadNotification(Request $request)
    {
        $id = $request->input('id');
        $user = $this->user;
        if (empty($id)) {
            $user->unreadNotifications->markAsRead();
        } else {
            $user->unreadNotifications->where('id', $id)->markAsRead();
        }

        return response([
            'status' => 1,
            'message' => 'Thành công'
        ]);
    }
}
