<?php

namespace App\Http\Controllers\Admin;

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

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

    /**
     * Create Alert.
     */
    public function store(Request $request): 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): RedirectResponse
    {
        Alert::where('id', $alert_id)->delete();

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

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