<?php

namespace App\Http\Controllers;

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

class AnnouncementController extends Controller
{
    public function store(Request $request)
    {
        $validated = $request->validate([
            'title'    => 'required|string|max:150',
            'content'  => 'required|string|max:1000',
            'priority' => 'required|in:normal,penting,mendesak',
        ]);

        Announcement::create([
            ...$validated,
            'user_id' => auth()->id(),
        ]);

        return back()->with('success', 'Pengumuman berhasil ditambahkan!');
    }

    public function togglePin(int $id)
    {
        $ann = Announcement::findOrFail($id);
        $ann->update(['is_pinned' => ! $ann->is_pinned]);

        return back()->with('success', $ann->is_pinned ? 'Pengumuman disematkan.' : 'Pin dilepas.');
    }

    public function destroy(int $id)
    {
        Announcement::findOrFail($id)->delete();

        return back()->with('success', 'Pengumuman dihapus.');
    }
}
