namespace ClaudeDo.Worker.Runner;

public sealed class LogWriter : IAsyncDisposable
{
    private readonly StreamWriter _writer;

    public LogWriter(string filePath)
    {
        var dir = Path.GetDirectoryName(filePath);
        if (dir is not null)
            Directory.CreateDirectory(dir);

        _writer = new StreamWriter(filePath, append: true) { AutoFlush = true };
    }

    public async Task WriteLineAsync(string line, CancellationToken ct = default)
    {
        ct.ThrowIfCancellationRequested();
        await _writer.WriteLineAsync(line.AsMemory(), ct);
    }

    public async ValueTask DisposeAsync()
    {
        await _writer.DisposeAsync();
    }
}
