<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Notifications\GeneralNotification;
use App\Models\User;

class NotificationController extends Controller
{
    public function notifications(){
        $user = Auth::user();
        $notifications = $user->unreadNotifications;

        return response()->json([
            'status' => 'success',
            'message' => 'notification loaded',
            'notifications' => $notifications
        ], 200);
    }

    public function readNotifications(Request $request){
        $request->validate([
            'id' => 'required'
        ]);

        $notification = Auth::user()->notifications->where('id', $request->id)->first();
        if ($notification) {
            $notification->markAsRead();
        }
        
        $notifications = Auth::user()->unreadNotifications;
        return response()->json([
            'status' => 'success',
            'message' => 'notification read',
            'notifications' => $notifications
        ], 200);
    }


     /**
     * @param string $comma_separated_string e.g. "a,b, c"
     * @return array The values between the comma's in the given string, ignoring any white space between values/separators
     */
    function explode_ignoring_whitespace(string $comma_separated_string): array
    {
        // Cheeky way to explode only "a" and "b, c" from "a,b, c"
        return preg_split('/,(?=\S)/', $comma_separated_string, null, PREG_SPLIT_NO_EMPTY);
    }


    public function sendMail(Request $request){

        $request->validate([
            'title' => 'required',
            'content' => 'required',
            'mail_content' => 'required',
            'receiver' => 'nullable',
            'all' => 'nullable'
        ]);

        $mail_details = [
            'title' => $request->title,
            'content' => $request->content,
            'mail_content'=> $request->mail_content,
            'url' => '/'
        ];

        $mail_data = (object)$mail_details;
        if ($request->has('all') && $request->all == 'yes') {
            if (!empty($request->all)) {
                $users = User::all();
                foreach ($users as $user) {
                    $user->notify(new GeneralNotification($mail_data));
                }   
            }
        }

        if ($request->has('receiver') && $request->receiver != '') {            
            $email_list = $this->explode_ignoring_whitespace($request->receiver);
            $collect_list = collect($email_list);
            foreach ($collect_list->unique()->values()->all() as $my_email) {               
                $user = User::where('email', $my_email)->first();
                if ($user) {
                    $user->notify(new GeneralNotification($mail_data));
                }
            }
        }

        return response()->json([
            'status' => 'success',
            'message' => 'mail sent successfully'
        ], 200);

    }

}
