namespace Books.Api.Domain.UserAccess;

/// <summary>
/// Roles a user can have for a company.
/// </summary>
public enum CompanyRole
{
    /// <summary>
    /// Full access - can manage users, delete company, all operations
    /// </summary>
    Owner,

    /// <summary>
    /// Can create journal entries, manage accounts, close fiscal years
    /// </summary>
    Accountant,

    /// <summary>
    /// Read-only access to all data
    /// </summary>
    Viewer
}

public static class CompanyRoleExtensions
{
    public static string ToDbString(this CompanyRole role) => role switch
    {
        CompanyRole.Owner => "owner",
        CompanyRole.Accountant => "accountant",
        CompanyRole.Viewer => "viewer",
        _ => throw new ArgumentOutOfRangeException(nameof(role))
    };

    public static CompanyRole FromDbString(string value) => value switch
    {
        "owner" => CompanyRole.Owner,
        "accountant" => CompanyRole.Accountant,
        "viewer" => CompanyRole.Viewer,
        _ => throw new ArgumentOutOfRangeException(nameof(value), $"Unknown role: {value}")
    };

    /// <summary>
    /// Check if role can perform write operations (create/update/delete)
    /// </summary>
    public static bool CanWrite(this CompanyRole role) => role is CompanyRole.Owner or CompanyRole.Accountant;

    /// <summary>
    /// Check if role can manage users (grant/revoke access)
    /// </summary>
    public static bool CanManageUsers(this CompanyRole role) => role is CompanyRole.Owner;

    /// <summary>
    /// Check if role can delete the company
    /// </summary>
    public static bool CanDeleteCompany(this CompanyRole role) => role is CompanyRole.Owner;
}
