
import React from 'react';
import type { ReportData, ComplianceItem } from '../types';
import { CheckCircleIcon, XCircleIcon, InfoIcon } from './Icons';

interface ReportDisplayProps {
  report: ReportData;
}

const statusClass = (status: 'OK' | 'FALHA' | 'CONFORME' | 'NÃO CONFORME') => {
  switch (status) {
    case 'OK':
    case 'CONFORME':
      return 'text-emerald-600 bg-emerald-50 dark:bg-emerald-900/20 dark:text-emerald-400 border border-emerald-200 dark:border-emerald-800';
    case 'FALHA':
    case 'NÃO CONFORME':
      return 'text-red-600 bg-red-50 dark:bg-red-900/20 dark:text-red-400 border border-red-200 dark:border-red-800';
    default:
      return 'text-slate-600 bg-slate-50 dark:bg-slate-800/50 dark:text-slate-400 border border-slate-200 dark:border-slate-700';
  }
};

const statusIcon = (status: 'OK' | 'FALHA') => {
  return status === 'OK'
    ? <CheckCircleIcon className="w-4 h-4" />
    : <XCircleIcon className="w-4 h-4" />;
};


const ComplianceTableRow: React.FC<{ item: ComplianceItem; headerLabel: string }> = ({ item, headerLabel }) => (
  <tr className="block md:table-row mb-4 md:mb-0 bg-white/50 dark:bg-slate-800/50 md:bg-transparent rounded-xl md:rounded-none shadow-sm md:shadow-none border border-slate-100 dark:border-slate-700 md:border-b md:border-slate-200/50 md:dark:border-slate-700/50 last:border-0 hover:bg-white/80 dark:hover:bg-slate-800/80 transition-colors">
    <td className="p-4 md:py-4 md:px-6 flex justify-between items-center border-b border-slate-100 dark:border-slate-700 md:border-0 md:table-cell">
      <span className="text-xs font-bold uppercase tracking-wider text-slate-500 dark:text-slate-400 md:hidden">{headerLabel}</span>
      <span className="font-medium text-slate-700 dark:text-slate-300">{item.property || item.element || item.test}</span>
    </td>
    <td className="p-4 md:py-4 md:px-6 flex justify-between items-center border-b border-slate-100 dark:border-slate-700 md:border-0 md:table-cell">
      <span className="text-xs font-bold uppercase tracking-wider text-slate-500 dark:text-slate-400 md:hidden">Norma</span>
      <span className="text-slate-500 dark:text-slate-400 text-sm font-mono">{item.norm}</span>
    </td>
    <td className="p-4 md:py-4 md:px-6 flex justify-between items-center border-b border-slate-100 dark:border-slate-700 md:border-0 md:table-cell">
      <span className="text-xs font-bold uppercase tracking-wider text-slate-500 dark:text-slate-400 md:hidden">Certificado</span>
      <span className="text-slate-800 dark:text-slate-200 font-semibold font-mono">{item.certificate}</span>
    </td>
    <td className="p-4 md:py-4 md:px-6 flex justify-between items-center md:table-cell">
      <span className="text-xs font-bold uppercase tracking-wider text-slate-500 dark:text-slate-400 md:hidden">Status</span>
      <span className={`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-bold uppercase tracking-wide ${statusClass(item.status)}`}>
        {statusIcon(item.status)}
        {item.status}
      </span>
    </td>
  </tr>
);

