using System.Text.Json;

namespace Tiku.Application.Tenancy;

public sealed record TenantFrontendConfigDraft(
    JsonElement Branding,
    JsonElement Theme,
    JsonElement Features,
    JsonElement Navigation,
    JsonElement HomeModules);

public sealed record TenantFrontendConfigItem(
    int SchemaVersion,
    int ConfigVersion,
    TenantFrontendConfigDraft Published,
    TenantFrontendConfigDraft Draft,
    DateTimeOffset? PublishedAt);

public sealed record TenantRuntimeBootstrap(
    int SchemaVersion,
    int ConfigVersion,
    string TenantCode,
    string TenantName,
    string SiteState,
    JsonElement Branding,
    JsonElement Theme,
    JsonElement Features,
    JsonElement Navigation,
    JsonElement HomeModules,
    IReadOnlyCollection<string> EnabledFeatures,
    IReadOnlyCollection<string> LoginMethods);

public interface ITenantFrontendConfigService
{
    Task<TenantFrontendConfigItem> GetAsync(Guid tenantId, CancellationToken cancellationToken = default);

    Task<TenantFrontendConfigItem> SaveDraftAsync(
        Guid tenantId,
        TenantFrontendConfigDraft draft,
        CancellationToken cancellationToken = default);

    Task<TenantFrontendConfigItem> PublishAsync(
        Guid tenantId,
        int expectedVersion,
        CancellationToken cancellationToken = default);

    Task<TenantRuntimeBootstrap> GetRuntimeAsync(Guid tenantId, CancellationToken cancellationToken = default);
}

public sealed class TenantFrontendConfigException(string code, string message) : Exception(message)
{
    public string Code { get; } = code;
}