<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Alert;
use Illuminate\Http\Request;

class AlertController extends Controller
{
    /** 
     * Display alert index page
     */
    public function index(): \Illuminate\View\View
    {
        return view('admin.alert.index');
    }

    /**
     * Create Alert.
     */
    public function store(Request $request): \Illuminate\Http\RedirectResponse
    {
        $validated = $request->validate([
            'message' => 'required|string|max:255',
            'type' => 'required|integer|min:0|digits_between:0,1',
        ]);

        Alert::create([
            'text' => $request->input('message'),
            'type' => $request->input('type'),
        ]);

        cache()->forget('alerts');

        return redirect()->back();
    }

    /**
     * Delete Alert.
     */
    public function delete(int $alert_id): \Illuminate\Http\RedirectResponse
    {
        Alert::where('id', $alert_id)->forceDelete();

        cache()->forget('alerts');

        return redirect()->back();
    }
}
