<?php

declare(strict_types=1);

namespace Plugin\tnt_stabila_shop\src\Models;

use JsonException;
use Plugin\tnt_stabila_shop\src\Abstracts\AbstractModel;

class Events extends AbstractModel
{
    public static function getTable(): string
    {
        return 'stabila_shop_events';
    }

    /**
     * @return array{
     *     id: 'int',
     *     exceptionType: 'string',
     *     sdkVersion: 'string',
     *     extra: 'string',
     *     filePath: 'string',
     *     functionName: 'string',
     *     template: 'string',
     *     shopVersion: 'string',
     *     severity: 'string',
     *     message: 'string',
     *     varDump: 'string',
     *     createDate: 'datetime'
     * }
     */
    public static function getSchema(): array
    {
        return [
            'id'            => 'int',
            'exceptionType' => 'string',
            'sdkVersion'    => 'string',
            'extra'         => 'string',
            'filePath'      => 'string',
            'functionName'  => 'string',
            'template'      => 'string',
            'shopVersion'   => 'string',
            'severity'      => 'string',
            'message'       => 'string',
            'varDump'       => 'string',
            'createDate'    => 'datetime',
        ];
    }

    /**
     * @param object $data
     *
     * @return object{id: int, exceptionType: string, sdkVersion: string, filePath: string, functionName: string,
     *      template: string, shopVersion: string, severity: object{badgeType: string, value: string},
     *      message: string, varDump: string, hash: string, createDate: \DateTime, reportLink: string,
     *      reportMaxDate: string, totalExecTime: float, shopLanguage: string, shopCurrency: string,
     *      cartCount: int, cartItems: string, deliveryCountry: string, invoiceCountry: string,
     *      taxRates: string[], userBrowser: string, userID: int}
     */
    public static function transformData(object $data): object
    {
        $data = parent::transformData($data);
        /** @var object{id: int, exceptionType: string, sdkVersion: string, extra: string, filePath: string, functionName: string, template: string, shopVersion: string, severity: string, message: string, varDump: string, hash: string, createDate: \DateTime} $data */
        try {
            $extra = \json_decode($data->extra, true, 512, JSON_THROW_ON_ERROR);
        } catch (JsonException) {
            $extra = [];
        }
        if (\is_array($extra) === false) {
            $extra = [];
        }
        unset($data->extra);

        $result = (object)\array_merge((array)$data, [
            'reportLink'      => \is_string($extra['reportLink'] ?? null) ? $extra['reportLink'] : '',
            'reportMaxDate'   => \is_string($extra['reportMaxDate'] ?? null) ? $extra['reportMaxDate'] : '',
            'totalExecTime'   => \is_numeric($extra['totalExecTime'] ?? null) ? (float)$extra['totalExecTime'] : 0.0,
            'shopLanguage'    => \is_string($extra['shopLanguage'] ?? null) ? $extra['shopLanguage'] : '',
            'shopCurrency'    => \is_string($extra['shopCurrency'] ?? null) ? $extra['shopCurrency'] : '',
            'cartCount'       => \is_numeric($extra['cartCount'] ?? null) ? (int)$extra['cartCount'] : 0,
            'cartItems'       => \is_string($extra['cartItems'] ?? null) ? $extra['cartItems'] : '',
            'deliveryCountry' => \is_string($extra['deliveryCountry'] ?? null) ? $extra['deliveryCountry'] : '',
            'invoiceCountry'  => \is_string($extra['invoiceCountry'] ?? null) ? $extra['invoiceCountry'] : '',
            'taxRates'        => \array_map(
                static fn (string $taxRate): string => \trim($taxRate),
                \explode(',', \is_string($extra['taxRates'] ?? null) ? $extra['taxRates'] : '')
            ),
            'userBrowser'     => \is_string($extra['userBrowser'] ?? null) ? $extra['userBrowser'] : '',
            'userID'          => \is_numeric($extra['userID'] ?? null) ? (int)$extra['userID'] : 0,
            'severity'        => (object)[
                'badgeType' => match ($data->severity) {
                    'debug'          => 'light',
                    'info'           => 'info',
                    'warning'        => 'warning',
                    'error', 'fatal' => 'error',
                    default          => 'dark',
                },
                'value'     => $data->severity,
            ],
        ]);

        /** @var \stdClass&object{id: int, exceptionType: string, sdkVersion: string, filePath: string, functionName: string, template: string, shopVersion: string, severity: object{badgeType: string, value: string}, message: string, varDump: string, hash: string, createDate: \DateTime, reportLink: string, reportMaxDate: string, totalExecTime: float, shopLanguage: string, shopCurrency: string, cartCount: int, cartItems: string, deliveryCountry: string, invoiceCountry: string, taxRates: string[], userBrowser: string, userID: int} $result */
        return $result;
    }
}
