import { View, Text, StyleSheet, ScrollView } from 'react-native';
import { colors, spacing, radii, typography } from '../../../theme';
import { DieticianReportBlock } from '../../../api/dieticianReport';

const TONE_COLORS: Record<'positive' | 'warning' | 'highlight', string> = {
  positive: colors.success,
  warning: '#d97706',
  highlight: colors.primary,
};

// Rendu des blocs structurés renvoyés par `DieticianReportService::buildPrompt()`
// (schéma `blocks`) — maquette Claude Design "Bilan de journée ClemAI". Un type de
// bloc = un style fixe côté app (pas de parsing de mise en forme libre, contrairement
// à l'ancien champ texte `rapport`) : plus robuste, et permet le tableau récapitulatif
// que le Markdown ne gérait pas.
export function BilanBlocks({ blocks }: { blocks: DieticianReportBlock[] }) {
  return (
    <View style={styles.container}>
      {blocks.map((block, index) => {
        switch (block.type) {
          case 'intro':
            return (
              <View key={index} style={styles.intro}>
                {!!block.title && <Text style={styles.introTitle}>{block.title}</Text>}
                {!!block.body && <Text style={styles.paragraph}>{block.body}</Text>}
              </View>
            );

          case 'h2':
            return (
              <View key={index} style={styles.h2Row}>
                {!!block.emoji && <Text style={styles.h2Emoji}>{block.emoji}</Text>}
                <Text style={[styles.h2Text, { color: TONE_COLORS[block.tone] }]}>{block.text}</Text>
              </View>
            );

          case 'h3':
            return (
              <Text key={index} style={styles.h3}>
                {block.text}
              </Text>
            );

          case 'p':
            return (
              <Text key={index} style={styles.paragraph}>
                {block.text}
              </Text>
            );

          case 'list':
            return (
              <View key={index} style={styles.list}>
                {block.items.map((item, itemIndex) => (
                  <View key={itemIndex} style={styles.listRow}>
                    <Text style={styles.listBullet}>•</Text>
                    <Text style={styles.paragraph}>{item}</Text>
                  </View>
                ))}
              </View>
            );

          case 'callout':
            return (
              <View key={index} style={styles.callout}>
                <Text style={styles.calloutText}>{block.text}</Text>
              </View>
            );

          case 'table':
            return (
              <ScrollView key={index} horizontal showsHorizontalScrollIndicator={false} style={styles.tableScroll}>
                <View style={styles.table}>
                  <View style={styles.tableHeaderRow}>
                    {block.headers.map((header, colIndex) => (
                      <Text key={colIndex} style={styles.tableHeaderCell}>
                        {header}
                      </Text>
                    ))}
                  </View>
                  {block.rows.map((row, rowIndex) => (
                    <View key={rowIndex} style={styles.tableRow}>
                      {row.map((cell, colIndex) => (
                        <Text key={colIndex} style={[styles.tableCell, colIndex === 0 && styles.tableCellFirst]}>
                          {cell}
                        </Text>
                      ))}
                    </View>
                  ))}
                </View>
              </ScrollView>
            );

          default:
            return null;
        }
      })}
    </View>
  );
}

const TABLE_COL_WIDTH = 130;

const styles = StyleSheet.create({
  container: { gap: spacing.sm },
  intro: { gap: 6 },
  introTitle: { fontSize: 17, fontWeight: '800', color: colors.text, lineHeight: 23 },
  h2Row: { flexDirection: 'row', alignItems: 'center', gap: 8, marginTop: spacing.sm },
  h2Emoji: { fontSize: 16 },
  h2Text: { fontSize: 16, fontWeight: '800' },
  h3: { fontSize: 14.5, fontWeight: '800', color: colors.text, marginTop: 2 },
  paragraph: { ...typography.body, color: colors.text, lineHeight: 21 },
  list: { gap: 6 },
  listRow: { flexDirection: 'row', alignItems: 'flex-start', gap: 8 },
  listBullet: { color: colors.textMuted, fontSize: 15, lineHeight: 21 },
  callout: {
    backgroundColor: colors.primary + '10',
    borderLeftWidth: 3,
    borderLeftColor: colors.primary,
    borderRadius: radii.sm,
    paddingVertical: spacing.sm,
    paddingHorizontal: spacing.md,
  },
  calloutText: { ...typography.body, color: colors.text, lineHeight: 21 },
  tableScroll: { marginVertical: 2 },
  table: { borderWidth: 1, borderColor: colors.border, borderRadius: radii.md, overflow: 'hidden' },
  tableHeaderRow: { flexDirection: 'row', backgroundColor: colors.surface },
  tableHeaderCell: {
    width: TABLE_COL_WIDTH,
    paddingVertical: 8,
    paddingHorizontal: 12,
    fontSize: 11,
    fontWeight: '800',
    color: colors.textMuted,
    textTransform: 'uppercase',
    letterSpacing: 0.3,
  },
  tableRow: { flexDirection: 'row', borderTopWidth: 1, borderTopColor: colors.border },
  tableCell: { width: TABLE_COL_WIDTH, paddingVertical: 8, paddingHorizontal: 12, fontSize: 13.5, color: colors.text },
  tableCellFirst: { fontWeight: '700' },
});
