import React from 'react';
import { View, Text, StyleSheet, TouchableOpacity, useWindowDimensions } from 'react-native';
import { useRouter } from 'expo-router';
import { Ionicons } from '@expo/vector-icons';
import { colors, spacing, borderRadius } from '../constants/theme';
import { StockBadge } from './StockBadge';
import { Medicine } from '../types';

const TABLET_MIN_WIDTH = 768;

interface MedicineCardProps {
  medicine: Medicine;
}

export function MedicineCard({ medicine }: MedicineCardProps) {
  const router = useRouter();
  const { width } = useWindowDimensions();
  const isTablet = width >= TABLET_MIN_WIDTH;

  const handlePress = () => {
    router.push(`/medicine/${medicine.nregistro}`);
  };

  return (
    <TouchableOpacity
      style={[styles.card, isTablet && styles.cardTablet]}
      onPress={handlePress}
      activeOpacity={0.7}
    >
      <View style={styles.header}>
        <Text style={[styles.name, isTablet && styles.nameTablet]} numberOfLines={2}>
          {medicine.nombre}
        </Text>
        <StockBadge stock={medicine.stock} />
      </View>

      <Text style={styles.principioActivo} numberOfLines={1}>
        {medicine.principioActivo}
      </Text>

      <View style={styles.footer}>
        <View style={styles.priceContainer}>
          <Ionicons name="pricetag" size={14} color={colors.textSecondary} />
          <Text style={[styles.price, isTablet && styles.priceTablet]}>
            {medicine.precio ? `${medicine.precio.toFixed(2)} €` : 'Sin precio'}
          </Text>
        </View>

        <View style={styles.labContainer}>
          <Ionicons name="business" size={14} color={colors.textSecondary} />
          <Text style={[styles.laboratorio, isTablet && styles.laboratorioTablet]} numberOfLines={1}>
            {medicine.laboratorio}
          </Text>
        </View>
      </View>
    </TouchableOpacity>
  );
}

const styles = StyleSheet.create({
  card: {
    backgroundColor: colors.card,
    borderRadius: borderRadius.lg,
    padding: spacing.md,
    marginHorizontal: spacing.lg,
    marginVertical: spacing.xs,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.06,
    shadowRadius: 8,
    elevation: 2,
  },
  cardTablet: {
    padding: spacing.lg,
    maxWidth: 700,
    alignSelf: 'center',
    width: '100%',
  },
  header: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'flex-start',
    marginBottom: spacing.xs,
  },
  name: {
    flex: 1,
    fontSize: 16,
    fontWeight: '600',
    color: colors.text,
    marginRight: spacing.sm,
  },
  nameTablet: {
    fontSize: 18,
  },
  principioActivo: {
    fontSize: 14,
    color: colors.textSecondary,
    marginBottom: spacing.sm,
  },
  footer: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
  },
  priceContainer: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  price: {
    fontSize: 14,
    fontWeight: '600',
    color: colors.primary,
    marginLeft: spacing.xs,
  },
  priceTablet: {
    fontSize: 16,
  },
  labContainer: {
    flexDirection: 'row',
    alignItems: 'center',
    flex: 1,
    justifyContent: 'flex-end',
  },
  laboratorio: {
    fontSize: 12,
    color: colors.textSecondary,
    marginLeft: spacing.xs,
    maxWidth: 120,
  },
  laboratorioTablet: {
    fontSize: 14,
    maxWidth: 200,
  },
});
