<?php

namespace App\Http\Controllers\Admin;

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

class NotificationController extends Controller
{
    protected $notificationService;

    public function __construct(NotificationService $notificationService)
    {
        $this->notificationService = $notificationService;
    }

    public function index(Request $request)
    {
        $query = Notification::with('user');

        if ($request->filled('type')) {
            $query->where('type', $request->type);
        }

        $notifications = $query->latest()->paginate(50);
        $types = Notification::distinct()->pluck('type');

        return view('admin.notifications.index', compact('notifications', 'types'));
    }

    public function create()
    {
        $users = User::where('is_active', true)->orderBy('name')->get();
        return view('admin.notifications.create', compact('users'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'message' => 'required|string|max:10000',
            'type' => 'required|string|max:100',
            'is_html' => 'nullable|boolean',
            'target' => 'required|in:all,role,specific',
            'role' => 'required_if:target,role|nullable|string',
            'user_ids' => 'required_if:target,specific|nullable|array',
            'action_url' => 'nullable|string|max:255'
        ]);

        $data = [
            'type' => $request->type,
            'title' => $request->title,
            'message' => $request->message,
            'is_html' => $request->boolean('is_html'),
            'action_url' => $request->action_url
        ];

        $count = 0;

        if ($request->target === 'all') {
            $users = User::where('is_active', true)->get();
        } elseif ($request->target === 'role') {
            $users = User::where('is_active', true)->where('role', $request->role)->get();
        } else {
            $users = User::whereIn('id', $request->user_ids)->get();
        }

        foreach ($users as $user) {
            Notification::create(array_merge($data, ['user_id' => $user->id]));
            $count++;
        }

        return redirect()->route('admin.notifications.index')
            ->with('success', "{$count} notification(s) envoyée(s) avec succès !");
    }

    public function destroy(Notification $notification)
    {
        $notification->delete();
        return redirect()->back()->with('success', 'Notification supprimée');
    }

    public function deleteAll(Request $request)
    {
        $query = Notification::query();
        
        if ($request->filled('type')) {
            $query->where('type', $request->type);
        }
        
        if ($request->boolean('read_only')) {
            $query->where('is_read', true);
        }

        $count = $query->count();
        $query->delete();

        return redirect()->back()->with('success', "{$count} notifications supprimées");
    }
}
