<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Announcement;

class AnnouncementController extends Controller
{
    public function announcement() {
        $announcements = Announcement::latest()->get();
        return view('admin.Announcements.announcement', compact('announcements'));
    }

    public function create() {
        return view('admin.Announcements.create');
    }

    public function store(Request $request) {
        $request->validate([
            'title' => 'required|max:255',
            'content' => 'required'
        ]);

        Announcement::create([
            'title' => $request->title,
            'content' => $request->content
        ]);

        return redirect()->route('announcements.announcement')->with('success', 'Pengumuman berhasil dibuat.');
    }

    public function edit(Announcement $announcement) {
        return view('admin.Announcements.edit', compact('announcement'));
    }

    public function update(Request $request, Announcement $announcement) {
        $request->validate([
            'title' => 'required|max:255',
            'content' => 'required'
        ]);

        $announcement->update([
            'title' => $request->title,
            'content' => $request->content
        ]);

        return redirect()->route('announcements.announcement')->with('success', 'Pengumuman berhasil diperbarui.');
    }

    public function destroy(Announcement $announcement) {
        $announcement->delete();
        return redirect()->route('announcements.announcement')->with('success', 'Pengumuman berhasil dihapus.');
    }
}
