import React from 'react';
import { StyleSheet, ScrollView } from 'react-native';
import { View, Text, useTheme } from '@bettoredge/styles';
import { Ionicons } from '@expo/vector-icons';
import type { CalcuttaCompetitionProps } from '@bettoredge/types';
import { formatCurrency } from '../helpers/formatting';

export interface AuctionInfoChipsProps {
    competition: CalcuttaCompetitionProps;
}

interface ChipData {
    icon: string;
    text: string;
}

const getChips = (comp: CalcuttaCompetitionProps): ChipData[] => {
    const chips: ChipData[] = [];
    const type = comp.auction_type;

    if (type === 'live') {
        chips.push({ icon: 'flash-outline', text: 'Live Auction' });
        if (comp.item_timer_seconds) {
            chips.push({ icon: 'timer-outline', text: `${comp.item_timer_seconds}s per item` });
        }
        if (comp.bid_extension_seconds) {
            chips.push({ icon: 'expand-outline', text: `${comp.bid_extension_seconds}s extension` });
        }
    } else if (type === 'sealed_bid') {
        chips.push({ icon: 'lock-closed-outline', text: 'Sealed Bids' });
    } else if (type === 'sweepstakes') {
        chips.push({ icon: 'shuffle-outline', text: 'Random Draw' });
    }

    if (type !== 'sweepstakes') {
        if (comp.min_bid && Number(comp.min_bid) > 0) {
            chips.push({ icon: 'cash-outline', text: `${formatCurrency(Number(comp.min_bid), comp.market_type)} min bid` });
        }
        if (comp.bid_increment && Number(comp.bid_increment) > 0) {
            chips.push({ icon: 'trending-up-outline', text: `${formatCurrency(Number(comp.bid_increment), comp.market_type)} increments` });
        }
        if (comp.max_escrow && Number(comp.max_escrow) > 0) {
            chips.push({ icon: 'wallet-outline', text: `${formatCurrency(Number(comp.max_escrow), comp.market_type)} budget cap` });
        }
    }

    if (comp.max_participants && Number(comp.max_participants) > 0) {
        chips.push({ icon: 'people-outline', text: `${comp.max_participants} spots` });
    }

    const entryFee = Number(comp.entry_fee) || 0;
    if (entryFee === 0) {
        chips.push({ icon: 'gift-outline', text: 'Free entry' });
    }

    return chips;
};

export const AuctionInfoChips: React.FC<AuctionInfoChipsProps> = ({ competition }) => {
    const { theme } = useTheme();
    const chips = getChips(competition);

    if (chips.length === 0) return null;

    return (
        <ScrollView
            horizontal
            showsHorizontalScrollIndicator={false}
            style={styles.scroll}
            contentContainerStyle={styles.scrollContent}
        >
            {chips.map((chip, i) => (
                <View
                    key={i}
                    variant="transparent"
                    style={[styles.chip, { backgroundColor: theme.colors.surface.elevated, borderColor: theme.colors.border.subtle }]}
                >
                    <Ionicons name={chip.icon as any} size={13} color={theme.colors.text.secondary} />
                    <Text variant="caption" color="secondary" style={styles.chipText}>{chip.text}</Text>
                </View>
            ))}
        </ScrollView>
    );
};

const styles = StyleSheet.create({
    scroll: {
        marginTop: 10,
    },
    scrollContent: {
        gap: 6,
        flexDirection: 'row',
    },
    chip: {
        flexDirection: 'row',
        alignItems: 'center',
        paddingHorizontal: 10,
        paddingVertical: 5,
        borderRadius: 20,
        borderWidth: 1,
    },
    chipText: {
        marginLeft: 4,
    },
});
