<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Artisan;
use App\Services\CourseNotificationService;
use App\Http\Requests\Learning\CourseNotificationStore;

class CourseNotificationController extends Controller
{

    private $courseNotificationService;

    public function __construct(CourseNotificationService $courseNotificationService)
    {
        $this->courseNotificationService = $courseNotificationService;
        $this->middleware('auth:sanctum');
    }

    public function index()
    {
        if (!auth()->user()->hasRole('admin')) {
            return response()->json(['message' => 'You have no rights to do this'], 401);
        }

        $course_notifications = $this->courseNotificationService->getAll();

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

    public function store(CourseNotificationStore $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);
        }

        $course_notification = $this->courseNotificationService->save($request->validated());

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

    public function show($id)
    {
        $course_notification = $this->courseNotificationService->get($id);

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

    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->courseNotificationService->delete($id);
        return response()->json(null, 204);
    }

    public function testCommand()
    {
        $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);
        }

        Artisan::call('send:lp-notifications');
        return 0;
    }
}
