import React from 'react';
import { View, Text, StyleSheet, TouchableOpacity, Image, useWindowDimensions } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { useRouter } from 'expo-router';
import { useThemeContext } from '../../components/ThemeProvider';
import { spacing, borderRadius, shadows } from '../../constants/theme';

const TABLET_MIN_WIDTH = 768;

export default function HomeScreen() {
  const router = useRouter();
  const { width } = useWindowDimensions();
  const isTablet = width >= TABLET_MIN_WIDTH;
  const { colors } = useThemeContext();

  return (
    <View style={[styles.container, { backgroundColor: colors.background }]}>
      <View style={[styles.hero, isTablet && styles.heroTablet]}>
        <Image
          source={require('../../assets/farmaclic_logo.png')}
          style={[styles.logo, isTablet && styles.logoTablet]}
          resizeMode="contain"
        />
        <Text style={[styles.brandName, isTablet && styles.brandNameTablet, { color: colors.text }]}>FarmaClic</Text>
        <Text style={[styles.description, isTablet && styles.descriptionTablet, { color: colors.textSecondary }]}>
          Encuentra tus medicamentos en farmacias cercanas
        </Text>
      </View>

      <View style={[styles.cards, isTablet && styles.cardsTablet]}>
        <TouchableOpacity
          style={[styles.card, isTablet && styles.cardTablet, { backgroundColor: colors.primaryContainer }]}
          activeOpacity={0.85}
          onPress={() => router.push('/(tabs)/search')}
        >
          <View style={[styles.cardIcon, styles.cardIconSearch]}>
            <Ionicons name="search" size={isTablet ? 28 : 24} color={colors.onPrimaryContainer} />
          </View>
          <View style={styles.cardContent}>
            <Text style={[styles.cardLabel, isTablet && styles.cardLabelTablet, { color: colors.onPrimaryContainer }]}>Buscar Medicamento</Text>
            <Ionicons name="chevron-forward" size={20} color={colors.onPrimaryContainer} style={{ opacity: 0.7 }} />
          </View>
        </TouchableOpacity>

        <TouchableOpacity
          style={[styles.card, styles.cardScan, isTablet && styles.cardTablet]}
          activeOpacity={0.85}
          onPress={() => router.push('/scanner')}
        >
          <View style={[styles.cardIcon, styles.cardIconScan]}>
            <Ionicons name="scan" size={isTablet ? 28 : 24} color="#ffffff" />
          </View>
          <View style={styles.cardContent}>
            <Text style={[styles.cardLabel, styles.cardLabelScan, isTablet && styles.cardLabelTablet]}>Escanear TSI</Text>
            <Ionicons name="chevron-forward" size={20} color="#ffffff" style={{ opacity: 0.7 }} />
          </View>
        </TouchableOpacity>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    paddingHorizontal: spacing.lg,
    gap: spacing.lg,
  },
  hero: {
    alignItems: 'center',
    gap: spacing.sm,
    marginBottom: spacing.md,
  },
  heroTablet: {
    marginBottom: spacing.xl,
  },
  logo: {
    width: 120,
    height: 120,
    marginBottom: spacing.xs,
  },
  logoTablet: {
    width: 160,
    height: 160,
  },
  brandName: {
    fontSize: 28,
    fontWeight: 'bold',
    letterSpacing: -0.5,
  },
  brandNameTablet: {
    fontSize: 36,
  },
  description: {
    fontSize: 16,
    textAlign: 'center',
    maxWidth: 260,
    lineHeight: 24,
  },
  descriptionTablet: {
    fontSize: 18,
    maxWidth: 400,
    lineHeight: 28,
  },
  cards: {
    width: '100%',
    maxWidth: 320,
    gap: spacing.md,
  },
  cardsTablet: {
    maxWidth: 480,
  },
  card: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: spacing.md,
    borderRadius: borderRadius.lg,
    padding: spacing.md,
    minHeight: 64,
    ...shadows.card,
  },
  cardTablet: {
    padding: spacing.lg,
    minHeight: 72,
  },
  cardScan: {
    backgroundColor: '#2b5bb5',
  },
  cardIcon: {
    width: 48,
    height: 48,
    borderRadius: borderRadius.md,
    backgroundColor: 'rgba(255, 255, 255, 0.2)',
    alignItems: 'center',
    justifyContent: 'center',
    flexShrink: 0,
  },
  cardIconSearch: {
    backgroundColor: 'rgba(255, 255, 255, 0.3)',
  },
  cardIconScan: {
    backgroundColor: 'rgba(255, 255, 255, 0.2)',
  },
  cardContent: {
    flex: 1,
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
  },
  cardLabel: {
    fontSize: 18,
    fontWeight: '700',
    lineHeight: 24,
  },
  cardLabelTablet: {
    fontSize: 20,
  },
  cardLabelScan: {
    color: '#ffffff',
  },
});
