import { Fragment } from 'react';
import { Panel } from '@/components/ui/panel';
import { formatScaledNumber } from '@/lib/format';
import type { CompanyAnalysis } from '@/lib/types';

type CompanyProfileFactsTableProps = {
  analysis: CompanyAnalysis;
};

function factValue(value: string | null | undefined) {
  return value && value.trim().length > 0 ? value : 'n/a';
}

function employeeCountLabel(value: number | null) {
  return value === null ? 'n/a' : formatScaledNumber(value, { maximumFractionDigits: 1 });
}

export function CompanyProfileFactsTable(props: CompanyProfileFactsTableProps) {
  const items = [
    { label: 'Exchange', value: factValue(props.analysis.companyProfile.exchange) },
    { label: 'Industry', value: factValue(props.analysis.companyProfile.industry ?? props.analysis.company.sector) },
    { label: 'Country / state', value: factValue(props.analysis.companyProfile.country) },
    { label: 'Fiscal year end', value: factValue(props.analysis.companyProfile.fiscalYearEnd) },
    { label: 'Employees', value: employeeCountLabel(props.analysis.companyProfile.employeeCount) },
    { label: 'Website', value: factValue(props.analysis.companyProfile.website) },
    { label: 'Category', value: factValue(props.analysis.company.category) },
    { label: 'CIK', value: factValue(props.analysis.company.cik) }
  ];
  const rows = Array.from({ length: Math.ceil(items.length / 2) }, (_, index) => items.slice(index * 2, index * 2 + 2));

  return (
    <Panel
      title="Company profile facts"
      className="pt-2"
    >
      <div className="overflow-x-auto">
        <table className="w-full border-collapse table-fixed">
          <tbody>
            {rows.map((row) => (
              <tr key={row.map((item) => item.label).join('-')} className="border-t border-[color:var(--line-weak)]">
                {row.map((item) => (
                  <Fragment key={item.label}>
                    <th className="w-[18%] py-2 pr-3 text-left align-top text-[11px] font-medium uppercase tracking-[0.14em] text-[color:var(--terminal-muted)]">
                      {item.label}
                    </th>
                    <td className="w-[32%] py-2 pr-4 text-sm text-[color:var(--terminal-bright)]">
                      {item.label === 'Website' && item.value !== 'n/a' ? (
                        <a href={item.value} target="_blank" rel="noreferrer" className="text-[color:var(--accent)] hover:text-[color:var(--accent-strong)]">
                          {item.value}
                        </a>
                      ) : (
                        item.value
                      )}
                    </td>
                  </Fragment>
                ))}
                {row.length === 1 ? (
                  <>
                    <th className="w-[18%] py-2 pr-3" />
                    <td className="w-[32%] py-2 pr-4" />
                  </>
                ) : null}
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </Panel>
  );
}
