using ColaFlow.Modules.Identity.Domain.Aggregates.Users;

namespace ColaFlow.Modules.Identity.Domain.Repositories;

/// <summary>
/// Repository for managing user-tenant-role assignments
/// </summary>
public interface IUserTenantRoleRepository
{
    /// <summary>
    /// Get user's role for a specific tenant
    /// </summary>
    Task<UserTenantRole?> GetByUserAndTenantAsync(
        Guid userId,
        Guid tenantId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// Get all roles for a specific user (across all tenants)
    /// </summary>
    Task<IReadOnlyList<UserTenantRole>> GetByUserAsync(
        Guid userId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// Get all user-role assignments for a specific tenant
    /// </summary>
    Task<IReadOnlyList<UserTenantRole>> GetByTenantAsync(
        Guid tenantId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// Add a new user-tenant-role assignment
    /// </summary>
    Task AddAsync(UserTenantRole role, CancellationToken cancellationToken = default);

    /// <summary>
    /// Update an existing user-tenant-role assignment
    /// </summary>
    Task UpdateAsync(UserTenantRole role, CancellationToken cancellationToken = default);

    /// <summary>
    /// Delete a user-tenant-role assignment (remove user from tenant)
    /// </summary>
    Task DeleteAsync(UserTenantRole role, CancellationToken cancellationToken = default);

    /// <summary>
    /// Get all users in a tenant with their roles (paginated)
    /// </summary>
    Task<(List<UserTenantRole> Items, int TotalCount)> GetTenantUsersWithRolesAsync(
        Guid tenantId,
        int pageNumber = 1,
        int pageSize = 20,
        string? searchTerm = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// Check if user is the last TenantOwner in the tenant
    /// </summary>
    Task<bool> IsLastTenantOwnerAsync(
        Guid tenantId,
        Guid userId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// Count users with specific role in tenant
    /// </summary>
    Task<int> CountByTenantAndRoleAsync(
        Guid tenantId,
        TenantRole role,
        CancellationToken cancellationToken = default);
}
