<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\Firebase\Request;
use App\Models\Firebase;

class FirebaseController extends Controller
{
    public function store(Request $request)
    {
        // get bearer token
        $token = $request->bearerToken();
        if ($token) {
            $user = getAuthUser();
            if ($user) {
                Firebase::where('token', $token)->update([
                    'user_id' => $user->id,
                ]);
            }
        } else {
            // store firebase token
            Firebase::create([
                'token' => $request->device_token,
                'device_id' => $request->device_id,
                'device_name' => $request->device_name,
                'device_type' => $request->device_type,
            ]);
        }

        return response()->json([
            'status' => 'success',
            'data' => md5($request->device_token . $request->device_id)
        ]);
    }
}
