namespace DodoSSH.Domain;

/// <summary>
/// A group of users who can share vaults.
/// </summary>
/// <remarks>
/// The tables exist from the first migration although team features ship in M3. Adding them
/// later would mean altering <see cref="Vault"/> to introduce a foreign key on a live table, and
/// the cost of carrying two unused tables is far lower than that.
/// </remarks>
public sealed class Team
{
    /// <summary>Primary key.</summary>
    public Guid Id { get; set; }

    /// <summary>Display name.</summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>URL-safe unique identifier.</summary>
    public string Slug { get; set; } = string.Empty;

    /// <summary>Optional description.</summary>
    public string? Description { get; set; }

    /// <summary>Who created it.</summary>
    public Guid CreatedByUserId { get; set; }

    /// <summary>Creation timestamp.</summary>
    public DateTimeOffset CreatedAtUtc { get; set; }

    /// <summary>Soft-delete marker.</summary>
    public DateTimeOffset? DeletedAtUtc { get; set; }

    /// <summary>Members.</summary>
    public ICollection<TeamMembership> Memberships { get; } = [];
}

/// <summary>
/// A user's membership of a team.
/// </summary>
/// <remarks>
/// Revoked memberships are retained rather than deleted, so historic audit entries remain
/// resolvable to a person.
/// </remarks>
public sealed class TeamMembership
{
    /// <summary>Primary key.</summary>
    public Guid Id { get; set; }

    /// <summary>The team.</summary>
    public Guid TeamId { get; set; }

    /// <summary>The team.</summary>
    public Team? Team { get; set; }

    /// <summary>The member.</summary>
    public Guid UserId { get; set; }

    /// <summary>The member.</summary>
    public UserAccount? User { get; set; }

    /// <summary>Role within the team.</summary>
    public TeamRole Role { get; set; }

    /// <summary>Membership state.</summary>
    public MembershipStatus Status { get; set; }

    /// <summary>
    /// Which admin added them.
    /// </summary>
    /// <remarks>
    /// Named for the invitations that used to be the other way in. They are gone — an account is added
    /// by somebody who names it, and there is no path that creates a membership out of a token's email
    /// claim — so this now records only that, and the column keeps its name rather than costing a
    /// migration to rename a field nothing reads but an audit trail.
    /// </remarks>
    public Guid? InvitedByUserId { get; set; }

    /// <summary>When the membership became active.</summary>
    public DateTimeOffset? JoinedAtUtc { get; set; }

    /// <summary>Creation timestamp.</summary>
    public DateTimeOffset CreatedAtUtc { get; set; }

    /// <summary>Soft-delete marker.</summary>
    public DateTimeOffset? DeletedAtUtc { get; set; }
}

