// <auto-generated />
// This file is auto-generated from core/models/src/vault/SystemFieldRegistry.ts
// Do not edit this file directly. Run 'npm run generate:models' to regenerate.

#nullable enable

namespace AliasClientDb.Models;

/// <summary>
/// Field categories for grouping in UI.
/// </summary>
public enum FieldCategory
{
    /// <summary>Primary fields.</summary>
    Primary,
    /// <summary>Login fields.</summary>
    Login,
    /// <summary>Alias fields.</summary>
    Alias,
    /// <summary>Card fields.</summary>
    Card,
    /// <summary>Custom fields.</summary>
    Custom,
    /// <summary>Notes fields.</summary>
    Notes,
    /// <summary>Metadata fields.</summary>
    Metadata
}

/// <summary>
/// Per-item-type configuration for a system field.
/// </summary>
/// <param name="ShowByDefault">Whether this field is shown by default in create mode (vs. hidden behind an "add" button)</param>
public record ItemTypeFieldConfig(bool ShowByDefault);

/// <summary>
/// System field definition with metadata.
/// </summary>
/// <param name="FieldKey">Unique system field key (e.g., 'login.username')</param>
/// <param name="FieldType">Field type for rendering/validation</param>
/// <param name="IsHidden">Whether field is hidden/masked by default</param>
/// <param name="IsMultiValue">Whether field supports multiple values</param>
/// <param name="ApplicableToTypes">ApplicableToTypes</param>
/// <param name="EnableHistory">Whether to track field value history</param>
/// <param name="Category">Category for grouping in UI. 'Primary' fields are shown in the name block.</param>
/// <param name="DefaultDisplayOrder">Default display order within category (lower = first)</param>
public record SystemFieldDefinition(
    string FieldKey,
    string FieldType,
    bool IsHidden,
    bool IsMultiValue,
    IReadOnlyDictionary<string, ItemTypeFieldConfig> ApplicableToTypes,
    bool EnableHistory,
    FieldCategory Category,
    int DefaultDisplayOrder);

