<?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;
use App\Modules\Notifications\NotificationSetting;

class NotificationSettingsControllerREST extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $query = NotificationSetting::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()
    {
        $notificationSettings = NotificationSetting::create(request()->all());

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

        return $notificationSettings;
    }

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

        if (! $notificationSettings) {
            return Response::json(['error' => '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 = NotificationSetting::find($id);

        if (! $notificationSettings) {
            return Response::json(['error' => '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 = NotificationSetting::find($id);

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

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