<?php

namespace App\Exceptions;

use App\Enums\PlatformEnum;
use App\Models\Article;
use Exception;
use Throwable;

class PublishException extends Exception
{
    public function __construct(
        private readonly Article $article,
        private readonly ?PlatformEnum $platform,
        ?Throwable $previous = null
    ) {
        $message = "Failed to publish article #$article->id";

        if ($this->platform) {
            $message .= "  to $platform->value";
        }

        if ($previous) {
            $message .= ": {$previous->getMessage()}";
        }

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

    public function getArticle(): Article
    {
        return $this->article;
    }

    public function getPlatform(): ?PlatformEnum
    {
        return $this->platform;
    }
}
