import React, { createContext, useState, useContext } from 'react';

const CurrencyContext = createContext();

// Base rate is IDR (Rupiah). 
// 1 GB = Rp. 1.500
// Rates against IDR (approximate for demonstration).
const RATES = {
  IDR: { rate: 1, symbol: "Rp", format: (val) => new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR' }).format(val) },
  USD: { rate: 1 / 16000, symbol: "$", format: (val) => new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(val) },
  CRYPTO: { rate: 1 / 1000000000, symbol: "₿", format: (val) => `${val.toFixed(8)} BTC` } // 1 BTC = 1B IDR
};

export function CurrencyProvider({ children }) {
  const [currency, setCurrency] = useState('IDR');

  const convertCost = (baseValueIDR) => {
    const c = RATES[currency];
    const converted = baseValueIDR * c.rate;
    return c.format(converted);
  };

  return (
    <CurrencyContext.Provider value={{ currency, setCurrency, convertCost }}>
      {children}
    </CurrencyContext.Provider>
  );
}

export function useCurrency() {
  return useContext(CurrencyContext);
}
