/**
 * Billing Dashboard Page
 * 
 * Zeigt Guthaben, Statistiken und Transaktionen für den Benutzer.
 */

import React, { useState, useEffect, useMemo } from 'react';
import { useBilling, type BillingBalance, type UsageReport } from '../../hooks/useBilling';
import { BillingNav } from './BillingNav';
import styles from './Billing.module.css';

// ============================================================================
// BALANCE CARD COMPONENT
// ============================================================================

interface BalanceCardProps {
  balance: BillingBalance;
  onClick?: () => void;
}

const BalanceCard: React.FC<BalanceCardProps> = ({ balance, onClick }) => {
  const formatCurrency = (amount: number) => {
    return new Intl.NumberFormat('de-CH', {
      style: 'currency',
      currency: 'CHF'
    }).format(amount);
  };
  
  const getBillingModelLabel = (model: string) => {
    switch (model) {
      case 'PREPAY_MANDATE': return 'Prepaid (Mandant)';
      case 'PREPAY_USER': return 'Prepaid (Benutzer)';
      case 'CREDIT_POSTPAY': return 'Kredit';
      case 'UNLIMITED': return 'Unlimited';
      default: return model;
    }
  };
  
  return (
    <div 
      className={`${styles.balanceCard} ${balance.isWarning ? styles.warning : ''}`}
      onClick={onClick}
    >
      <div className={styles.balanceHeader}>
        <h3 className={styles.mandateName}>{balance.mandateName}</h3>
        <span className={styles.billingModel}>{getBillingModelLabel(balance.billingModel)}</span>
      </div>
      <div className={styles.balanceAmount}>
        {formatCurrency(balance.balance)}
      </div>
      {balance.isWarning && (
        <div className={styles.warningBadge}>
          Niedriges Guthaben
        </div>
      )}
    </div>
  );
};

// ============================================================================
// STATISTICS CHART COMPONENT
// ============================================================================

interface StatisticsChartProps {
  statistics: UsageReport | null;
  loading?: boolean;
}

const StatisticsChart: React.FC<StatisticsChartProps> = ({ statistics, loading }) => {
  const formatCurrency = (amount: number) => {
    return new Intl.NumberFormat('de-CH', {
      style: 'currency',
      currency: 'CHF'
    }).format(amount);
  };
  
  if (loading) {
    return <div className={styles.loadingPlaceholder}>Lade Statistiken...</div>;
  }
  
  if (!statistics) {
    return <div className={styles.noData}>Keine Statistiken verfügbar</div>;
  }
  
  // Calculate max cost for bar scaling
  const maxProviderCost = Math.max(...Object.values(statistics.costByProvider), 1);
  
  return (
    <div className={styles.statisticsChart}>
      <div className={styles.totalCost}>
        <span className={styles.totalLabel}>Gesamtkosten</span>
        <span className={styles.totalAmount}>{formatCurrency(statistics.totalCost)}</span>
      </div>
      
      <div className={styles.chartSection}>
        <h4>Kosten nach Anbieter</h4>
        {Object.entries(statistics.costByProvider).length === 0 ? (
          <div className={styles.noData}>Keine Daten</div>
        ) : (
          <div className={styles.barChart}>
            {Object.entries(statistics.costByProvider).map(([provider, cost]) => (
              <div key={provider} className={styles.barRow}>
                <span className={styles.barLabel}>{provider}</span>
                <div className={styles.barContainer}>
                  <div 
                    className={styles.bar}
                    style={{ width: `${(cost / maxProviderCost) * 100}%` }}
                  />
                </div>
                <span className={styles.barValue}>{formatCurrency(cost)}</span>
              </div>
            ))}
          </div>
        )}
      </div>
      
      <div className={styles.chartSection}>
        <h4>Kosten nach Modell</h4>
        {Object.entries(statistics.costByModel || {}).length === 0 ? (
          <div className={styles.noData}>Keine Daten</div>
        ) : (
          <div className={styles.barChart}>
            {Object.entries(statistics.costByModel || {}).map(([model, cost]) => (
              <div key={model} className={styles.barRow}>
                <span className={styles.barLabel}>{model}</span>
                <div className={styles.barContainer}>
                  <div 
                    className={styles.bar}
                    style={{ width: `${(cost / maxProviderCost) * 100}%` }}
                  />
                </div>
                <span className={styles.barValue}>{formatCurrency(cost)}</span>
              </div>
            ))}
          </div>
        )}
      </div>
      
      <div className={styles.chartSection}>
        <h4>Kosten nach Feature</h4>
        {Object.entries(statistics.costByFeature).length === 0 ? (
          <div className={styles.noData}>Keine Daten</div>
        ) : (
          <div className={styles.featureList}>
            {Object.entries(statistics.costByFeature).map(([feature, cost]) => (
              <div key={feature} className={styles.featureRow}>
                <span className={styles.featureLabel}>{feature}</span>
                <span className={styles.featureValue}>{formatCurrency(cost)}</span>
              </div>
            ))}
          </div>
        )}
      </div>
    </div>
  );
};

