using ZB.MOM.WW.ScadaBridge.Commons.Entities.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;

namespace ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;

public interface ICentralUiRepository
{
    /// <summary>Returns all configured sites.</summary>
    /// <param name="cancellationToken">Cancellation token.</param>
    /// <returns>A task that resolves to a read-only list of all sites.</returns>
    Task<IReadOnlyList<Site>> GetAllSitesAsync(CancellationToken cancellationToken = default);
    /// <summary>Returns all data connections for the specified site.</summary>
    /// <param name="siteId">The site database ID to filter by.</param>
    /// <param name="cancellationToken">Cancellation token.</param>
    /// <returns>A task that resolves to a read-only list of data connections for the site.</returns>
    Task<IReadOnlyList<DataConnection>> GetDataConnectionsBySiteIdAsync(int siteId, CancellationToken cancellationToken = default);
    /// <summary>Returns all data connections across all sites.</summary>
    /// <param name="cancellationToken">Cancellation token.</param>
    /// <returns>A task that resolves to a read-only list of all data connections.</returns>
    Task<IReadOnlyList<DataConnection>> GetAllDataConnectionsAsync(CancellationToken cancellationToken = default);
    /// <summary>Returns the full template tree including folders and templates.</summary>
    /// <param name="cancellationToken">Cancellation token.</param>
    /// <returns>A task that resolves to a read-only list of templates.</returns>
    Task<IReadOnlyList<Template>> GetTemplateTreeAsync(CancellationToken cancellationToken = default);
    /// <summary>Returns instances filtered by optional site, template, or search term.</summary>
    /// <param name="siteId">Optional site ID to filter by.</param>
    /// <param name="templateId">Optional template ID to filter by.</param>
    /// <param name="searchTerm">Optional keyword to filter instance names.</param>
    /// <param name="cancellationToken">Cancellation token.</param>
    /// <returns>A task that resolves to a read-only list of matching instances.</returns>
    Task<IReadOnlyList<Instance>> GetInstancesFilteredAsync(int? siteId = null, int? templateId = null, string? searchTerm = null, CancellationToken cancellationToken = default);
    /// <summary>Returns the most recent deployment records up to the specified count.</summary>
    /// <param name="count">Maximum number of records to return.</param>
    /// <param name="cancellationToken">Cancellation token.</param>
    /// <returns>A task that resolves to a read-only list of recent deployment records.</returns>
    Task<IReadOnlyList<DeploymentRecord>> GetRecentDeploymentsAsync(int count, CancellationToken cancellationToken = default);
    /// <summary>Returns the area tree for the specified site.</summary>
    /// <param name="siteId">The site database ID.</param>
    /// <param name="cancellationToken">Cancellation token.</param>
    /// <returns>A task that resolves to a read-only list of areas for the site.</returns>
    Task<IReadOnlyList<Area>> GetAreaTreeBySiteIdAsync(int siteId, CancellationToken cancellationToken = default);

    // Audit log queries
    /// <summary>
    /// Queries audit log entries with optional filters, returning a page of results and the total matching count.
    /// </summary>
    /// <param name="user">Optional user filter.</param>
    /// <param name="entityType">Optional entity type filter.</param>
    /// <param name="action">Optional action filter.</param>
    /// <param name="from">Optional start of date range filter.</param>
    /// <param name="to">Optional end of date range filter.</param>
    /// <param name="entityId">Optional entity ID filter.</param>
    /// <param name="entityName">Optional entity name filter.</param>
    /// <param name="bundleImportId">Optional bundle import correlation ID filter.</param>
    /// <param name="page">One-based page number.</param>
    /// <param name="pageSize">Number of entries per page.</param>
    /// <param name="cancellationToken">Cancellation token.</param>
    /// <returns>A task that resolves to a tuple of the matching entries and the total count.</returns>
    Task<(IReadOnlyList<AuditLogEntry> Entries, int TotalCount)> GetAuditLogEntriesAsync(
        string? user = null,
        string? entityType = null,
        string? action = null,
        DateTimeOffset? from = null,
        DateTimeOffset? to = null,
        string? entityId = null,
        string? entityName = null,
        Guid? bundleImportId = null,
        int page = 1,
        int pageSize = 50,
        CancellationToken cancellationToken = default);

    /// <summary>Persists pending changes to the underlying store.</summary>
    /// <param name="cancellationToken">Cancellation token.</param>
    /// <returns>A task that resolves to the number of entities written.</returns>
    Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}
