<?php

namespace App\Http\Controllers;

use App\Services\SynonymService;
use App\Http\Requests\Learning\SynonymStore;

class SynonymController extends Controller
{

    private $synonymService;

    public function __construct(SynonymService $synonymService)
    {
        $this->synonymService = $synonymService;
        $this->middleware('auth:sanctum', [
            'except' => [
                'index',
            ]
        ]);
    }

    public function index()
    {
        $synonyms = $this->synonymService->getAll();

        return response()->json($synonyms, 201);
    }

    public function store(SynonymStore $request)
    {
        $isSuperAdmin = auth()->user()->hasRole('super_admin');
        $isAdmin = auth()->user()->hasRole('admin');
        $isOperator = auth()->user()->hasRole('operator');

        if (!$isSuperAdmin && !$isAdmin && !$isOperator) {
            return response()->json(['message' => 'You have no rights to do this'], 401);
        }

        $synonym = $this->synonymService->save($request->all());

        return response()->json($synonym, 201);
    }

    public function show($id)
    {
        $synonym = $this->synonymService->get($id);

        return response()->json($synonym);
    }

    public function destroy($id)
    {
        $isSuperAdmin = auth()->user()->hasRole('super_admin');
        $isAdmin = auth()->user()->hasRole('admin');
        $isOperator = auth()->user()->hasRole('operator');

        if (!$isSuperAdmin && !$isAdmin && !$isOperator) {
            return response()->json(['message' => 'You have no rights to do this'], 401);
        }

        $this->synonymService->delete($id);
        return response()->json(null, 204);
    }
}
