import { storage } from '@wxt-dev/storage';

// --- User ---
export const storageUserId = storage.defineItem('local:user_id', {
  fallback: '',
});

export const storageUserName = storage.defineItem('local:user_name', {
  fallback: '',
});

export const storageUserToken = storage.defineItem('local:user_token', {
  fallback: '',
});

export const storageUserPlan = storage.defineItem('local:user_extension_plan', {
  fallback: 'Free',
});

export const storageUserData = storage.defineItem('local:user_data', {
  fallback: {},
});

export const storageUserPassword = storage.defineItem('local:user_password', {
  fallback: '',
});

// --- Identity ---
export const storageAppUuid = storage.defineItem('local:indentify_app_uuid', {
  fallback: '',
});

export const storageProfileId = storage.defineItem('local:indentify_profile_id', {
  fallback: '',
});

export const storageBrowserIp = storage.defineItem('local:indentify_browser_ip', {
  fallback: '',
});

export const storageBrowserName = storage.defineItem('local:indentify_browser_name', {
  fallback: '',
});

// --- App ---
export const storageBetaMode = storage.defineItem('local:beta_mode', {
  fallback: false,
});

export const storageUseShopManager = storage.defineItem('local:use_shopmanager', {
  fallback: false,
});

export const storageWorkerFetchService = storage.defineItem('local:worker_fetch_service', {
  fallback: true,
});

export const storageToggleMenu = storage.defineItem('local:toggle_menu', {
  fallback: false,
});

export const storageBadWord = storage.defineItem('local:bard_word_v4', {
  fallback: ['chuyển khoản', 'stk', 'lazada', 'tiktok'],
});

export const storageSaveHistoryPrint = storage.defineItem('local:save_history_print', {
  fallback: { shopee: false, lazada: false, tiktok: false },
});

export const storageExtensionInfo = storage.defineItem('local:config_extension_data', {
  fallback: {},
  version: 2,
  migrations: {
    2: (oldValue) => {
      if (!oldValue || typeof oldValue !== 'object') {
        return {};
      }
      return { ...oldValue };
    },
  },
});

const platformShopConfigFallback = { shopee: {}, lazada: {}, tiktok: {} };

export const storagePlatformShopConfig = storage.defineItem('local:config_platform_shop_data', {
  fallback: platformShopConfigFallback,
  version: 2,
  migrations: {
    2: (oldValue) => {
      if (!oldValue || typeof oldValue !== 'object') {
        return { ...platformShopConfigFallback };
      }
      return {
        shopee: oldValue.shopee || {},
        lazada: oldValue.lazada || {},
        tiktok: oldValue.tiktok || {},
        ...oldValue,
      };
    },
  },
});

// --- Shopee ---
export const storageShopeeShopId = storage.defineItem('local:shopee_shop_id', {
  fallback: '',
});

export const storageShopeeShopName = storage.defineItem('local:shopee_shop_name', {
  fallback: '',
});

const shopeeConfigProductFallback = {
  spyDetail: true,
  spySearch: true,
  spyAds: true,
  showShopName: false,
  addCloneBtn: false,
};

export const storageShopeeConfigProduct = storage.defineItem('local:shopee_config_product_v2', {
  fallback: shopeeConfigProductFallback,
  version: 2,
  migrations: {
    2: (oldValue) => {
      if (!oldValue || typeof oldValue !== 'object') {
        return { ...shopeeConfigProductFallback };
      }
      return { ...shopeeConfigProductFallback, ...oldValue };
    },
  },
});

export const storageShopeeFlashsaleCampaignAuto = storage.defineItem('local:shopee_flashsale_campaign_auto', {
  fallback: false,
});

export const storageShopeeFlashsaleSchedulerAuto = storage.defineItem('local:shopee_flashsale_scheduler_auto', {
  fallback: true,
});

// --- Affiliate Tracking ---
export const storageAffTrackingShopeeTime = storage.defineItem('local:tracker_id_sp_last_time', {
  fallback: 0,
});

export const storageAffTrackingLazadaTime = storage.defineItem('local:tracker_id_lz_last_time', {
  fallback: 0,
});

export const storageAffTrackingTikiTime = storage.defineItem('local:tracker_id_tk_last_time', {
  fallback: 0,
});

// --- TikTok ---
export const storageTiktokShopId = storage.defineItem('local:tiktok_shop_id', {
  fallback: '',
});

export const storageTiktokShopName = storage.defineItem('local:tiktok_shop_name', {
  fallback: '',
});

// --- Lazada ---
export const storageLazadaShopId = storage.defineItem('local:lazada_shop_id', {
  fallback: '',
});

export const storageLazadaShopName = storage.defineItem('local:lazada_shop_name', {
  fallback: '',
});
