'use client';

import React from 'react';
import { t } from '@/i18n/locales';
import { useApi } from '@/lib/use-api';

/**
 * SEC Filing Management - SEC文件提交与管理
 */

interface SecFilingData {
  stats: { label: string; value: string; color: string }[];
  filings: { id: string; formType: string; title: string; filingDate: string; deadline: string; status: string; reviewer: string }[];
  timeline: { date: string; event: string; type: string; done: boolean }[];
  disclosureItems: { name: string; status: string; lastUpdated: string }[];
  disclosureProgress: number;
}

const loadingBox: React.CSSProperties = {
  display: 'flex', alignItems: 'center', justifyContent: 'center',
  padding: 40, color: 'var(--color-text-tertiary)', font: 'var(--text-body)',
};

const getFilingStatusStyle = (status: string) => {
  switch (status) {
    case 'passed':
      return { background: 'var(--color-success-light)', color: 'var(--color-success)' };
    case 'reviewing':
      return { background: 'var(--color-info-light)', color: 'var(--color-info)' };
    case 'submitted':
      return { background: 'var(--color-primary-light)', color: 'var(--color-primary)' };
    case 'needs_revision':
      return { background: 'var(--color-error-light)', color: 'var(--color-error)' };
    case 'pending':
      return { background: 'var(--color-gray-100)', color: 'var(--color-text-tertiary)' };
    default:
      return { background: 'var(--color-gray-100)', color: 'var(--color-text-tertiary)' };
  }
};

const getStatusLabel = (status: string) => {
  const map: Record<string, () => string> = {
    passed: () => t('sec_status_passed'),
    reviewing: () => t('sec_status_reviewing'),
    submitted: () => t('sec_status_submitted'),
    needs_revision: () => t('sec_status_needs_revision'),
    pending: () => t('sec_status_pending'),
  };
  return map[status]?.() ?? status;
};