// ============================================================================
// MAIN COMPONENT
// ============================================================================

export const BillingDashboard: React.FC = () => {
  const { 
    balances, 
    statistics, 
    loading, 
    loadStatistics 
  } = useBilling();
  
  const [selectedPeriod, setSelectedPeriod] = useState<'month' | 'year'>('month');
  const [selectedYear, setSelectedYear] = useState(new Date().getFullYear());
  const [selectedMonth, setSelectedMonth] = useState(new Date().getMonth() + 1);
  
  // Load statistics when period changes
  useEffect(() => {
    if (selectedPeriod === 'month') {
      loadStatistics('month', selectedYear);
    } else {
      loadStatistics('year', selectedYear);
    }
  }, [selectedPeriod, selectedYear, loadStatistics]);
  
  // Available years (current and last 2 years)
  const availableYears = useMemo(() => {
    const current = new Date().getFullYear();
    return [current, current - 1, current - 2];
  }, []);
  
  // Available months
  const availableMonths = [
    { value: 1, label: 'Januar' },
    { value: 2, label: 'Februar' },
    { value: 3, label: 'März' },
    { value: 4, label: 'April' },
    { value: 5, label: 'Mai' },
    { value: 6, label: 'Juni' },
    { value: 7, label: 'Juli' },
    { value: 8, label: 'August' },
    { value: 9, label: 'September' },
    { value: 10, label: 'Oktober' },
    { value: 11, label: 'November' },
    { value: 12, label: 'Dezember' },
  ];
  
  return (
    <div className={styles.billingDashboard}>
      <header className={styles.pageHeader}>
        <h1>Billing</h1>
        <p className={styles.subtitle}>Übersicht über Guthaben und Nutzung</p>
      </header>
      
      <BillingNav />
      
      {/* Balance Cards */}
      <section className={styles.section}>
        <h2 className={styles.sectionTitle}>Guthaben</h2>
        {loading ? (
          <div className={styles.loadingPlaceholder}>Lade Guthaben...</div>
        ) : balances.length === 0 ? (
          <div className={styles.noData}>Keine Abrechnungskonten vorhanden</div>
        ) : (
          <div className={styles.balanceGrid}>
            {balances.map((balance) => (
              <BalanceCard key={balance.mandateId} balance={balance} />
            ))}
          </div>
        )}
      </section>
      
      {/* Statistics */}
      <section className={styles.section}>
        <div className={styles.sectionHeader}>
          <h2 className={styles.sectionTitle}>Nutzungsstatistik</h2>
          <div className={styles.periodSelector}>
            <select 
              value={selectedPeriod} 
              onChange={(e) => setSelectedPeriod(e.target.value as 'month' | 'year')}
              className={styles.select}
            >
              <option value="month">Monat</option>
              <option value="year">Jahr</option>
            </select>
            <select 
              value={selectedYear} 
              onChange={(e) => setSelectedYear(Number(e.target.value))}
              className={styles.select}
            >
              {availableYears.map((year) => (
                <option key={year} value={year}>{year}</option>
              ))}
            </select>
            {selectedPeriod === 'month' && (
              <select 
                value={selectedMonth} 
                onChange={(e) => setSelectedMonth(Number(e.target.value))}
                className={styles.select}
              >
                {availableMonths.map((month) => (
                  <option key={month.value} value={month.value}>{month.label}</option>
                ))}
              </select>
            )}
          </div>
        </div>
        <StatisticsChart statistics={statistics} loading={loading} />
      </section>
    </div>
  );
};

export default BillingDashboard;
