<?php

namespace App\Modules\Notifications\controllers\api\v1;

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\BaseController;

class DesktopNotificationsSettingsController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $query = DesktopNotificationSettings::select('*')->with('user');

        $query = $query->paginate(request()->get('limit', 20));

        $response = [
            'success'      => true,
            'data'         => $query->getCollection()->toArray(),
            'total'        => $query->total(),
            'current_page' => $query->currentPage(),
            'last_page'    => $query->lastPage(),
        ];

        return Response::json($response);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        // Retrieve the current notifications
        $notifications = DesktopNotificationSettings::currentUser();

        // Delete all current notifications
        $notifications->delete();

        // Cache the user id
        $user = Auth::user()->id;

        // Store the results in this variable
        $results = [];

        foreach (request()->get('settings', []) as $entry) {
            $notification = DesktopNotificationSettings::create([
                'user_id'      => $user,
                'notification' => $entry['notification'],
                'enabled'      => (isset($entry['enabled']) && $entry['enabled']) ? 1 : 0,
            ]);

            $results[] = $notification->toArray();
        }

        return Response::json(['success' => true]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function show($id)
    {
        $notificationSettings = DesktopNotificationSettings::find($id);

        if (! $notificationSettings) {
            return Response::json(['error' => 'Desktop notification settings not found.'], 404);
        }

        return Response::json($notificationSettings->toArray());
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function update($id)
    {
        $notificationSettings = DesktopNotificationSettings::find($id);

        if (! $notificationSettings) {
            return Response::json(['error' => 'Desktop notification settings not found.'], 404);
        }

        foreach (request()->all() as $key => $value) {
            $notificationSettings->$key = $value;
        }

        if ($notificationSettings->save()) {
            return Response::json($notificationSettings->toArray());
        } else {
            return Response::json(['errors' => $notificationSettings->errors->all()], 400);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $notificationSettings = DesktopNotificationSettings::find($id);

        if (! $notificationSettings) {
            return Response::json(['error' => 'Desktop notification settings not found.'], 404);
        }

        if ($notificationSettings->delete()) {
            return Response::json($notificationSettings->toArray());
        } else {
            return Response::json(['errors' => $notificationSettings->errors->all()], 400);
        }
    }
}
