import React from 'react';
import { View, Text, Image, StyleSheet, TouchableOpacity, Dimensions } from 'react-native';
import { Deal } from '@/models/Deal';
import { openAmazonDeal } from '@/utils/openAmazon';

interface DealCardProps {
  deal: Deal;
  compact?: boolean;
}

const { width } = Dimensions.get('window');

export function DealCard({ deal, compact = false }: DealCardProps) {
  const handlePress = () => {
    openAmazonDeal(deal);
  };

  return (
    <View style={[styles.card, compact && styles.cardCompact]}>
      <Image source={{ uri: deal.image_url }} style={styles.image} resizeMode="cover" />
      <View style={styles.content}>
        <Text style={styles.title} numberOfLines={2}>
          {deal.title}
        </Text>
        <View style={styles.priceRow}>
          <Text style={styles.originalPrice}>${deal.original_price.toFixed(2)}</Text>
          <Text style={styles.discountedPrice}>${deal.discounted_price.toFixed(2)}</Text>
          <View style={styles.discountBadge}>
            <Text style={styles.discountText}>{deal.discount_percentage}% OFF</Text>
          </View>
        </View>
        <TouchableOpacity style={styles.ctaButton} onPress={handlePress}>
          <Text style={styles.ctaText}>View on Amazon</Text>
        </TouchableOpacity>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  card: {
    backgroundColor: '#fff',
    borderRadius: 12,
    overflow: 'hidden',
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.1,
    shadowRadius: 8,
    elevation: 4,
    width: width - 40,
  },
  cardCompact: {
    width: (width - 60) / 2,
  },
  image: {
    width: '100%',
    height: 200,
    backgroundColor: '#f0f0f0',
  },
  content: {
    padding: 16,
  },
  title: {
    fontSize: 16,
    fontWeight: '600',
    marginBottom: 12,
    color: '#333',
  },
  priceRow: {
    flexDirection: 'row',
    alignItems: 'center',
    marginBottom: 12,
    gap: 8,
  },
  originalPrice: {
    fontSize: 14,
    color: '#999',
    textDecorationLine: 'line-through',
  },
  discountedPrice: {
    fontSize: 20,
    fontWeight: 'bold',
    color: '#e63946',
  },
  discountBadge: {
    backgroundColor: '#e63946',
    paddingHorizontal: 8,
    paddingVertical: 4,
    borderRadius: 4,
  },
  discountText: {
    color: '#fff',
    fontSize: 12,
    fontWeight: 'bold',
  },
  ctaButton: {
    backgroundColor: '#ff9500',
    paddingVertical: 12,
    borderRadius: 8,
    alignItems: 'center',
  },
  ctaText: {
    color: '#fff',
    fontSize: 16,
    fontWeight: '600',
  },
});
