import delay from 'delay';
import { browser } from 'wxt/browser';
import { listenerService } from './bridge/listener';
import { brideService } from './bridge/global';
import { socketService } from './socket';
import { shopeeService, shopeeShopIdentify } from './shopee';
import { tiktokShopIdentify } from './tiktok';
import { lazadaShopIdentify } from './lazada';
import { userSessionRefresh } from './service/user';
import { storageWatchService } from './service/storage';
import { loggerBackground } from '@/plugins/logger';
import { modelCleanerData, modelIndexLoader } from '@/models/loader';

loggerBackground.log('🚀 Service Worker started', new Date().toLocaleTimeString());

self.addEventListener('error', (event) => {
  if (event.message?.includes('fingerprint')) {
    event.preventDefault();
  }
});

self.addEventListener('unhandledrejection', (event) => {
  if (event.reason?.message?.includes('fingerprint')) {
    event.preventDefault();
  }
});

browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message && message.type === 'wakeUp') {
    sendResponse({ ok: true });
  }
  return true;
});

browser.runtime.onConnect.addListener((port) => {
  if (port.name === 'keepAlive') {
    port.onMessage.addListener(() => {});
    port.onDisconnect.addListener(() => {});
  }
});

const serviceStart = () => {
  modelCleanerData().catch((error) => {
    loggerBackground.error({
      msg: 'BACKGROUND_SERVICE_ERROR',
      service: 'cleanerOldData',
      error,
    });
  });

  modelIndexLoader().catch((error) => {
    loggerBackground.error({
      msg: 'BACKGROUND_SERVICE_ERROR',
      service: 'modelIndexLoader',
      error,
    });
  });

  shopeeShopIdentify().catch((error) => {
    loggerBackground.error({
      msg: 'BACKGROUND_SERVICE_ERROR',
      service: 'shopee',
      error,
    });
  });

  tiktokShopIdentify().catch((error) => {
    loggerBackground.error({
      msg: 'BACKGROUND_SERVICE_ERROR',
      service: 'tiktok',
      error,
    });
  });

  lazadaShopIdentify().catch((error) => {
    loggerBackground.error({
      msg: 'BACKGROUND_SERVICE_ERROR',
      service: 'lazada',
      error,
    });
  });

  listenerService().catch((error) => {
    loggerBackground.error({
      msg: 'BACKGROUND_SERVICE_ERROR',
      service: 'listener',
      error,
    });
  });

  brideService().catch((error) => {
    loggerBackground.error({
      msg: 'BACKGROUND_SERVICE_ERROR',
      service: 'bridge',
      error,
    });
  });

  socketService().catch((error) => {
    loggerBackground.error({
      msg: 'BACKGROUND_SERVICE_ERROR',
      service: 'socket',
      error,
    });
  });

  setInterval(() => {
    shopeeShopIdentify().catch((error) => {
      loggerBackground.error({
        msg: 'BACKGROUND_SERVICE_ERROR',
        service: 'shopee',
        error,
      });
    });

    tiktokShopIdentify().catch((error) => {
      loggerBackground.error({
        msg: 'BACKGROUND_SERVICE_ERROR',
        service: 'tiktok',
        error,
      });
    });

    lazadaShopIdentify().catch((error) => {
      loggerBackground.error({
        msg: 'BACKGROUND_SERVICE_ERROR',
        service: 'lazada',
        error,
      });
    });
  }, 60_000);
};

storageWatchService();

delay(1000).then(serviceStart);

delay(60_000).then(async () => {
  await userSessionRefresh().catch((error) => {
    loggerBackground.error({
      msg: 'BACKGROUND_SERVICE_ERROR',
      service: 'userSessionRefresh',
      error,
    });
  });

  shopeeService().catch((error) => {
    loggerBackground.error({
      msg: 'BACKGROUND_SERVICE_ERROR',
      service: 'shopee',
      error,
    });
  });
});
