<template>
  <div v-if="isOpen" class="fixed inset-0 bg-black bg-opacity-50 overflow-y-auto h-full w-full z-50">
    <div class="relative top-10 mx-auto p-5 border-2 border-black w-full max-w-2xl shadow-lg bg-white">
      <div class="mt-3">
        <div class="flex justify-between items-center mb-6">
          <h3 class="text-lg font-medium text-black">Update Available Balances</h3>
          <button
            @click="closeModal"
            class="text-black hover:text-red-600">
            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
            </svg>
          </button>
        </div>

        <form @submit.prevent="updateBalances" class="space-y-6">
          <!-- Manual Entry Balances -->
          <div class="border-2 border-black p-4">
            <div class="flex justify-between items-center mb-4">
              <div>
                <h4 class="text-md font-semibold text-black">Manual Entry</h4>
                <div v-if="lastManualUpdate" class="text-xs text-black mt-1">
                  Last updated: {{ formatTimestamp(lastManualUpdate) }}
                </div>
              </div>
            </div>
            
            <!-- RBC -->
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-4">
              <div>
                <label class="block text-sm font-medium text-black mb-1">
                  RBC Available Cash (CAD $)
                </label>
                <input
                  v-model.number="balances.rbc_cad"
                  type="number"
                  step="0.01"
                  class="w-full px-3 py-2 border-2 border-black focus:outline-none focus:ring-2 focus:ring-blue-500"
                  placeholder="0.00" />
                <p class="text-xs text-black mt-1">Including overdraft limit</p>
              </div>

              <!-- TD -->
              <div>
                <label class="block text-sm font-medium text-black mb-1">
                  TD Available Cash (CAD $)
                </label>
                <input
                  v-model.number="balances.td_cad"
                  type="number"
                  step="0.01"
                  class="w-full px-3 py-2 border-2 border-black focus:outline-none focus:ring-2 focus:ring-blue-500"
                  placeholder="0.00" />
                <p class="text-xs text-black mt-1">Including overdraft limit</p>
              </div>
            </div>

            <!-- Millennium -->
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
              <div>
                <label class="block text-sm font-medium text-black mb-1">
                  Millennium Balance (€)
                </label>
                <input
                  v-model.number="balances.millennium_eur"
                  type="number"
                  step="0.01"
                  class="w-full px-3 py-2 border-2 border-black focus:outline-none focus:ring-2 focus:ring-blue-500"
                  placeholder="0.00" />
              </div>
            </div>
          </div>


          <!-- Wise API Balances -->
          <div class="border rounded-lg p-4">
            <div class="flex justify-between items-center mb-4">
              <div>
                <h4 class="text-md font-semibold text-gray-800">Wise Balances</h4>
                <div v-if="lastWiseFetch" class="text-xs text-gray-500 mt-1">
                  Last fetched: {{ formatTimestamp(lastWiseFetch) }}
                </div>
              </div>
              <button
                type="button"
                @click="fetchWiseBalances"
                :disabled="loading"
                class="bg-green-500 hover:bg-green-600 disabled:bg-gray-400 text-white text-sm font-medium py-1 px-3 border-2 border-green-700">
                {{ loading ? 'Fetching...' : 'Fetch from API' }}
              </button>
            </div>

            <!-- Jennie Wise -->
            <div class="mb-4">
              <h5 class="text-sm font-medium text-black mb-2">Jennie Wise Balances</h5>
              <div v-if="wiseBalances.jennie && wiseBalances.jennie.length > 0" class="grid grid-cols-1 md:grid-cols-3 gap-3">
                <div v-for="balance in wiseBalances.jennie" :key="`jennie-${balance.currency}`" class="bg-white border-2 border-black p-3">
                  <div class="text-xs text-black">{{ balance.currency }}</div>
                  <div class="text-sm font-medium">{{ formatCurrency(balance.value.value, balance.currency) }}</div>
                </div>
              </div>
              <div v-else class="text-sm text-black italic">
                {{ loading ? 'Loading...' : 'No balances fetched yet' }}
              </div>
            </div>

            <!-- Henry Wise -->
            <div>
              <h5 class="text-sm font-medium text-black mb-2">Henry Wise Balances</h5>
              <div v-if="wiseBalances.henry && wiseBalances.henry.length > 0" class="grid grid-cols-1 md:grid-cols-3 gap-3">
                <div v-for="balance in wiseBalances.henry" :key="`henry-${balance.currency}`" class="bg-white border-2 border-black p-3">
                  <div class="text-xs text-black">{{ balance.currency }}</div>
                  <div class="text-sm font-medium">{{ formatCurrency(balance.value.value, balance.currency) }}</div>
                </div>
              </div>
              <div v-else class="text-sm text-black italic">
                {{ loading ? 'Loading...' : 'No balances fetched yet' }}
              </div>
            </div>
          </div>

          <!-- Total in CAD -->
          <div class="border-t pt-4">
            <div class="bg-white border-2 border-black p-4">
              <div class="flex justify-between items-center">
                <span class="text-sm font-medium text-black">Total Available Balance (CAD $)</span>
                <span class="text-lg font-bold text-blue-600">{{ formatCurrency(totalBalanceCAD, 'CAD') }}</span>
              </div>
              <p class="text-xs text-black mt-1">All balances converted to CAD using current exchange rates</p>
            </div>
          </div>


          <!-- Error Display -->
          <div v-if="error" class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded">
            {{ error }}
          </div>

          <!-- Action Buttons -->
          <div class="flex justify-end space-x-3 pt-4">
            <button
              type="button"
              @click="closeModal"
              class="px-4 py-2 text-sm font-medium text-black bg-white border-2 border-black hover:bg-blue-50 focus:outline-none focus:ring-2 focus:ring-black">
              Cancel
            </button>
            <button
              type="submit"
              class="px-4 py-2 text-sm font-medium text-white bg-blue-600 border-2 border-blue-800 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500">
              Update Balances
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  isOpen: {
    type: Boolean,
    default: false
  },
  currentBalance: {
    type: Number,
    default: 0
  }
});