export const ReportDisplay: React.FC<ReportDisplayProps> = ({ report }) => {
  const { identification, compliance, overPerformance, equivalents, confidence } = report;

  return (
    <div className="space-y-8 animate-fade-in pb-12">
      <div className="text-center space-y-2">
        <h2 className="text-3xl md:text-4xl font-display font-bold text-slate-900 dark:text-white">Relatório Técnico de Qualidade</h2>
        <p className="text-slate-500 dark:text-slate-400">Análise de conformidade normativa assistida por IA</p>
      </div>

      <div className="bg-white/70 dark:bg-slate-800/60 backdrop-blur-md border border-white/20 dark:border-slate-700/50 shadow-xl p-8 rounded-2xl text-center relative overflow-hidden">
        <div className="absolute top-0 left-0 w-full h-1 bg-gradient-to-r from-blue-500 via-indigo-500 to-purple-500"></div>
        <h3 className="text-lg font-medium text-slate-500 dark:text-slate-400 mb-2 uppercase tracking-wide">Grau de Confiança da Análise</h3>
        <div className="flex justify-center items-end gap-2">
          <span className="text-6xl sm:text-7xl font-display font-bold bg-clip-text text-transparent bg-gradient-to-br from-blue-600 to-indigo-600 dark:from-blue-400 dark:to-indigo-400">{confidence}</span>
          <span className="text-3xl font-bold text-slate-400 mb-2">%</span>
        </div>
      </div>

      {/* Identification Section */}
      <div className="bg-white/70 dark:bg-slate-800/60 backdrop-blur-md border border-white/20 dark:border-slate-700/50 shadow-xl rounded-2xl overflow-hidden">
        <div className="px-6 py-4 bg-slate-50/50 dark:bg-slate-800/50 border-b border-slate-200/50 dark:border-slate-700/50 backdrop-blur-sm">
          <h3 className="text-xl font-display font-bold text-slate-800 dark:text-white">1. Dados de Identificação</h3>
        </div>
        <div className="p-6">
          <ul className="grid grid-cols-1 md:grid-cols-2 gap-x-8 gap-y-4">
            {Object.entries(identification).map(([key, value]) => (
              <li key={key} className="flex flex-col border-b border-slate-100 dark:border-slate-800/50 pb-3 last:border-0 last:pb-0">
                <span className="text-xs font-bold text-slate-400 uppercase tracking-wider mb-1">{key.replace(/([A-Z])/g, ' $1')}</span>
                <span className="text-lg font-medium text-slate-800 dark:text-slate-200">{value}</span>
              </li>
            ))}
          </ul>
        </div>
      </div>

      {/* Compliance Section */}
      <div className="bg-white/70 dark:bg-slate-800/60 backdrop-blur-md border border-white/20 dark:border-slate-700/50 shadow-xl rounded-2xl overflow-hidden">
        <div className="px-6 py-4 bg-slate-50/50 dark:bg-slate-800/50 border-b border-slate-200/50 dark:border-slate-700/50 backdrop-blur-sm flex flex-col md:flex-row md:items-center justify-between gap-4">
          <h3 className="text-xl font-display font-bold text-slate-800 dark:text-white">2. Verificação de Conformidade</h3>
          <div className={`px-4 py-1.5 rounded-full font-bold text-sm uppercase tracking-wide border shadow-sm ${statusClass(compliance.status)}`}>
            {compliance.status}
          </div>
        </div>

        <div className="p-6 space-y-8">
          <div>
            <h4 className="flex items-center gap-2 font-display font-semibold text-lg mb-4 text-slate-800 dark:text-slate-200">
              <span className="w-1.5 h-6 bg-blue-500 rounded-full"></span>
              Análise de Propriedades Mecânicas
            </h4>
            <div className="overflow-x-auto">
              <table className="w-full text-left border-collapse">
                <thead className="hidden md:table-header-group text-xs uppercase tracking-wider text-slate-500 dark:text-slate-400 border-b-2 border-slate-100 dark:border-slate-700">
                  <tr>
                    <th className="py-3 px-6 font-semibold">Propriedade</th>
                    <th className="py-3 px-6 font-semibold">Norma</th>
                    <th className="py-3 px-6 font-semibold">Certificado</th>
                    <th className="py-3 px-6 font-semibold w-32">Status</th>
                  </tr>
                </thead>
                <tbody className="md:divide-y md:divide-slate-100 dark:md:divide-slate-700/50">
                  {compliance.mechanical.map((item, i) => <ComplianceTableRow key={i} item={item} headerLabel="Propriedade" />)}
                </tbody>
              </table>
            </div>
          </div>

          <div>
            <h4 className="flex items-center gap-2 font-display font-semibold text-lg mb-4 text-slate-800 dark:text-slate-200">
              <span className="w-1.5 h-6 bg-purple-500 rounded-full"></span>
              Análise de Composição Química (%)
            </h4>
            <div className="overflow-x-auto">
              <table className="w-full text-left border-collapse">
                <thead className="hidden md:table-header-group text-xs uppercase tracking-wider text-slate-500 dark:text-slate-400 border-b-2 border-slate-100 dark:border-slate-700">
                  <tr>
                    <th className="py-3 px-6 font-semibold">Elemento</th>
                    <th className="py-3 px-6 font-semibold">Norma</th>
                    <th className="py-3 px-6 font-semibold">Certificado</th>
                    <th className="py-3 px-6 font-semibold w-32">Status</th>
                  </tr>
                </thead>
                <tbody className="md:divide-y md:divide-slate-100 dark:md:divide-slate-700/50">
                  {compliance.chemical.map((item, i) => <ComplianceTableRow key={i} item={item} headerLabel="Elemento" />)}
                </tbody>
              </table>
            </div>
          </div>
          {compliance.otherTests.length > 0 && (
            <div>
              <h4 className="flex items-center gap-2 font-display font-semibold text-lg mb-4 text-slate-800 dark:text-slate-200">
                <span className="w-1.5 h-6 bg-emerald-500 rounded-full"></span>
                Outros Testes
              </h4>
              <div className="overflow-x-auto">
                <table className="w-full text-left border-collapse">
                  <thead className="hidden md:table-header-group text-xs uppercase tracking-wider text-slate-500 dark:text-slate-400 border-b-2 border-slate-100 dark:border-slate-700">
                    <tr>
                      <th className="py-3 px-6 font-semibold">Teste</th>
                      <th className="py-3 px-6 font-semibold">Norma</th>
                      <th className="py-3 px-6 font-semibold">Certificado</th>
                      <th className="py-3 px-6 font-semibold w-32">Status</th>
                    </tr>
                  </thead>
                  <tbody className="md:divide-y md:divide-slate-100 dark:md:divide-slate-700/50">
                    {compliance.otherTests.map((item, i) => <ComplianceTableRow key={i} item={item} headerLabel="Teste" />)}
                  </tbody>
                </table>
              </div>
            </div>
          )}
        </div>
      </div>

      {/* Over-Performance Section */}
      {compliance.status === 'CONFORME' && overPerformance.length > 0 && (
        <div className="bg-white/70 dark:bg-slate-800/60 backdrop-blur-md border border-white/20 dark:border-slate-700/50 shadow-xl rounded-2xl overflow-hidden border-l-4 border-l-green-500">
          <div className="px-6 py-4 bg-green-50/50 dark:bg-green-900/10 border-b border-green-100 dark:border-green-900/30">
            <h3 className="text-xl font-display font-bold text-green-800 dark:text-green-300">3. Destaques de Desempenho</h3>
          </div>
          <div className="p-6">
            <p className="text-sm text-slate-600 dark:text-slate-400 mb-6">Este material excede os requisitos mínimos normativos nos seguintes aspectos:</p>
            <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
              {overPerformance.map((item, i) => (
                <div key={i} className="flex items-start gap-4 p-4 rounded-xl bg-white/50 dark:bg-slate-800/50 border border-green-100 dark:border-green-900/30 shadow-sm">
                  <div className="p-2 bg-green-100 dark:bg-green-900/30 rounded-full text-green-600 dark:text-green-400 shrink-0">
                    <CheckCircleIcon className="w-5 h-5" />
                  </div>
                  <div>
                    <span className="block font-semibold text-slate-800 dark:text-slate-200">{item.property}</span>
                    <span className="text-green-600 dark:text-green-400 text-sm font-medium">{item.value} superior ao mínimo.</span>
                  </div>
                </div>
              ))}
            </div>
          </div>
        </div>
      )}

      {/* Equivalents Section */}
      <div className="glass-panel rounded-2xl overflow-hidden">
        <div className="px-6 py-4 bg-slate-50/50 dark:bg-slate-800/50 border-b border-slate-200/50 dark:border-slate-700/50 backdrop-blur-sm">
          <h3 className="text-xl font-display font-bold text-slate-800 dark:text-white">4. Normas Equivalentes</h3>
        </div>
        <div className="p-6">
          <p className="text-sm text-slate-500 dark:text-slate-400 mb-6">Equivalências internacionais para <span className="font-semibold text-slate-700 dark:text-slate-300">{identification.standards}</span>:</p>
          <div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
            {equivalents.map((item, i) => (
              <div key={i} className="group p-4 rounded-xl bg-gradient-to-br from-slate-50 to-white dark:from-slate-800 dark:to-slate-800/50 border border-slate-200 dark:border-slate-700 shadow-sm hover:shadow-md transition-all hover:-translate-y-1">
                <p className="text-xs font-semibold text-slate-400 dark:text-slate-500 uppercase tracking-wider mb-1 group-hover:text-blue-500 transition-colors">{item.system}</p>
                <p className="font-bold text-slate-800 dark:text-slate-200 font-mono text-lg">{item.norm}</p>
              </div>
            ))}
          </div>
        </div>
      </div>
    </div>
  );
};
