<?php

namespace Modules\Linksehat\Http\Controllers\Api;

use App\Helpers\Helper;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Symfony\Component\HttpFoundation\Response;

class NotificationTokenController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index()
    {
        return view('linksehat::index');
    }

    /**
     * Show the form for creating a new resource.
     * @return Renderable
     */
    public function create()
    {
        return view('linksehat::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param Request $request
     * @return Renderable
     */
    public function store(Request $request)
    {
        $request->validate([
            'origin' => 'required|in:linksehat-app,linksehat-web,linksehat-ios,linkmedis-app,linkmedis-web,linkmedis-ios',
            'type' => 'required',
            'token' => 'required',
        ]);

        $user = auth()->user();
        $user->notificationTokens()->updateOrCreate([
            'type' => $request->type,
            'token' => $request->token
        ], [
            'origin' => $request->origin,
            'type' => $request->type,
            'token' => $request->token,
            'status' => 'active'
        ]);

        return  Helper::responseJson(data: ['tokens' => $user->notificationTokens], message: 'Token Berhasil Ditambah');
    }

    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {
        return view('linksehat::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit($id)
    {
        return view('linksehat::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy($id)
    {
        $user = auth()->user();
        $notificationToken = $user->notificationTokens()->findOrFail($id);

        if ($notificationToken->delete())
        {
            return Helper::responseJson(data: ['tokens' => $user->notificationTokens], message: 'Token Berhasil Dihapus');
        }

        return Helper::responseJson(data: [], message: 'Something Went Wrong', statusCode: Response::HTTP_NOT_MODIFIED);
    }
}
