<?php

declare(strict_types=1);

namespace App\Framework\DateTime\Exceptions;

use App\Framework\Exception\ExceptionContext;
use App\Framework\Exception\FrameworkException;

class InvalidDateTimeException extends FrameworkException
{
    public function __construct(string $dateTime, ?string $format = null, ?\Throwable $previous = null)
    {
        $message = $format !== null
            ? sprintf('Konnte DateTime nicht aus "%s" mit Format "%s" erstellen', $dateTime, $format)
            : sprintf('Ungültiges DateTime-Format: "%s"', $dateTime);

        $context = ExceptionContext::forOperation('datetime.parse', 'datetime')
            ->withData([
                'input_datetime' => $dateTime,
                'format' => $format,
            ]);

        parent::__construct($message, $context, 0, $previous);
    }
}
