<?php

namespace App\Helpers;

use App\Models\NotificationDetails;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Carbon\Carbon;
use Ladumor\OneSignal\OneSignal;


class onesignalhelper
{
    public static function sendNotificationApi($playerId, $title, $message, $content_type, $imageUrl, $id)
    {
        // Retrieve configuration based on the specified app
        // $config = config("one-signal.apps.$app");

        $fields = [
            'include_player_ids' => [$playerId],
            'contents' => ['en' => $message],
            'headings' => ['en' => $title],
            'content_available' => true,
            // 'data' => [
            //     'content_type' => $content_type,
            //     'id' => $id,
            // ],
            'big_picture' => $imageUrl,

            // 'external_id' => [$playerId],
            // 'authorization' => env('ONE_SIGNAL_AUTHORIZE')
        ];

        $result = OneSignal::sendPush($fields, null ,env('ONE_SIGNAL_APP_ID'));
        Log::info("Notification sending To " . $playerId);
        Log::info($result);
    }


    public static function restSendNotificationApi($playerId, $title, $message, $content_type, $imageUrl, $id)
    {
        // Retrieve configuration based on the specified app
        // $config = config("one-signal.apps.$app");

        $fields = [
            'include_player_ids' => [$playerId],
            'contents' => ['en' => $message],
            'headings' => ['en' => $title],
            'content_available' => true,
            'data' => [
                'content_type' => $content_type,
                'id' => $id,
            ],
            'big_picture' => $imageUrl,
            // 'app_id' => env('ONE_SIGNAL_APP_ID_2'),
            // 'authorization' => env('ONE_SIGNAL_AUTHORIZE_2')
        ];

        $result = OneSignal::sendPush($fields, null, env('ONE_SIGNAL_APP_ID_2'));
    }


    public static function StoreNotificationDetails($customerId, $type, $description, $imagePath)
    {

        try {
            $currentUtcTime = Carbon::now();

            $newRecord = NotificationDetails::create([
                'principal_xid' => $customerId,
                'type' => $type,
                'date_added' => $currentUtcTime,
                'description' => $description,
                'image' => $imagePath,

            ]);
            Log::info('success'. $newRecord);
        } catch (\Exception $e) {
            Log::info('error'. $e);
            // Return response in case of error
            return response()->json([
                'status' => 500,
                'message' => "Something Went Wrong " . $e->getMessage(),
                'result' => null,
                'error_code' => 1
            ]);
        }
    }
}
