<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Notice;
use Illuminate\Support\Facades\Auth;

class NoticeController extends Controller
{
    /**
     * Display a listing of the notices.
     */
    public function index()
    {
        $notices = Notice::orderBy('created_at', 'desc')->paginate(10);
        return view('notices.index', compact('notices'));
    }

    /**
     * Show the form for creating a new notice.
     */
    public function create()
    {
        return view('notices.create');
    }

    /**
     * Store a newly created notice.
     */
    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'content' => 'required|string',
            'priority' => 'required|in:low,medium,high',
            'expiry_date' => 'nullable|date|after:today'
        ]);

        Notice::create([
            'title' => $request->title,
            'content' => $request->content,
            'priority' => $request->priority,
            'expiry_date' => $request->expiry_date,
            'created_by' => Auth::id()
        ]);

        return redirect()->route('notices.index')
                         ->with('success', 'নোটিশ সফলভাবে তৈরি করা হয়েছে');
    }

    /**
     * Display the specified notice.
     */
    public function show($id)
    {
        $notice = Notice::findOrFail($id);
        return view('notices.show', compact('notice'));
    }

    /**
     * Show the form for editing the specified notice.
     */
    public function edit($id)
    {
        $notice = Notice::findOrFail($id);
        return view('notices.edit', compact('notice'));
    }

    /**
     * Update the specified notice.
     */
    public function update(Request $request, $id)
    {
        $notice = Notice::findOrFail($id);
        
        $request->validate([
            'title' => 'required|string|max:255',
            'content' => 'required|string',
            'priority' => 'required|in:low,medium,high',
            'expiry_date' => 'nullable|date|after:today'
        ]);

        $notice->update($request->all());

        return redirect()->route('notices.index')
                         ->with('success', 'নোটিশ আপডেট করা হয়েছে');
    }

    /**
     * Remove the specified notice.
     */
    public function destroy($id)
    {
        $notice = Notice::findOrFail($id);
        $notice->delete();

        return redirect()->route('notices.index')
                         ->with('success', 'নোটিশ মুছে ফেলা হয়েছে');
    }
}