import { z } from 'zod';

type LanguagePriority = 'user' | 'application';

const FeatureFlagsSchema = z.object({
  enableThemeToggle: z
    .boolean({
      error: 'Provide the variable NEXT_PUBLIC_ENABLE_THEME_TOGGLE',
    })
    .describe('Enable theme toggle in the user interface.'),
  enableAccountDeletion: z
    .boolean({
      error:
        'Provide the variable NEXT_PUBLIC_ENABLE_PERSONAL_ACCOUNT_DELETION',
    })
    .describe('Enable personal account deletion.'),
  enableTeamDeletion: z
    .boolean({
      error: 'Provide the variable NEXT_PUBLIC_ENABLE_TEAM_ACCOUNTS_DELETION',
    })
    .describe('Enable team deletion.'),
  enableTeamAccounts: z
    .boolean({
      error: 'Provide the variable NEXT_PUBLIC_ENABLE_TEAM_ACCOUNTS',
    })
    .describe('Enable team accounts.'),
  enableTeamCreation: z
    .boolean({
      error: 'Provide the variable NEXT_PUBLIC_ENABLE_TEAM_ACCOUNTS_CREATION',
    })
    .describe('Enable team creation.'),
  enablePersonalAccountBilling: z
    .boolean({
      error: 'Provide the variable NEXT_PUBLIC_ENABLE_PERSONAL_ACCOUNT_BILLING',
    })
    .describe('Enable personal account billing.'),
  enableTeamAccountBilling: z
    .boolean({
      error: 'Provide the variable NEXT_PUBLIC_ENABLE_TEAM_ACCOUNTS_BILLING',
    })
    .describe('Enable team account billing.'),
  languagePriority: z
    .enum(['user', 'application'], {
      error: 'Provide the variable NEXT_PUBLIC_LANGUAGE_PRIORITY',
    })
    .describe(
      `If set to user, use the user's preferred language. If set to application, use the application's default language.`,
    )
    .default('application'),
  enableNotifications: z
    .boolean({
      error: 'Provide the variable NEXT_PUBLIC_ENABLE_NOTIFICATIONS',
    })
    .describe('Enable notifications functionality'),
  realtimeNotifications: z
    .boolean({
      error: 'Provide the variable NEXT_PUBLIC_REALTIME_NOTIFICATIONS',
    })
    .describe('Enable realtime for the notifications functionality'),
  enableVersionUpdater: z
    .boolean({
      error: 'Provide the variable NEXT_PUBLIC_ENABLE_VERSION_UPDATER',
    })
    .describe('Enable version updater'),
});

const featureFlagsConfig = FeatureFlagsSchema.parse({
  enableThemeToggle: getBoolean(
    process.env.NEXT_PUBLIC_ENABLE_THEME_TOGGLE,
    true,
  ),
  enableAccountDeletion: getBoolean(
    process.env.NEXT_PUBLIC_ENABLE_PERSONAL_ACCOUNT_DELETION,
    false,
  ),
  enableTeamDeletion: getBoolean(
    process.env.NEXT_PUBLIC_ENABLE_TEAM_ACCOUNTS_DELETION,
    false,
  ),
  enableTeamAccounts: getBoolean(
    process.env.NEXT_PUBLIC_ENABLE_TEAM_ACCOUNTS,
    true,
  ),
  enableTeamCreation: getBoolean(
    process.env.NEXT_PUBLIC_ENABLE_TEAM_ACCOUNTS_CREATION,
    true,
  ),
  enablePersonalAccountBilling: getBoolean(
    process.env.NEXT_PUBLIC_ENABLE_PERSONAL_ACCOUNT_BILLING,
    false,
  ),
  enableTeamAccountBilling: getBoolean(
    process.env.NEXT_PUBLIC_ENABLE_TEAM_ACCOUNTS_BILLING,
    false,
  ),
  languagePriority: process.env
    .NEXT_PUBLIC_LANGUAGE_PRIORITY as LanguagePriority,
  enableNotifications: getBoolean(
    process.env.NEXT_PUBLIC_ENABLE_NOTIFICATIONS,
    true,
  ),
  realtimeNotifications: getBoolean(
    process.env.NEXT_PUBLIC_REALTIME_NOTIFICATIONS,
    false,
  ),
  enableVersionUpdater: getBoolean(
    process.env.NEXT_PUBLIC_ENABLE_VERSION_UPDATER,
    false,
  ),
} satisfies z.infer<typeof FeatureFlagsSchema>);

export default featureFlagsConfig;

function getBoolean(value: unknown, defaultValue: boolean) {
  if (typeof value === 'string') {
    return value === 'true';
  }

  return defaultValue;
}
