@using AliasVault.ImportExport.Models
@using AliasVault.ImportExport.Importers
@using Microsoft.Extensions.Localization
@using System.Text
@inject NavigationManager NavigationManager
@inject GlobalNotificationService GlobalNotificationService
@inject IStringLocalizerFactory LocalizerFactory
@inject ILogger<ImportServiceBitwarden> Logger

<ImportServiceCard
    ServiceName="Bitwarden"
    Description="@Localizer["BitwardenDescription"]"
    LogoUrl="img/importers/bitwarden.svg"
    AcceptedFileExtensions="AcceptedExtensions"
    ProcessFileCallback="ProcessFile">
    <p class="text-gray-700 dark:text-gray-300 mb-4">@Localizer["BitwardenInstructionsPart1"]</p>
    <p class="text-gray-700 dark:text-gray-300 mb-4">@Localizer["BitwardenInstructionsPart2"]</p>
</ImportServiceCard>

@code {
    private IStringLocalizer Localizer => LocalizerFactory.Create("Components.Main.Settings.ImportExport.ImportServices", "AliasVault.Client");
    private static readonly string[] AcceptedExtensions = new[] { ".zip", ".csv" };

    private static async Task<ImportFileResult> ProcessFile(string filename, byte[] fileBytes)
    {
        var extension = Path.GetExtension(filename).ToLowerInvariant();

        switch (extension)
        {
            case ".zip":
                // Binary .zip file with attachments
                var zipImporter = new BitwardenZipImporter();
                return await zipImporter.ImportFromArchiveAsync(fileBytes);

            case ".csv":
                // Text CSV file
                var fileContents = Encoding.UTF8.GetString(fileBytes);
                return await BitwardenImporter.ImportFromCsvAsync(fileContents);

            default:
                throw new InvalidOperationException($"Unsupported file extension: {extension}");
        }
    }
}
