using Microsoft.Extensions.Hosting;

namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Persistence;

/// <summary>
/// Hosted service that initializes the SQLite schema on startup.
/// Runs before the Akka actor system starts creating actors.
/// </summary>
public class SiteStorageInitializer : IHostedService
{
    private readonly SiteStorageService _storage;

    /// <summary>
    /// Initializes a new <see cref="SiteStorageInitializer"/>.
    /// </summary>
    /// <param name="storage">The site storage service whose schema is initialized on startup.</param>
    public SiteStorageInitializer(SiteStorageService storage)
    {
        _storage = storage;
    }

    /// <summary>
    /// Initializes the SQLite schema before the actor system starts.
    /// </summary>
    /// <param name="cancellationToken">Cancellation token for the startup operation.</param>
    /// <returns>A task that represents the asynchronous operation.</returns>
    public async Task StartAsync(CancellationToken cancellationToken)
    {
        await _storage.InitializeAsync();
    }

    /// <summary>
    /// No-op stop; schema initialization does not require cleanup.
    /// </summary>
    /// <param name="cancellationToken">Unused cancellation token.</param>
    /// <returns>A task that represents the asynchronous operation.</returns>
    public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
