"""
This plugin provides a command to fetch the current Bitcoin price.
"""
import logging
import requests
import simplematrixbotlib as botlib
BITCOIN_API_URL = "https://api.bitcointicker.co/trades/bitstamp/btcusd/60/"
async def handle_command(room, message, bot, prefix, config):
    """
    Function to handle the !btc command.
    Args:
        room (Room): The Matrix room where the command was invoked.
        message (RoomMessage): The message object containing the command.
        bot (Bot): The bot object.
        prefix (str): The command prefix.
        config (dict): Configuration parameters.
    Returns:
        None
    """
    match = botlib.MessageMatch(room, message, bot, prefix)
    if match.is_not_from_this_bot() and match.prefix() and match.command("btc"):
        logging.info("Received !btc command")
        try:
            # Fetch Bitcoin price data
            headers = {
                'Accept-Encoding': 'gzip, deflate',
                'User-Agent': 'FunguyBot/1.0'
            }
            logging.info(f"Fetching Bitcoin price from {BITCOIN_API_URL}")
            response = requests.get(BITCOIN_API_URL, headers=headers, timeout=10)
            response.raise_for_status()
            data = response.json()
            if not data or len(data) == 0:
                await bot.api.send_text_message(
                    room.room_id,
                    "No Bitcoin price data available."
                )
                logging.warning("No Bitcoin price data returned from API")
                return
            # Get the most recent trade (last item in the array)
            latest_trade = data[-1]
            price = latest_trade.get('price')
            if price is None:
                await bot.api.send_text_message(
                    room.room_id,
                    "Could not extract Bitcoin price from API response."
                )
                logging.error("Price field not found in API response")
                return
            # Convert to float and format with commas
            try:
                price_float = float(price)
                price_formatted = f"${price_float:,.2f}"
            except (ValueError, TypeError):
                price_formatted = f"${price}"
            # Optional: Get additional info if available
            timestamp = latest_trade.get('timestamp', '')
            volume = latest_trade.get('volume', '')
            # Build the message
            message_text = f"₿ BTC/USD"
            message_text += f" Current Price: {price_formatted}"
            message_text += ", bitcointicker.co"
            await bot.api.send_markdown_message(room.room_id, message_text)
            logging.info(f"Sent Bitcoin price: {price_formatted}")
        except requests.exceptions.Timeout:
            await bot.api.send_text_message(
                room.room_id,
                "Request timed out. Bitcoin price API may be slow or unavailable."
            )
            logging.error("Bitcoin API timeout")
        except requests.exceptions.RequestException as e:
            await bot.api.send_text_message(
                room.room_id,
                f"Error fetching Bitcoin price: {e}"
            )
            logging.error(f"Error fetching Bitcoin price: {e}")
        except (KeyError, IndexError, ValueError) as e:
            await bot.api.send_text_message(
                room.room_id,
                "Error parsing Bitcoin price data."
            )
            logging.error(f"Error parsing Bitcoin API response: {e}", exc_info=True)
        except Exception as e:
            await bot.api.send_text_message(
                room.room_id,
                "An unexpected error occurred while fetching Bitcoin price."
            )
            logging.error(f"Unexpected error in Bitcoin plugin: {e}", exc_info=True)