/**
 * Reports Module - Shared Store Instances
 *
 * This file instantiates the shared stores (auth, companies, accountingPeriod)
 * with the Reports module's API service.
 */

import { createAuthStore } from '@shared/stores/auth'
import { createCompaniesStore } from '@shared/stores/companies'
import { createAccountingPeriodStore } from '@shared/stores/accountingPeriod'
import api from '@reports/services/api'

// Import module-specific stores that need to be reset on logout
import { useDashboardStore } from './dashboard'
import { useInvoicesStore } from './invoices'
import { useTreasuryStore } from './treasury'
import { useTrialBalanceStore } from './trialBalance'

/**
 * US-028/US-031: Reset all module stores on logout
 * Prevents stale data from previous company after re-login
 *
 * @param {Object} context - Context saved before logout (US-031)
 * @param {string} context.username - Username from before logout
 * @param {string} context.serverId - Server ID from before logout
 */
const resetAllStores = (context = {}) => {
  const { username, serverId } = context
  console.log('[Reports] Resetting all stores on logout...', { username, serverId })

  // Reset module-specific stores
  try {
    const dashboardStore = useDashboardStore()
    if (dashboardStore.reset) dashboardStore.reset()
  } catch (e) {
    console.warn('[Reports] Could not reset dashboard store:', e.message)
  }

  try {
    const invoicesStore = useInvoicesStore()
    if (invoicesStore.reset) invoicesStore.reset()
  } catch (e) {
    console.warn('[Reports] Could not reset invoices store:', e.message)
  }

  try {
    const treasuryStore = useTreasuryStore()
    if (treasuryStore.reset) treasuryStore.reset()
  } catch (e) {
    console.warn('[Reports] Could not reset treasury store:', e.message)
  }

  try {
    const trialBalanceStore = useTrialBalanceStore()
    if (trialBalanceStore.reset) trialBalanceStore.reset()
  } catch (e) {
    console.warn('[Reports] Could not reset trial balance store:', e.message)
  }

  // Reset shared stores (companies and periods)
  // Note: These are reset AFTER module stores since they may depend on auth
  // US-031: Use resetWithContext with explicit username/serverId
  try {
    const companyStore = useCompanyStore()
    if (companyStore.resetWithContext && username) {
      // Use the new method that accepts explicit parameters
      companyStore.resetWithContext(username, serverId)
    } else if (companyStore.reset) {
      // Fallback to regular reset (won't clear localStorage properly)
      companyStore.reset()
    }
  } catch (e) {
    console.warn('[Reports] Could not reset company store:', e.message)
  }

  try {
    const periodStore = useAccountingPeriodStore()
    if (periodStore.reset) periodStore.reset()
  } catch (e) {
    console.warn('[Reports] Could not reset period store:', e.message)
  }

  console.log('[Reports] All stores reset complete')
}

// Create auth store with onLogout callback (US-028)
export const useAuthStore = createAuthStore(api, {
  onLogout: resetAllStores
})

// Create companies store (needs auth store reference)
export const useCompanyStore = createCompaniesStore(api, useAuthStore)

// Create accounting period store
export const useAccountingPeriodStore = createAccountingPeriodStore(api)
