<?php

namespace App\Http\Controllers\api\v1;

use App\Http\Controllers\Controller;
use App\Models\NotificationSetting;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class NotificationController extends Controller
{

    private function validateAccesToken($access_token, $check_user = true)
    {
        $user = User::where(['user_access_token' => $access_token])->first();
        if($check_user){
            if (!$user) {
                http_response_code(401);
                echo json_encode([
                    'status' => (string) 0,
                    'message' => "Invalid access token",
                    'oData' => (object) array(),
                    'errors' => (object) [],
                ]);
                exit;
            }
    
            if ($user->status != 1) {
                http_response_code(401);
                echo json_encode([
                    'status' => (string) 0,
                    'message' => "User is not active",
                    'oData' => (object) array(),
                    'errors' => (object) [],
                ]);
                exit;
            }
        }

        return $user;
    }

    public function setPushNotification(Request $request){
        $status = "1";
        $message = "Push Notification sent successfully";
        $o_data = [];
        $errors = [];

        // Validate the request
        $validator = Validator::make($request->all(), [
            'access_token' => 'required|string',
            'title' => 'required|string|max:255',
            'body' => 'required|string|max:255',
            'user_id' => 'required|exists:users,id',
            'data' => 'required|json',
        ]);

        if ($validator->fails()) {
            if ($validator->fails()) {
                $status = (string) 0;
                $message = $validator->errors()->first();
                $errors = $validator->messages();
                return response()->json(['status' => $status, 'error' => $errors, 'message' => $message, 'oData' => (object)$o_data], 200); 
            }
        }

        $this->validateAccesToken($request->access_token);

        $user = User::find($request->user_id);

        $description = $request->body;
        $title = $request->title;
        $jsonData = json_decode($request->data, true);
         

        if (!empty($user->user_device_token) && get_user_notification_setting($user->id, NotificationSetting::MESSAGE)) {
               
            $res = send_single_notification(
                $user->user_device_token,
                [
                    "title" => $title ,
                    "body" => $description,
                    "icon" => 'myicon',
                    "sound" => 'default',
                    "click_action" => "EcomNotification"
                ],
                $jsonData
            );
            
        } else {
            $message = "User device token not found";
        }

        return response()->json(['status' => "", 'error' => $errors, 'message' => $message, 'oData' => (object)[]], 200);  
    }
}