<?php

namespace App\Http\Controllers\Api\RT;

use App\Http\Controllers\Controller;
use App\Models\Pengumuman;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class PengumumanController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = Pengumuman::with('user:id,name');

        if ($request->has('kategori')) {
            $query->where('kategori', $request->kategori);
        }

        if ($request->has('is_active')) {
            $query->where('is_active', $request->boolean('is_active'));
        } else {
            $query->where('is_active', true);
        }

        $pengumuman = $query->orderBy('prioritas', 'desc')
                           ->orderBy('tanggal_mulai', 'desc')
                           ->paginate(20);

        return response()->json($pengumuman);
    }

    public function store(Request $request): JsonResponse
    {
        $validator = Validator::make($request->all(), [
            'judul' => 'required|string|max:255',
            'isi' => 'required|string',
            'kategori' => 'required|in:Umum,Keamanan,Kebersihan,Iuran,Acara,Lainnya',
            'prioritas' => 'required|in:Rendah,Sedang,Tinggi',
            'tanggal_mulai' => 'required|date',
            'tanggal_selesai' => 'nullable|date|after_or_equal:tanggal_mulai',
        ]);

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 422);
        }

        $pengumuman = Pengumuman::create([
            ...$request->all(),
            'user_id' => auth()->id()
        ]);

        return response()->json([
            'message' => 'Pengumuman berhasil dibuat',
            'data' => $pengumuman
        ], 201);
    }

    public function show(Pengumuman $pengumuman): JsonResponse
    {
        return response()->json([
            'data' => $pengumuman->load('user:id,name')
        ]);
    }

    public function update(Request $request, Pengumuman $pengumuman): JsonResponse
    {
        $validator = Validator::make($request->all(), [
            'judul' => 'required|string|max:255',
            'isi' => 'required|string',
            'kategori' => 'required|in:Umum,Keamanan,Kebersihan,Iuran,Acara,Lainnya',
            'prioritas' => 'required|in:Rendah,Sedang,Tinggi',
            'tanggal_mulai' => 'required|date',
            'tanggal_selesai' => 'nullable|date|after_or_equal:tanggal_mulai',
            'is_active' => 'required|boolean',
        ]);

        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 422);
        }

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

        return response()->json([
            'message' => 'Pengumuman berhasil diupdate',
            'data' => $pengumuman
        ]);
    }

    public function destroy(Pengumuman $pengumuman): JsonResponse
    {
        $pengumuman->delete();

        return response()->json([
            'message' => 'Pengumuman berhasil dihapus'
        ]);
    }
}
