<?php

namespace AutoAdmin\Bundle\BrickBuilder\Exception;

use AutoAdmin\Bundle\BrickBuilder\Model\BrickInterface;

class BadSettingFormatException extends \Exception
{
    public function __construct(
        protected BrickInterface $brick,
        string $key,
        string $appendMsg = ''
    ) {
        $value = $this->brick->getSettings()[$key] ?? 'NO_SETTING_VAL';

        $message = sprintf(
            'Brick setting error, encountered value that is not a string. '.
            '[type: %s] [id: %s] [setting: %s] [type: %s] [value: %s]%s',
            $this->brick->getType(),
            $this->brick->getId(),
            $key,
            gettype($value),
            match (gettype($value)) {
                'object' => 'object',
                'array' => '['.implode(',', $value).']',
                default => $value,
            },
            $appendMsg ? (' '.$appendMsg) : ''
        );
        parent::__construct($message, 0, null);
    }
}
