<?php

namespace App\Http\Controllers;

use App\Models\Notification;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class AdminNotificationController extends Controller
{
    public function index()
    {
        $notifications = Notification::with('user')->latest()->get();

        [$absensiNotifications, $adminNotifications] = $notifications->partition(function ($notif) {
            $title = Str::lower($notif->title ?? '');
            $message = Str::lower($notif->message ?? '');
            $absensiKeywords = ['absensi', 'clock in', 'clock out', 'jobdesk', 'job desk', 'catatan'];
            $shouldBeAbsensi = Str::contains($title, $absensiKeywords) || Str::contains($message, $absensiKeywords);

            if ($shouldBeAbsensi && $notif->type !== 'absensi') {
                $notif->type = 'absensi';
                $notif->save();
            }

            if (!empty($notif->type)) {
                return $notif->type === 'absensi';
            }

            return $shouldBeAbsensi;
        });

        return view('admin.notifications.index', [
            'absensiNotifications' => $absensiNotifications->values(),
            'adminNotifications' => $adminNotifications->values(),
        ]);
    }

    public function create()
    {
        $users = User::all();
        return view('admin.notifications.create', compact('users'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'user_id' => 'required',
            'title' => 'required|string|max:255',
            'message' => 'required|string',
            'image' => 'nullable|image|max:2048',
        ]);

        $imagePath = null;
        if ($request->hasFile('image')) {
            $imagePath = $request->file('image')->store('notifications', 'public');
        }

        $title = Str::lower($validated['title']);
        $message = Str::lower($validated['message']);
        $absensiKeywords = ['absensi', 'clock in', 'clock out', 'jobdesk', 'job desk', 'catatan'];
        $type = Str::contains($title, $absensiKeywords) || Str::contains($message, $absensiKeywords)
            ? 'absensi'
            : 'admin';

        if ($validated['user_id'] === 'all') {
            $users = User::all();
            foreach ($users as $user) {
                Notification::create([
                    'user_id' => $user->id,
                    'title' => $validated['title'],
                    'message' => $validated['message'],
                    'type' => $type,
                    'is_read' => false,
                    'image' => $imagePath,
                ]);
            }
        } else {
            Notification::create([
                'user_id' => $validated['user_id'],
                'title' => $validated['title'],
                'message' => $validated['message'],
                'type' => $type,
                'is_read' => false,
                'image' => $imagePath,
            ]);
        }

        return redirect()->route('admin.notifications.index')->with('success', 'Pemberitahuan berhasil dikirim.');
    }
}
