<?php

namespace App\Http\Controllers;

use App\Models\ChaosEvents;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class EventController extends Controller
{
    public static function eventsPaginated(){
        return ChaosEvents::paginate(10);
    }

    public function addEvent(Request $request) {
        if(Auth::check()){
            $name = $request->input('name');
            $shortname = $request->input('shortname');
            $from_date_internal = $request->input('from_date_internal');
            $to_date_internal = $request->input('to_date_internal');
            $from_date_visible = $request->input('from_date_visible');
            $to_date_visible = $request->input('to_date_visible');
            $active = $request->input('active');

            try {

                $event = new ChaosEvents();
                $event->name = $name;
                $event->shortname = $shortname;
                $event->from_date_internal = $from_date_internal;
                $event->to_date_internal = $to_date_internal;
                $event->from_date_visible = $from_date_visible;
                $event->to_date_visible = $to_date_visible;
                $event->active = $active;

                $event->save();
                return ['messageStatus' => 'success', 'errorMessage' => ''];
            } catch (\Illuminate\Database\QueryException $e) {
                return ['messageStatus' => 'failure', 'errorMessage' => __("controller_messages.EventController.error_occurred", ["error" => $e->getMessage()])];
            }
        }
        abort(404);
    }

    public function deleteEvent(Request $request) {
        if(Auth::check()){
            try {
                $event = ChaosEvents::find($request->input('id'));
                if ($event) {
                    $event->delete();
                    return ['messageStatus' => 'success', 'errorMessage' => ''];
                } else {
                    return ['messageStatus' => 'failure', 'errorMessage' => __("controller_messages.EventController.error_occurred", ["error" => "Event not found"])];
                }
            } catch (\Illuminate\Database\QueryException $e) {
                return ['messageStatus' => 'failure', 'errorMessage' => __("controller_messages.EventController.error_occurred", ["error" => $e->getMessage()])];
            }
        }
        abort(404);
    }

    public function setActivity(Request $request) {
        if(Auth::check()) {
            try {
                $event = ChaosEvents::find($request->input('id'));
                if ($event) {
                    $event->active = $request->input('active');
                    $event->save();
                    return ['messageStatus' => 'success', 'errorMessage' => ''];
                } else {
                    return ['messageStatus' => 'failure', 'errorMessage' => __("controller_messages.EventController.error_occurred", ["error" => "Event not found"])];
                }
            } catch (\Illuminate\Database\QueryException $e) {
                return ['messageStatus' => 'failure', 'errorMessage' => __("controller_messages.EventController.error_occurred", ["error" => $e->getMessage()])];
            }
        }
        abort(404);
    }
}
