import {
  View, Text, ScrollView, TouchableOpacity, Linking, ActivityIndicator, StyleSheet, Platform
} from 'react-native'
import { SafeAreaView } from 'react-native-safe-area-context'
import { useLocalSearchParams, useRouter, Stack } from 'expo-router'
import { Ionicons } from '@expo/vector-icons'
import { useStelleDetail } from '@/hooks/useStellen'
import { Button } from '@/components/ui/Button'

const SPARTE_COLOR: Record<string, string> = {
  elektro: '#1D4ED8', sanitär: '#0E7490', it: '#7C3AED',
  info: '#7C3AED', heizung: '#D97706', maler: '#059669',
}
function getSparteColor(sparte: string): string {
  const lower = sparte.toLowerCase()
  for (const [k, v] of Object.entries(SPARTE_COLOR)) {
    if (lower.includes(k)) return v
  }
  return '#003B7E'
}

export default function StelleDetailScreen() {
  const { id } = useLocalSearchParams<{ id: string }>()
  const router = useRouter()
  const { data: stelle, isLoading } = useStelleDetail(id)

  if (isLoading) {
    return (
      <View style={styles.loadingContainer}>
        <ActivityIndicator size="large" color="#003B7E" />
      </View>
    )
  }
  if (!stelle) return null

  const color = getSparteColor(stelle.sparte)
  const initial = stelle.sparte.charAt(0).toUpperCase()
  const bewerbungsUrl = `mailto:${stelle.kontaktEmail}?subject=${encodeURIComponent(`Bewerbung als Auszubildender bei ${stelle.member.betrieb}`)}`

  return (
    <>
      <Stack.Screen options={{ headerShown: false }} />
      <SafeAreaView style={styles.container} edges={['top']}>
        {/* Header */}
        <View style={styles.header}>
          <TouchableOpacity onPress={() => router.back()} style={styles.backButton}>
            <Ionicons name="arrow-back" size={24} color="#0F172A" />
          </TouchableOpacity>
          <View style={styles.headerSpacer} />
          <TouchableOpacity style={styles.shareButton}>
            <Ionicons name="share-outline" size={24} color="#0F172A" />
          </TouchableOpacity>
        </View>

        <ScrollView contentContainerStyle={styles.scrollContent} showsVerticalScrollIndicator={false}>

          {/* Header Card */}
          <View style={styles.heroCard}>
            <View style={[styles.logoBox, { backgroundColor: color + '15' }]}>
              <Text style={[styles.logoText, { color }]}>{initial}</Text>
            </View>
            <Text style={styles.jobTitle}>Auszubildender {stelle.sparte}</Text>
            <Text style={styles.companyName}>{stelle.member.betrieb}</Text>

            <View style={styles.locationBadge}>
              <Ionicons name="location-sharp" size={14} color="#64748B" />
              <Text style={styles.locationText}>
                {stelle.member.ort} · {stelle.org.name}
              </Text>
            </View>
          </View>

          {/* Key Facts */}
          <Text style={styles.sectionHeader}>Eckdaten</Text>
          <View style={styles.factsContainer}>
            <View style={styles.factItem}>
              <View style={styles.factIconBox}>
                <Ionicons name="people-outline" size={20} color="#003B7E" />
              </View>
              <View>
                <Text style={styles.factLabel}>Anzahl Stellen</Text>
                <Text style={styles.factValue}>{stelle.stellenAnz}</Text>
              </View>
            </View>

            {stelle.lehrjahr && (
              <View style={styles.factItem}>
                <View style={styles.factIconBox}>
                  <Ionicons name="school-outline" size={20} color="#003B7E" />
                </View>
                <View>
                  <Text style={styles.factLabel}>Lehrjahr</Text>
                  <Text style={styles.factValue}>{stelle.lehrjahr}</Text>
                </View>
              </View>
            )}

            {stelle.verguetung && (
              <View style={styles.factItem}>
                <View style={styles.factIconBox}>
                  <Ionicons name="cash-outline" size={20} color="#003B7E" />
                </View>
                <View>
                  <Text style={styles.factLabel}>Vergütung</Text>
                  <Text style={styles.factValue}>{stelle.verguetung}</Text>
                </View>
              </View>
            )}
          </View>

          {/* Description */}
          {stelle.beschreibung && (
            <View style={styles.section}>
              <Text style={styles.sectionHeader}>Beschreibung</Text>
              <Text style={styles.description}>{stelle.beschreibung}</Text>
            </View>
          )}

          {/* Contact */}
          {stelle.kontaktName && (
            <View style={styles.contactBox}>
              <Text style={styles.contactTitle}>Ansprechpartner</Text>
              <View style={styles.contactRow}>
                <View style={styles.avatarPlaceholder}>
                  <Text style={styles.avatarInitials}>{stelle.kontaktName.charAt(0)}</Text>
                </View>
                <View>
                  <Text style={styles.contactName}>{stelle.kontaktName}</Text>
                  <Text style={styles.contactRole}>Recruiting</Text>
                </View>
              </View>
            </View>
          )}

        </ScrollView>

        {/* Footer */}
        <View style={styles.footer}>
          <Button label="Jetzt bewerben" onPress={() => Linking.openURL(bewerbungsUrl)} />
        </View>

      </SafeAreaView>
    </>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F8FAFC',
  },
  loadingContainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  header: {
    paddingHorizontal: 16,
    paddingVertical: 12,
    flexDirection: 'row',
    alignItems: 'center',
    backgroundColor: '#F8FAFC',
  },
  backButton: {
    padding: 8,
    borderRadius: 12,
    backgroundColor: '#FFFFFF',
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 1 },
    shadowOpacity: 0.05,
    shadowRadius: 2,
    elevation: 1,
  },
  headerSpacer: {
    flex: 1,
  },
  shareButton: {
    padding: 8,
  },
  scrollContent: {
    padding: 24,
    paddingBottom: 40,
  },
  heroCard: {
    alignItems: 'center',
    marginBottom: 32,
  },
  logoBox: {
    width: 80,
    height: 80,
    borderRadius: 24,
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: 16,
  },
  logoText: {
    fontSize: 32,
    fontWeight: '800',
  },
  jobTitle: {
    fontSize: 24,
    fontWeight: '800',
    color: '#0F172A',
    textAlign: 'center',
    marginBottom: 8,
    letterSpacing: -0.5,
  },
  companyName: {
    fontSize: 16,
    fontWeight: '600',
    color: '#64748B',
    marginBottom: 16,
  },
  locationBadge: {
    flexDirection: 'row',
    alignItems: 'center',
    backgroundColor: '#F1F5F9',
    paddingHorizontal: 12,
    paddingVertical: 6,
    borderRadius: 99,
    gap: 6,
  },
  locationText: {
    fontSize: 13,
    color: '#475569',
    fontWeight: '500',
  },
  sectionHeader: {
    fontSize: 18,
    fontWeight: '700',
    color: '#0F172A',
    marginBottom: 16,
  },
  factsContainer: {
    flexDirection: 'row',
    flexWrap: 'wrap',
    gap: 12,
    marginBottom: 32,
  },
  factItem: {
    flex: 1,
    minWidth: '45%',
    backgroundColor: '#FFFFFF',
    padding: 16,
    borderRadius: 16,
    flexDirection: 'row',
    alignItems: 'center',
    gap: 12,
    shadowColor: '#64748B',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.03,
    shadowRadius: 8,
    elevation: 1,
  },
  factIconBox: {
    width: 36,
    height: 36,
    borderRadius: 10,
    backgroundColor: '#EFF6FF',
    justifyContent: 'center',
    alignItems: 'center',
  },
  factLabel: {
    fontSize: 11,
    color: '#94A3B8',
    fontWeight: '600',
    textTransform: 'uppercase',
    marginBottom: 2,
  },
  factValue: {
    fontSize: 14,
    fontWeight: '700',
    color: '#0F172A',
  },
  section: {
    marginBottom: 32,
  },
  description: {
    fontSize: 16,
    lineHeight: 26,
    color: '#334155',
  },
  contactBox: {
    backgroundColor: '#FFFFFF',
    padding: 20,
    borderRadius: 20,
    marginBottom: 32,
  },
  contactTitle: {
    fontSize: 14,
    fontWeight: '700',
    color: '#94A3B8',
    textTransform: 'uppercase',
    marginBottom: 16,
    letterSpacing: 0.5,
  },
  contactRow: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: 12,
  },
  avatarPlaceholder: {
    width: 48,
    height: 48,
    borderRadius: 24,
    backgroundColor: '#F1F5F9',
    justifyContent: 'center',
    alignItems: 'center',
  },
  avatarInitials: {
    fontSize: 18,
    fontWeight: '700',
    color: '#64748B',
  },
  contactName: {
    fontSize: 16,
    fontWeight: '700',
    color: '#0F172A',
  },
  contactRole: {
    fontSize: 13,
    color: '#64748B',
  },
  footer: {
    position: 'absolute',
    bottom: 0,
    left: 0,
    right: 0,
    backgroundColor: '#FFFFFF',
    padding: 20,
    paddingBottom: Platform.OS === 'ios' ? 32 : 20,
    borderTopWidth: 1,
    borderTopColor: '#F1F5F9',
  },
})