const emit = defineEmits(['close', 'update']);

// State
const loading = ref(false);
const error = ref('');

// Balance storage
const balances = ref({
  rbc_cad: 0,
  td_cad: 0,
  millennium_eur: 0,
});

const wiseBalances = ref({
  jennie: [],
  henry: []
});

// Exchange rates - updated from Wise API for accurate conversions
const exchangeRates = ref({
  CAD: 1.0,
  EUR: 1.45, // Will be updated from Wise API
  USD: 1.35, // Fallback rate  
  GBP: 1.65  // Fallback rate
});

// Fetch real-time exchange rates for balance conversions
const fetchBalanceExchangeRates = async () => {
  try {
    // Fetch EUR to CAD rate (most common conversion)
    const eurResponse = await fetch('/api/wise/exchange-rates?source=EUR&target=CAD');
    if (eurResponse.ok) {
      const eurData = await eurResponse.json();
      exchangeRates.value.EUR = eurData.rate;
    }
  } catch (error) {
    console.error('Failed to update exchange rates for balances:', error);
    // Keep using fallback rates
  }
};

// Timestamps for last updates
const lastWiseFetch = ref(null);
const lastManualUpdate = ref(null);

// Watch for modal open to load saved data
watch(() => props.isOpen, (newVal) => {
  if (newVal) {
    loadSavedBalances();
    fetchBalanceExchangeRates(); // Update exchange rates when modal opens
  }
});

// Load saved balances from MongoDB
const loadSavedBalances = async () => {
  try {
    const response = await fetch('/api/balances');
    if (response.ok) {
      const data = await response.json();
      
      if (data.accountBalances) {
        // Pre-fill with previous values
        balances.value = { 
          rbc_cad: data.accountBalances.manual?.rbc_cad || 0,
          td_cad: data.accountBalances.manual?.td_cad || 0,
          millennium_eur: data.accountBalances.manual?.millennium_eur || 0,
        };
        wiseBalances.value = { 
          jennie: data.accountBalances.wise?.jennie || [],
          henry: data.accountBalances.wise?.henry || []
        };
        
        // Load timestamps
        lastWiseFetch.value = data.accountBalances.lastWiseFetch ? new Date(data.accountBalances.lastWiseFetch) : null;
        lastManualUpdate.value = data.accountBalances.lastManualUpdate ? new Date(data.accountBalances.lastManualUpdate) : null;
      }
    }
  } catch (error) {
    console.error('Failed to load balances from MongoDB:', error);
    // Fallback to localStorage
    if (typeof window !== 'undefined') {
      const saved = localStorage.getItem('account_balances');
      if (saved) {
        const parsed = JSON.parse(saved);
        balances.value = { 
          rbc_cad: parsed.manual?.rbc_cad || 0,
          td_cad: parsed.manual?.td_cad || 0,
          millennium_eur: parsed.manual?.millennium_eur || 0,
        };
        wiseBalances.value = { 
          jennie: parsed.wise?.jennie || [],
          henry: parsed.wise?.henry || []
        };
        lastWiseFetch.value = parsed.lastWiseFetch ? new Date(parsed.lastWiseFetch) : null;
        lastManualUpdate.value = parsed.lastManualUpdate ? new Date(parsed.lastManualUpdate) : null;
      }
    }
  }
};

