using Fengling.Platform.Domain.AggregatesModel.TenantAggregate;
using Microsoft.AspNetCore.Identity;

namespace Fengling.Platform.Infrastructure;


public interface ITenantManager
{
    Task<Tenant?> FindByIdAsync(long? tenantId, CancellationToken cancellationToken = default);
    Task<Tenant?> FindByTenantCodeAsync(string tenantCode, CancellationToken cancellationToken = default);
    Task<IList<Tenant>> GetAllAsync(CancellationToken cancellationToken = default);
    Task<IList<Tenant>> GetPagedAsync(int page, int pageSize, string? name = null, string? tenantCode = null, TenantStatus? status = null, CancellationToken cancellationToken = default);
    Task<int> GetCountAsync(string? name = null, string? tenantCode = null, TenantStatus? status = null, CancellationToken cancellationToken = default);
    Task<IdentityResult> CreateAsync(Tenant tenant, CancellationToken cancellationToken = default);
    Task<IdentityResult> UpdateAsync(Tenant tenant, CancellationToken cancellationToken = default);
    Task<IdentityResult> DeleteAsync(Tenant tenant, CancellationToken cancellationToken = default);
    Task<int> GetUserCountAsync(long tenantId, CancellationToken cancellationToken = default);
    Task<IdentityResult> SetTenantCodeAsync(Tenant tenant, string code, CancellationToken cancellationToken = default);
}

public sealed class TenantManager(ITenantStore store) : ITenantManager
{
    public async Task<Tenant?> FindByIdAsync(long? tenantId, CancellationToken cancellationToken = default)
    {
        return await store.FindByIdAsync(tenantId, cancellationToken);
    }

    public async Task<Tenant?> FindByTenantCodeAsync(string tenantCode, CancellationToken cancellationToken = default)
    {
        return await store.FindByTenantCodeAsync(tenantCode, cancellationToken);
    }

    public async Task<IList<Tenant>> GetAllAsync(CancellationToken cancellationToken = default)
    {
        return await store.GetAllAsync(cancellationToken);
    }

    public async Task<IList<Tenant>> GetPagedAsync(int page, int pageSize, string? name = null,
        string? tenantCode = null, TenantStatus? status = null, CancellationToken cancellationToken = default)
    {
        return await store.GetPagedAsync(page, pageSize, name, tenantCode, status, cancellationToken);
    }

    public async Task<int> GetCountAsync(string? name = null, string? tenantCode = null,
        TenantStatus? status = null, CancellationToken cancellationToken = default)
    {
        return await store.GetCountAsync(name, tenantCode, status, cancellationToken);
    }

    public async Task<IdentityResult> CreateAsync(Tenant tenant, CancellationToken cancellationToken = default)
    {
        return await store.CreateAsync(tenant, cancellationToken);
    }

    public async Task<IdentityResult> UpdateAsync(Tenant tenant, CancellationToken cancellationToken = default)
    {
        return await store.UpdateAsync(tenant, cancellationToken);
    }

    public async Task<IdentityResult> DeleteAsync(Tenant tenant, CancellationToken cancellationToken = default)
    {
        return await store.DeleteAsync(tenant, cancellationToken);
    }

    public async Task<int> GetUserCountAsync(long tenantId, CancellationToken cancellationToken = default)
    {
        return await store.GetUserCountAsync(tenantId, cancellationToken);
    }

    public async Task<IdentityResult> SetTenantCodeAsync(Tenant tenant, string code, CancellationToken cancellationToken = default)
    {
        await store.SetTenantCodeAsync(tenant, code, cancellationToken);
        return IdentityResult.Success;
    }
}
