'use client';

import type { ReportSynthesis } from '../types';
import type { ReportLocale } from '../i18n/useReportLocale';
import { useReportLocale } from '../i18n/useReportLocale';
import ReportPage from '../components/ReportPage';
import SectionHeader from '../components/SectionHeader';
import ThemeMatrix from '../charts/ThemeMatrix';
import IntensityHeatmap from '../charts/IntensityHeatmap';
import MomentumDual from '../charts/MomentumDual';
import { getDomainColor } from '../styles/report-theme';
import { translateThemeLabel } from '../i18n/contentTranslations';

interface ThemeAnalysisProps {
  report: ReportSynthesis;
  locale?: ReportLocale;
  pageNumber?: number;
  totalPages?: number;
}

export default function ThemeAnalysis({ report, locale = 'en', pageNumber = 4, totalPages = 9 }: ThemeAnalysisProps) {
  const { t } = useReportLocale(locale);
  const themes = report.themes;

  if (themes.length === 0) return null;

  const negativeThemes = [...themes]
    .filter(t => t.valence.negative > 0)
    .sort((a, b) => b.valence.negative * b.weight - a.valence.negative * a.weight)
    .slice(0, 4);

  const positiveThemes = [...themes]
    .filter(t => t.valence.positive > 0)
    .sort((a, b) => b.valence.positive * b.weight - a.valence.positive * a.weight)
    .slice(0, 4);

  return (
    <ReportPage pageNumber={pageNumber} totalPages={totalPages} background="white">
      <SectionHeader
        number={3}
        title={t('theme_analysis')}
        subtitle={`${themes.length} ${t('theme_analysis_lead')}`}
      />

      {/* Theme Narrative */}
      {report.themes_narrative && (
        <p className="body-sm" style={{ marginBottom: 16, lineHeight: 1.6, color: 'var(--text-secondary)' }}>
          {report.themes_narrative}
        </p>
      )}

      {/* Theme Matrix Chart */}
      <div style={{ marginBottom: 20 }}>
        <h3 className="caption" style={{ marginBottom: 10 }}>{t('frequency_vs_sentiment')}</h3>
        {report.matrix_narrative && (
          <p className="body-sm" style={{ marginBottom: 10, lineHeight: 1.5, color: 'var(--text-secondary)', fontSize: 12 }}>
            {report.matrix_narrative}
          </p>
        )}
        <ThemeMatrix themes={themes} locale={locale} />
      </div>

      {/* Two-column: Pain Points + What Customers Love */}
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 20, marginTop: 16 }}>
        {/* Negative */}
        {negativeThemes.length > 0 && (
          <div>
            <h3 className="caption" style={{ color: 'var(--ui-error)', marginBottom: 10 }}>{t('pain_points')}</h3>
            {negativeThemes.map((theme) => (
              <div key={theme.primitive} style={{ display: 'flex', gap: 10, padding: '10px 12px', background: '#FEF2F2', borderRadius: 8, marginBottom: 8 }}>
                <span className="domain-dot" style={{ backgroundColor: getDomainColor(theme.domain), marginTop: 5 }} />
                <div style={{ flex: 1 }}>
                  <div className="heading-md" style={{ fontSize: 13 }}>{translateThemeLabel(theme.label, locale)}</div>
                  <div className="body-sm" style={{ color: 'var(--ui-error)', fontWeight: 600, fontSize: 11 }}>
                    {theme.valence.negative} {t('complaints')}
                  </div>
                  {theme.top_quotes.negative.length > 0 && (
                    <div className="evidence-quote red" style={{ fontSize: 11, margin: '6px 0 0', padding: '4px 10px' }}>
                      &ldquo;{theme.top_quotes.negative[0]}&rdquo;
                    </div>
                  )}
                </div>
              </div>
            ))}
          </div>
        )}

        {/* Positive */}
        {positiveThemes.length > 0 && (
          <div>
            <h3 className="caption" style={{ color: 'var(--ui-success)', marginBottom: 10 }}>{t('what_customers_love')}</h3>
            {positiveThemes.map((theme) => (
              <div key={theme.primitive} style={{ display: 'flex', gap: 10, padding: '10px 12px', background: '#F0FDF4', borderRadius: 8, marginBottom: 8 }}>
                <span className="domain-dot" style={{ backgroundColor: getDomainColor(theme.domain), marginTop: 5 }} />
                <div style={{ flex: 1 }}>
                  <div className="heading-md" style={{ fontSize: 13 }}>{translateThemeLabel(theme.label, locale)}</div>
                  <div className="body-sm" style={{ color: 'var(--ui-success)', fontWeight: 600, fontSize: 11 }}>
                    {theme.valence.positive} {t('mentions')}
                  </div>
                  {theme.top_quotes.positive.length > 0 && (
                    <div className="evidence-quote green" style={{ fontSize: 11, margin: '6px 0 0', padding: '4px 10px' }}>
                      &ldquo;{theme.top_quotes.positive[0]}&rdquo;
                    </div>
                  )}
                </div>
              </div>
            ))}
          </div>
        )}
      </div>
    </ReportPage>
  );
}
