/*
 * Copyright (C) Contributors to the Suwayomi project
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

import { plural, t } from '@lingui/core/macro';
import type {
    AutoBackupFlagInclusionState,
    BackupFlag,
    BackupFlagInclusionState,
} from '@/features/backup/Backup.types.ts';
import { BACKUP_FLAGS_TO_TRANSLATION } from '@/features/backup/Backup.constants.ts';

export const convertToAutoBackupFlags = (flags: BackupFlagInclusionState): AutoBackupFlagInclusionState => ({
    autoBackupIncludeCategories: flags.includeCategories,
    autoBackupIncludeChapters: flags.includeChapters,
    autoBackupIncludeClientData: flags.includeClientData,
    autoBackupIncludeHistory: flags.includeHistory,
    autoBackupIncludeManga: flags.includeManga,
    autoBackupIncludeServerSettings: flags.includeServerSettings,
    autoBackupIncludeTracking: flags.includeTracking,
});

export const convertToBackupFlags = (flags: AutoBackupFlagInclusionState): BackupFlagInclusionState => ({
    includeCategories: flags.autoBackupIncludeCategories,
    includeChapters: flags.autoBackupIncludeChapters,
    includeClientData: flags.autoBackupIncludeClientData,
    includeHistory: flags.autoBackupIncludeHistory,
    includeManga: flags.autoBackupIncludeManga,
    includeServerSettings: flags.autoBackupIncludeServerSettings,
    includeTracking: flags.autoBackupIncludeTracking,
});

const getIncludeExcludeText = (count: number, allCount: number, specificText: string): string => {
    if (count === 0) {
        return t`None`;
    }

    if (count === allCount) {
        return t`All`;
    }

    return specificText;
};

export const getAutoBackupFlagsInfo = (autoFlags: AutoBackupFlagInclusionState): Record<`${boolean}`, string> => {
    const flags = convertToBackupFlags(autoFlags);
    const totalFlags = Object.keys(flags).length;

    const flagsByState = Object.groupBy(Object.entries(flags), ([, value]) => value.toString());

    const includedFlagsString =
        flagsByState.true?.map(([key]) => t(BACKUP_FLAGS_TO_TRANSLATION[key as BackupFlag])).join(', ') ?? '';
    const excludedFlagsString =
        flagsByState.false?.map(([key]) => t(BACKUP_FLAGS_TO_TRANSLATION[key as BackupFlag])).join(', ') ?? '';

    return {
        false: getIncludeExcludeText(flagsByState.false?.length ?? 0, totalFlags, excludedFlagsString),
        true: getIncludeExcludeText(flagsByState.true?.length ?? 0, totalFlags, includedFlagsString),
    };
};

export const getBackupCleanupDisplayValue = (ttl: number): string => {
    if (ttl === 0) {
        return t`Never`;
    }

    return plural(ttl, {
        one: `Delete backups that are older than # day`,
        other: `Delete backups that are older than # days`,
    });
};
