<?php

namespace App\Livewire\Teacher;

use App\Models\Announcement;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

class PostAnnouncement extends Component
{
    public string $message = '';
    public string $target_group = 'All Grades';
    public string $priority = 'normal';
    public string $toast = '';

    public function post(): void
    {
        $this->validate(['message' => 'required|string']);

        Announcement::create([
            'author_id'    => Auth::id(),
            'message'      => $this->message,
            'target_group' => $this->target_group,
            'priority'     => $this->priority,
            'published_at' => now(),
        ]);

        $this->toast = 'Announcement published!';
        $this->reset('message', 'target_group', 'priority');
        $this->dispatch('announcement-posted');
    }

    public function render()
    {
        $recent = Announcement::where('author_id', Auth::id())
            ->latest('published_at')->take(3)->get();
        return view('livewire.teacher.post-announcement', compact('recent'));
    }
}
