<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Notification;
use App\Services\NotificationService;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    public function __construct(private NotificationService $notificationService) {}

    public function index()
    {
        $notifications = Notification::with('sender')->latest()->paginate(20);
        return view('admin.notifications.index', compact('notifications'));
    }

    public function create()
    {
        return view('admin.notifications.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'title'      => 'required|string|max:200',
            'body'       => 'required|string',
            'channel'    => 'required|in:push,in_app',
            'target'     => 'required|in:all,customers,vendors,specific',
            'target_ids' => 'required_if:target,specific|nullable|array',
        ]);

        $this->notificationService->broadcast($request->all());

        return redirect()->route('admin.notifications.index')->with('success', 'Notification broadcasted.');
    }
}