export const SecFilingPage: React.FC = () => {
  const { data, isLoading, error } = useApi<SecFilingData>('/api/v1/admin/compliance/sec-filing');

  if (error) return <div style={loadingBox}>Error: {error.message}</div>;
  if (isLoading) return <div style={loadingBox}>Loading...</div>;

  const filingStats = data?.stats ?? [];
  const secFilings = data?.filings ?? [];
  const timelineEvents = data?.timeline ?? [];
  const disclosureItems = data?.disclosureItems ?? [];
  const disclosureProgress = data?.disclosureProgress ?? 0;

  return (
    <div>
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 24 }}>
        <h1 style={{ font: 'var(--text-h1)', color: 'var(--color-text-primary)' }}>{t('sec_title')}</h1>
        <button style={{
          padding: '8px 16px', border: 'none', borderRadius: 'var(--radius-full)',
          background: 'var(--color-primary)', color: 'white', cursor: 'pointer', font: 'var(--text-label-sm)',
        }}>{t('sec_new_filing')}</button>
      </div>

      {/* Stats */}
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 16, marginBottom: 24 }}>
        {filingStats.map(s => (
          <div key={s.label} style={{
            background: 'var(--color-surface)', borderRadius: 'var(--radius-md)',
            border: '1px solid var(--color-border-light)', padding: 20,
          }}>
            <div style={{ font: 'var(--text-body-sm)', color: 'var(--color-text-tertiary)', marginBottom: 8 }}>{s.label}</div>
            <div style={{ font: 'var(--text-h1)', color: s.color }}>{s.value}</div>
          </div>
        ))}
      </div>

      {/* Filing Table */}
      <div style={{
        background: 'var(--color-surface)', borderRadius: 'var(--radius-md)',
        border: '1px solid var(--color-border-light)', overflow: 'hidden', marginBottom: 24,
      }}>
        <div style={{ padding: '16px 20px', borderBottom: '1px solid var(--color-border-light)' }}>
          <h2 style={{ font: 'var(--text-h2)', color: 'var(--color-text-primary)' }}>{t('sec_filing_list')}</h2>
        </div>
        <table style={{ width: '100%', borderCollapse: 'collapse' }}>
          <thead>
            <tr style={{ background: 'var(--color-gray-50)' }}>
              {[t('sec_th_id'), t('sec_th_form_type'), t('sec_th_title'), t('sec_th_filing_date'), t('sec_th_deadline'), t('sec_th_reviewer'), t('sec_th_status'), t('actions')].map(h => (
                <th key={h} style={{ font: 'var(--text-label-sm)', color: 'var(--color-text-tertiary)', padding: '10px 14px', textAlign: 'left' }}>{h}</th>
              ))}
            </tr>
          </thead>
          <tbody>
            {secFilings.map(f => (
              <tr key={f.id} style={{ borderBottom: '1px solid var(--color-border-light)' }}>
                <td style={{ font: 'var(--text-body-sm)', fontFamily: 'var(--font-family-mono)', padding: '10px 14px' }}>{f.id}</td>
                <td style={{ padding: '10px 14px' }}>
                  <span style={{
                    padding: '2px 10px', borderRadius: 'var(--radius-full)',
                    background: 'var(--color-primary-light)', color: 'var(--color-primary)',
                    font: 'var(--text-label-sm)', fontWeight: 600,
                  }}>{f.formType}</span>
                </td>
                <td style={{ font: 'var(--text-body-sm)', padding: '10px 14px', color: 'var(--color-text-primary)' }}>{f.title}</td>
                <td style={{ font: 'var(--text-body-sm)', padding: '10px 14px', color: 'var(--color-text-tertiary)' }}>{f.filingDate || '-'}</td>
                <td style={{ font: 'var(--text-body-sm)', padding: '10px 14px', color: 'var(--color-text-secondary)' }}>{f.deadline}</td>
                <td style={{ font: 'var(--text-caption)', padding: '10px 14px', color: 'var(--color-text-tertiary)' }}>{f.reviewer}</td>
                <td style={{ padding: '10px 14px' }}>
                  <span style={{
                    padding: '2px 8px', borderRadius: 'var(--radius-full)', font: 'var(--text-caption)', fontWeight: 600,
                    ...getFilingStatusStyle(f.status),
                  }}>{getStatusLabel(f.status)}</span>
                </td>
                <td style={{ padding: '10px 14px' }}>
                  <button style={{ padding: '4px 12px', border: '1px solid var(--color-border)', borderRadius: 'var(--radius-sm)', background: 'none', cursor: 'pointer', font: 'var(--text-caption)', color: 'var(--color-primary)' }}>{t('view')}</button>
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>

      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 24 }}>
        {/* Filing Timeline */}
        <div style={{ background: 'var(--color-surface)', borderRadius: 'var(--radius-md)', border: '1px solid var(--color-border-light)', padding: 20 }}>
          <h2 style={{ font: 'var(--text-h2)', marginBottom: 16 }}>{t('sec_timeline')}</h2>
          {timelineEvents.map((evt, i) => (
            <div key={i} style={{ display: 'flex', alignItems: 'flex-start', padding: '10px 0', borderBottom: '1px solid var(--color-border-light)' }}>
              <div style={{
                width: 20, height: 20, borderRadius: '50%', marginRight: 12, marginTop: 2,
                display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
                background: evt.done ? 'var(--color-success)' : evt.type === 'deadline' ? 'var(--color-error)' : 'var(--color-info)',
                color: 'white', fontSize: 10,
              }}>
                {evt.done ? '✓' : evt.type === 'deadline' ? '!' : 'i'}
              </div>
              <div style={{ flex: 1 }}>
                <div style={{ font: 'var(--text-body)', color: 'var(--color-text-primary)' }}>{evt.event}</div>
                <div style={{ font: 'var(--text-caption)', color: 'var(--color-text-tertiary)', marginTop: 2 }}>{evt.date}</div>
              </div>
              {!evt.done && evt.type === 'deadline' && (
                <span style={{
                  padding: '2px 8px', borderRadius: 'var(--radius-full)',
                  background: 'var(--color-error-light)', color: 'var(--color-error)',
                  font: 'var(--text-caption)', whiteSpace: 'nowrap',
                }}>{t('sec_upcoming')}</span>
              )}
            </div>
          ))}
        </div>

        {/* Auto-generation Status */}
        <div style={{ background: 'var(--color-surface)', borderRadius: 'var(--radius-md)', border: '1px solid var(--color-border-light)', padding: 20 }}>
          <h2 style={{ font: 'var(--text-h2)', marginBottom: 16 }}>{t('sec_disclosure_status')}</h2>
          {disclosureItems.map((item, i) => (
            <div key={i} style={{ display: 'flex', alignItems: 'center', padding: '10px 0', borderBottom: '1px solid var(--color-border-light)' }}>
              <div style={{
                width: 20, height: 20, borderRadius: '50%', marginRight: 12,
                display: 'flex', alignItems: 'center', justifyContent: 'center',
                background: item.status === 'done' ? 'var(--color-success)' : item.status === 'progress' ? 'var(--color-warning)' : 'var(--color-gray-200)',
                color: item.status === 'pending' ? 'var(--color-text-tertiary)' : 'white', fontSize: 12,
              }}>
                {item.status === 'done' ? '✓' : item.status === 'progress' ? '...' : '○'}
              </div>
              <span style={{ flex: 1, font: 'var(--text-body)', color: 'var(--color-text-primary)' }}>{item.name}</span>
              <span style={{ font: 'var(--text-caption)', color: 'var(--color-text-tertiary)', marginRight: 12 }}>{item.lastUpdated}</span>
              <span style={{
                font: 'var(--text-caption)',
                color: item.status === 'done' ? 'var(--color-success)' : item.status === 'progress' ? 'var(--color-warning)' : 'var(--color-text-tertiary)',
              }}>
                {item.status === 'done' ? t('completed') : item.status === 'progress' ? t('generating') : t('pending')}
              </span>
            </div>
          ))}
          {/* Overall Progress */}
          <div style={{ marginTop: 16 }}>
            <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 6 }}>
              <span style={{ font: 'var(--text-body-sm)', color: 'var(--color-text-secondary)' }}>{t('sec_disclosure_progress')}</span>
              <span style={{ font: 'var(--text-label-sm)', color: 'var(--color-primary)' }}>{disclosureProgress}%</span>
            </div>
            <div style={{ height: 8, background: 'var(--color-gray-100)', borderRadius: 'var(--radius-full)', overflow: 'hidden' }}>
              <div style={{ width: `${disclosureProgress}%`, height: '100%', background: 'var(--color-primary)', borderRadius: 'var(--radius-full)' }} />
            </div>
          </div>
        </div>
      </div>
    </div>
  );
};
