<?php

namespace App\Services\Loggers;

use App\TransactionOnlineLog;

class OnlineTransactionLoggerService
{
    public static function logs($request)
    {
        $log = TransactionOnlineLog::create([
            'bill_id' => $request->bill_id,
            'status' => $request->status,
            'payload' => $request->payload,
            'response' => $request->response,
        ]);

        return $log;
    }

    public static function logsAfterDBTransaction($bill_id, $status, $request, $response)
    {
        $formatToString = function ($data) {
            if (is_array($data) || is_object($data)) {
                return json_encode($data);
            }

            return $data;
        };

        $log = TransactionOnlineLog::create([
            'bill_id' => $formatToString($bill_id),
            'status' => $formatToString($status),
            'payload' => $formatToString($request),
            'response' => $formatToString($response),
        ]);

        return $log;
    }
}
