<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class SubscriptionController extends Controller
{
    /** Subscribe / unsubscribe to the site channel. */
    public function toggle(Request $request)
    {
        $user = $request->user();
        $existing = $user->subscription()->first();

        if ($existing) {
            $existing->delete();
            $subscribed = false;
        } else {
            $user->subscription()->create([]);
            $subscribed = true;
        }

        return response()->json(['subscribed' => $subscribed]);
    }

    public function status(Request $request)
    {
        return response()->json([
            'subscribed' => $request->user()->subscription()->exists(),
        ]);
    }
}
