<?php

namespace App\Services\Env;

use RuntimeException;

/**
 * Thrown by EnvFileEditor::write() when the content handed to it must not be
 * written — either a line that does not parse as env syntax, or content that
 * is empty and would erase every setting in the file.
 *
 * Never thrown for anything already written: write() checks before it touches
 * the file, never after — see its own docblock for why that order is the
 * whole point of this class.
 */
final class InvalidEnvContentException extends RuntimeException
{
    // Not $line: Exception already declares that property (the line the
    // exception was THROWN from), non-readonly — redeclaring it readonly
    // here is a fatal error, not merely a shadowing warning.
    private function __construct(string $message, public readonly ?int $invalidLine)
    {
        parent::__construct($message);
    }

    public static function atLine(int $line): self
    {
        return new self("Line {$line} is not blank, a comment, or KEY=value.", $line);
    }

    public static function empty(): self
    {
        return new self('Refusing to write an empty .env file.', null);
    }
}
