import { useState } from 'react';
import { View, Text, Pressable, StyleSheet } from 'react-native';
import { LinearGradient } from 'expo-linear-gradient';
import Ionicons from '@expo/vector-icons/Ionicons';
import { colors, spacing, radii, typography, gradients } from '../../../theme';
import { DieticianReport } from '../../../api/dieticianReport';

type Props = {
  report: DieticianReport;
  onOpenFull: () => void;
};

// Carte "Débrief d'hier" (maquette Claude Design "Refonte ClemAI débrief sport") :
// divulgation progressive en 3 niveaux — phrase de résumé IA toujours visible,
// un clic sur la flèche déplie un résumé de ~4 lignes, puis "Voir le débrief
// complet" ouvre DieticianReportScreen (`content.blocks`, rendu via BilanBlocks).
export function DebriefCard({ report, onOpenFull }: Props) {
  const [open, setOpen] = useState(false);
  const { resume, detail } = report.content;

  if (!resume) return null;

  return (
    <View style={styles.card}>
      <Pressable style={styles.header} onPress={() => setOpen((o) => !o)} hitSlop={4}>
        <LinearGradient colors={gradients.hero} start={{ x: 0, y: 0 }} end={{ x: 1, y: 1 }} style={styles.badge}>
          <Ionicons name="sparkles" size={17} color="#fff" />
        </LinearGradient>
        <View style={styles.headerText}>
          <Text style={styles.label}>Débrief d'hier</Text>
          <Text style={styles.resume}>{resume}</Text>
        </View>
        <Ionicons
          name="chevron-down"
          size={16}
          color={colors.textMuted}
          style={open ? styles.chevronOpen : undefined}
        />
      </Pressable>

      {open && (
        <View style={styles.detailWrap}>
          {!!detail && <Text style={styles.detailText}>{detail}</Text>}
          <Pressable style={styles.fullButton} onPress={onOpenFull}>
            <Text style={styles.fullButtonText}>Voir le débrief complet</Text>
          </Pressable>
        </View>
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  card: {
    backgroundColor: colors.card,
    borderRadius: radii.lg,
    padding: spacing.md,
    borderWidth: 1,
    borderColor: colors.primary + '22',
  },
  header: { flexDirection: 'row', alignItems: 'flex-start', gap: 11 },
  badge: { width: 38, height: 38, borderRadius: radii.md, alignItems: 'center', justifyContent: 'center' },
  headerText: { flex: 1, minWidth: 0 },
  label: { fontSize: 11.5, fontWeight: '800', color: colors.primary, letterSpacing: 0.3, textTransform: 'uppercase' },
  resume: { ...typography.body, color: colors.text, marginTop: 2, lineHeight: 20 },
  chevronOpen: { transform: [{ rotate: '180deg' }] },
  detailWrap: {
    marginTop: spacing.sm,
    paddingTop: spacing.sm,
    borderTopWidth: 1,
    borderTopColor: colors.border,
    gap: spacing.sm,
  },
  detailText: { ...typography.caption, color: colors.textMuted, lineHeight: 20 },
  fullButton: { backgroundColor: colors.primary + '14', borderRadius: radii.md, paddingVertical: 11, alignItems: 'center' },
  fullButtonText: { fontSize: 14, fontWeight: '700', color: colors.primary },
});