/// <summary>
/// Registry of all system-defined fields.
/// These fields are immutable and their metadata is defined in code.
/// </summary>
public static class SystemFieldRegistry
{
    /// <summary>
    /// All system field definitions indexed by field key.
    /// </summary>
    public static readonly IReadOnlyDictionary<string, SystemFieldDefinition> Fields =
        new Dictionary<string, SystemFieldDefinition>
        {
            [FieldKey.LoginUsername] = new SystemFieldDefinition(
                FieldKey: "login.username",
                FieldType: "Text",
                IsHidden: false,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["Login"] = new ItemTypeFieldConfig(true), ["Alias"] = new ItemTypeFieldConfig(true) },
                EnableHistory: true,
                Category: FieldCategory.Login,
                DefaultDisplayOrder: 10),
            [FieldKey.LoginPassword] = new SystemFieldDefinition(
                FieldKey: "login.password",
                FieldType: "Password",
                IsHidden: true,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["Login"] = new ItemTypeFieldConfig(true), ["Alias"] = new ItemTypeFieldConfig(true) },
                EnableHistory: true,
                Category: FieldCategory.Login,
                DefaultDisplayOrder: 20),
            [FieldKey.LoginEmail] = new SystemFieldDefinition(
                FieldKey: "login.email",
                FieldType: "Email",
                IsHidden: false,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["Login"] = new ItemTypeFieldConfig(false), ["Alias"] = new ItemTypeFieldConfig(true) },
                EnableHistory: true,
                Category: FieldCategory.Login,
                DefaultDisplayOrder: 15),
            [FieldKey.LoginUrl] = new SystemFieldDefinition(
                FieldKey: "login.url",
                FieldType: "URL",
                IsHidden: false,
                IsMultiValue: true,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["Login"] = new ItemTypeFieldConfig(true), ["Alias"] = new ItemTypeFieldConfig(true) },
                EnableHistory: false,
                Category: FieldCategory.Primary,
                DefaultDisplayOrder: 5),
            [FieldKey.AliasFirstName] = new SystemFieldDefinition(
                FieldKey: "alias.first_name",
                FieldType: "Text",
                IsHidden: false,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["Alias"] = new ItemTypeFieldConfig(true) },
                EnableHistory: false,
                Category: FieldCategory.Alias,
                DefaultDisplayOrder: 20),
            [FieldKey.AliasLastName] = new SystemFieldDefinition(
                FieldKey: "alias.last_name",
                FieldType: "Text",
                IsHidden: false,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["Alias"] = new ItemTypeFieldConfig(true) },
                EnableHistory: false,
                Category: FieldCategory.Alias,
                DefaultDisplayOrder: 30),
            [FieldKey.AliasGender] = new SystemFieldDefinition(
                FieldKey: "alias.gender",
                FieldType: "Text",
                IsHidden: false,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["Alias"] = new ItemTypeFieldConfig(true) },
                EnableHistory: false,
                Category: FieldCategory.Alias,
                DefaultDisplayOrder: 50),
            [FieldKey.AliasBirthdate] = new SystemFieldDefinition(
                FieldKey: "alias.birthdate",
                FieldType: "Date",
                IsHidden: false,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["Alias"] = new ItemTypeFieldConfig(true) },
                EnableHistory: false,
                Category: FieldCategory.Alias,
                DefaultDisplayOrder: 60),
            [FieldKey.CardCardholderName] = new SystemFieldDefinition(
                FieldKey: "card.cardholder_name",
                FieldType: "Text",
                IsHidden: false,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["CreditCard"] = new ItemTypeFieldConfig(true) },
                EnableHistory: false,
                Category: FieldCategory.Card,
                DefaultDisplayOrder: 10),
            [FieldKey.CardNumber] = new SystemFieldDefinition(
                FieldKey: "card.number",
                FieldType: "Hidden",
                IsHidden: true,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["CreditCard"] = new ItemTypeFieldConfig(true) },
                EnableHistory: false,
                Category: FieldCategory.Card,
                DefaultDisplayOrder: 20),
            [FieldKey.CardExpiryMonth] = new SystemFieldDefinition(
                FieldKey: "card.expiry_month",
                FieldType: "Text",
                IsHidden: false,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["CreditCard"] = new ItemTypeFieldConfig(true) },
                EnableHistory: false,
                Category: FieldCategory.Card,
                DefaultDisplayOrder: 30),
            [FieldKey.CardExpiryYear] = new SystemFieldDefinition(
                FieldKey: "card.expiry_year",
                FieldType: "Text",
                IsHidden: false,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["CreditCard"] = new ItemTypeFieldConfig(true) },
                EnableHistory: false,
                Category: FieldCategory.Card,
                DefaultDisplayOrder: 40),
            [FieldKey.CardCvv] = new SystemFieldDefinition(
                FieldKey: "card.cvv",
                FieldType: "Hidden",
                IsHidden: true,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["CreditCard"] = new ItemTypeFieldConfig(true) },
                EnableHistory: false,
                Category: FieldCategory.Card,
                DefaultDisplayOrder: 50),
            [FieldKey.CardPin] = new SystemFieldDefinition(
                FieldKey: "card.pin",
                FieldType: "Hidden",
                IsHidden: true,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["CreditCard"] = new ItemTypeFieldConfig(false) },
                EnableHistory: false,
                Category: FieldCategory.Card,
                DefaultDisplayOrder: 60),
            [FieldKey.NotesContent] = new SystemFieldDefinition(
                FieldKey: "notes.content",
                FieldType: "TextArea",
                IsHidden: false,
                IsMultiValue: false,
                ApplicableToTypes: new Dictionary<string, ItemTypeFieldConfig> { ["Login"] = new ItemTypeFieldConfig(false), ["Alias"] = new ItemTypeFieldConfig(false), ["CreditCard"] = new ItemTypeFieldConfig(false), ["Note"] = new ItemTypeFieldConfig(true) },
                EnableHistory: false,
                Category: FieldCategory.Notes,
                DefaultDisplayOrder: 100)
        };

    /// <summary>
    /// Get system field definition by key.
    /// </summary>
    /// <param name="fieldKey">The field key to look up.</param>
    /// <returns>The field definition, or null if not found.</returns>
    public static SystemFieldDefinition? GetSystemField(string fieldKey)
    {
        return Fields.TryGetValue(fieldKey, out var field) ? field : null;
    }

    /// <summary>
    /// Check if a field key represents a system field.
    /// </summary>
    /// <param name="fieldKey">The field key to check.</param>
    /// <returns>True if the field key is a system field.</returns>
    public static bool IsSystemField(string fieldKey)
    {
        return Fields.ContainsKey(fieldKey);
    }

    /// <summary>
    /// Check if a field applies to a specific item type.
    /// </summary>
    /// <param name="field">The field definition.</param>
    /// <param name="itemType">The item type to check.</param>
    /// <returns>True if the field applies to the item type.</returns>
    public static bool FieldAppliesToType(SystemFieldDefinition field, string itemType)
    {
        return field.ApplicableToTypes.ContainsKey(itemType);
    }

    /// <summary>
    /// Get all system fields applicable to a specific item type.
    /// Results are sorted by DefaultDisplayOrder.
    /// </summary>
    /// <param name="itemType">The item type.</param>
    /// <returns>Fields applicable to the item type.</returns>
    public static IEnumerable<SystemFieldDefinition> GetFieldsForItemType(string itemType)
    {
        return Fields.Values
            .Where(f => f.ApplicableToTypes.ContainsKey(itemType))
            .OrderBy(f => f.DefaultDisplayOrder);
    }

    /// <summary>
    /// Get system fields that should be shown by default for a specific item type.
    /// Results are sorted by DefaultDisplayOrder.
    /// </summary>
    /// <param name="itemType">The item type.</param>
    /// <returns>Fields shown by default for the item type.</returns>
    public static IEnumerable<SystemFieldDefinition> GetDefaultFieldsForItemType(string itemType)
    {
        return Fields.Values
            .Where(f => f.ApplicableToTypes.TryGetValue(itemType, out var config) && config.ShowByDefault)
            .OrderBy(f => f.DefaultDisplayOrder);
    }

    /// <summary>
    /// Get system fields that are NOT shown by default for a specific item type.
    /// These are the fields that can be added via an "add field" button.
    /// Results are sorted by DefaultDisplayOrder.
    /// </summary>
    /// <param name="itemType">The item type.</param>
    /// <returns>Optional fields for the item type.</returns>
    public static IEnumerable<SystemFieldDefinition> GetOptionalFieldsForItemType(string itemType)
    {
        return Fields.Values
            .Where(f => f.ApplicableToTypes.TryGetValue(itemType, out var config) && !config.ShowByDefault)
            .OrderBy(f => f.DefaultDisplayOrder);
    }

    /// <summary>
    /// Check if a field key matches a known system field prefix.
    /// </summary>
    /// <param name="fieldKey">The field key to check.</param>
    /// <returns>True if the field key has a system field prefix.</returns>
    public static bool IsSystemFieldPrefix(string fieldKey)
    {
        return fieldKey.StartsWith("login.") ||
               fieldKey.StartsWith("alias.") ||
               fieldKey.StartsWith("card.") ||
               fieldKey.StartsWith("notes.");
    }
}