// Save balances to MongoDB
const saveBalances = async () => {
  try {
    // Update manual update timestamp
    lastManualUpdate.value = new Date();
    
    const requestBody = {
      currentBalance: totalBalanceCAD.value,
      accountBalances: {
        manual: balances.value,
        wise: wiseBalances.value,
        lastWiseFetch: lastWiseFetch.value?.toISOString(),
        lastManualUpdate: lastManualUpdate.value.toISOString()
      }
    };
    
    
    const response = await fetch('/api/balances', {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(requestBody)
    });
    
    if (!response.ok) {
      throw new Error('Failed to save balances');
    }
    
    console.log("✅ Balances saved to MongoDB");
  } catch (error) {
    console.error('Failed to save balances to MongoDB:', error);
    // Fallback to localStorage
    if (typeof window !== 'undefined') {
      const data = {
        manual: balances.value,
        wise: wiseBalances.value,
        lastWiseFetch: lastWiseFetch.value?.toISOString(),
        lastManualUpdate: lastManualUpdate.value.toISOString()
      };
      localStorage.setItem('account_balances', JSON.stringify(data));
    }
  }
};

// Fetch Wise balances via API
const fetchWiseBalances = async () => {
  loading.value = true;
  error.value = '';
  
  try {
    // Fetch balances for both profiles
    const [jennieResponse, henryResponse] = await Promise.allSettled([
      fetch('/api/wise/jennie/balances'),
      fetch('/api/wise/henry/balances')
    ]);
    
    const newBalances = { jennie: [], henry: [] };
    
    // Handle Jennie's balances
    if (jennieResponse.status === 'fulfilled' && jennieResponse.value.ok) {
      newBalances.jennie = await jennieResponse.value.json();
    } else {
      console.warn('Failed to fetch Jennie Wise balances:', jennieResponse.reason || jennieResponse.value.statusText);
    }
    
    // Handle Henry's balances
    if (henryResponse.status === 'fulfilled' && henryResponse.value.ok) {
      newBalances.henry = await henryResponse.value.json();
    } else {
      console.warn('Failed to fetch Henry Wise balances:', henryResponse.reason || henryResponse.value.statusText);
    }
    
    wiseBalances.value = newBalances;
    
    // Update Wise fetch timestamp if successful
    if (newBalances.jennie.length > 0 || newBalances.henry.length > 0) {
      lastWiseFetch.value = new Date();
    }
    
    // Show error only if both failed
    if (!newBalances.jennie.length && !newBalances.henry.length) {
      error.value = 'Failed to fetch Wise balances. Please check your API tokens or try again later.';
    }
    
  } catch (err) {
    error.value = 'Failed to fetch Wise balances. Please try again or enter manually.';
    console.error('Wise API error:', err);
  } finally {
    loading.value = false;
  }
};

// Calculate total balance in CAD
const totalBalanceCAD = computed(() => {
  let total = 0;
  
  // Manual balances - Personal accounts
  total += balances.value.rbc_cad || 0;
  total += balances.value.td_cad || 0;
  total += (balances.value.millennium_eur || 0) * exchangeRates.value.EUR;
  
  
  // Wise balances
  [...(wiseBalances.value.jennie || []), ...(wiseBalances.value.henry || [])].forEach(balance => {
    const rate = exchangeRates.value[balance.currency] || 1;
    total += balance.value.value * rate;
  });
  
  return total;
});

// Format currency
const formatCurrency = (amount, currency = 'CAD') => {
  const symbols = {
    CAD: '$',
    USD: '$',
    EUR: '€',
    GBP: '£'
  };
  
  return new Intl.NumberFormat('en-CA', {
    style: 'currency',
    currency: currency,
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  }).format(amount || 0);
};

// Format timestamp
const formatTimestamp = (date) => {
  if (!date) return '';
  
  const now = new Date();
  const diff = now - date;
  const minutes = Math.floor(diff / (1000 * 60));
  const hours = Math.floor(diff / (1000 * 60 * 60));
  const days = Math.floor(diff / (1000 * 60 * 60 * 24));
  
  if (minutes < 1) return 'Just now';
  if (minutes < 60) return `${minutes}m ago`;
  if (hours < 24) return `${hours}h ago`;
  if (days < 7) return `${days}d ago`;
  
  return new Intl.DateTimeFormat('en-CA', {
    month: 'short',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit'
  }).format(date);
};

// Close modal
const closeModal = () => {
  error.value = '';
  emit('close');
};

// Update balances
const updateBalances = async () => {
  await saveBalances();
  emit('update', totalBalanceCAD.value);
  closeModal();
};
</script>