export const DEFAULT_CUSTOM_EMAILS = [
  "new_subscription",
  "subscription_activated",
  "subscription_on_hold",
  "subscription_cancelled",
  "subscription_expired",
  "renewal_upcoming",
  "renewal_invoice",
  "renewal_payment_received",
  "payment_failed",
];

export const DEFAULT_ADMIN_EMAILS = [
  "admin_new_subscription",
  "admin_cancelled",
  "admin_payment_failed",
];

export const PRO_ADDITIONAL_FEATURES = [
  "store_credit",
  "feature_manager",
  "audit_logging",
  "multi_login",
];

const BASE_PROFILE = {
  business_type: "other",
  billing_cycle: "monthly",
  billing_interval: "1",
  billing_period: "month",
  plan_structure: "single",
  offer_trials: "no",
  trial_require_payment: "yes",
  one_trial_per_customer: "yes",
  grace_period: "standard",
  custom_grace_active_days: "3",
  custom_grace_hold_days: "7",
  invoice_timing: "6_hours",
  renewal_sync: "no",
  renewal_sync_first_charge_mode: "prorate",
  billing_flexibility: [],
  max_skips: "3",
  skip_cutoff_days: "2",
  max_pause_days: "30",
  max_pauses: "2",
  multiple_subscriptions: "allow",
  auto_migrate: "yes",
  mixed_cart: "yes",
  one_click_checkout: "default",
  disable_cart_page: "no",
  auto_create_account: "yes",
  plan_switching: "disabled",
  proration_type: "prorate_immediately",
  cancel_timing: "immediately",
  require_cancel_reason: "yes",
  retention_offers: "no",
  retention_offer_types: ["discount"],
  retention_discount_percent: "20",
  retention_discount_cycles: "3",
  refund_policy: "none",
  enable_access_control: "no",
  restricted_redirect: "redirect",
  restricted_redirect_url: "/pricing",
  require_login: "yes",
  email_level: "all",
  custom_emails: [...DEFAULT_CUSTOM_EMAILS],
  renewal_reminder_days: "3",
  admin_emails: [...DEFAULT_ADMIN_EMAILS],
  additional_features: [],
  store_credit_auto_apply: "yes",
  store_credit_expiration: "0",
  feature_manager_product_page: "yes",
  multi_login_max_sessions: "1",
  dashboard_redirect: "my_account",
};

const cloneArray = (value) => (Array.isArray(value) ? [...value] : []);

const createProfile = (overrides = {}) => ({
  ...BASE_PROFILE,
  ...overrides,
  billing_flexibility: cloneArray(
    overrides.billing_flexibility ?? BASE_PROFILE.billing_flexibility,
  ),
  retention_offer_types: cloneArray(
    overrides.retention_offer_types ?? BASE_PROFILE.retention_offer_types,
  ),
  custom_emails: cloneArray(
    overrides.custom_emails ?? BASE_PROFILE.custom_emails,
  ),
  admin_emails: cloneArray(overrides.admin_emails ?? BASE_PROFILE.admin_emails),
  additional_features: cloneArray(
    overrides.additional_features ?? BASE_PROFILE.additional_features,
  ),
});

export const BUSINESS_PROFILES = {
  other: createProfile(),
  saas: createProfile({
    business_type: "saas",
    plan_structure: "multiple",
    offer_trials: "yes",
    grace_period: "strict",
    multiple_subscriptions: "one_per_customer",
    mixed_cart: "no",
    one_click_checkout: "subscription_items",
    plan_switching: "all",
    cancel_timing: "end_of_period",
    retention_offers: "yes",
    retention_offer_types: ["discount", "downgrade"],
    refund_policy: "immediate",
    enable_access_control: "yes",
    additional_features: [
      "feature_manager",
      "audit_logging",
      "hide_admin_bar",
      "restrict_dashboard",
    ],
  }),
  box: createProfile({
    business_type: "box",
    grace_period: "lenient",
    billing_flexibility: ["skip", "pause"],
    retention_offers: "yes",
    retention_offer_types: ["pause", "contact_support"],
    additional_features: [
      "store_credit",
      "custom_profile_fields",
      "hide_admin_bar",
    ],
  }),
  membership: createProfile({
    business_type: "membership",
    plan_structure: "multiple",
    offer_trials: "yes",
    billing_flexibility: ["pause"],
    multiple_subscriptions: "one_per_customer",
    mixed_cart: "no",
    one_click_checkout: "subscription_items",
    plan_switching: "all",
    cancel_timing: "end_of_period",
    retention_offers: "yes",
    retention_offer_types: ["discount", "downgrade", "pause"],
    enable_access_control: "yes",
    additional_features: [
      "feature_manager",
      "custom_profile_fields",
      "myaccount_editor",
      "hide_admin_bar",
      "restrict_dashboard",
      "multi_login",
    ],
  }),
  content: createProfile({
    business_type: "content",
    plan_structure: "multiple",
    offer_trials: "yes",
    plan_switching: "upgrades_only",
    cancel_timing: "end_of_period",
    retention_offers: "yes",
    retention_offer_types: ["discount", "downgrade"],
    refund_policy: "end_of_period",
    enable_access_control: "yes",
    additional_features: [
      "myaccount_editor",
      "hide_admin_bar",
      "restrict_dashboard",
    ],
  }),
  services: createProfile({
    business_type: "services",
    billing_flexibility: ["pause"],
    retention_offer_types: ["contact_support"],
    refund_policy: "immediate",
    additional_features: ["custom_profile_fields", "hide_admin_bar"],
  }),
  nonprofit: createProfile({
    business_type: "nonprofit",
    grace_period: "lenient",
    additional_features: ["hide_admin_bar"],
  }),
};

export const getProfileDefaults = (businessType = "other") => {
  const profile = BUSINESS_PROFILES[businessType] || BUSINESS_PROFILES.other;

  return createProfile(profile);
};

export const isAccessControlBusinessType = (businessType) => {
  return ["membership", "content", "saas"].includes(businessType);
};
