<?php

namespace App\Http\Controllers;

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

class AnnouncementController extends Controller
{
    public function create(Request $request)
    {
        try {
            $this->validate($request, [
                'title' => 'required',
                'content' => 'required'
            ]);

            Announcement::create($request->all());

            return response()->json(['message' => 'Announcement created successfully'], 201);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Failed to create announcement'], 500);
        }
    }

    public function update(Request $request, $id)
    {
        try {
            $this->validate($request, [
                'title' => 'required',
                'content' => 'required'
            ]);

            $announcement = Announcement::find($id);
            $announcement->update($request->all());

            return response()->json(['message' => 'Announcement updated successfully'], 200);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Failed to update announcement'], 500);
        }
    }

    public function delete($id)
    {
        try {
            $announcement = Announcement::find($id);
            $announcement->delete();

            return response()->json(['message' => 'Announcement deleted successfully'], 200);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Failed to delete announcement'], 500);
        }
    }

    public function get()
    {
        try {
            $announcements = Announcement::all();

            return response()->json($announcements, 200);
        } catch (\Exception $e) {
            return response()->json(['message' => 'Failed to get announcements'], 500);
        }
    }
}
