import { useEffect, useState } from 'react';
import { Text, View } from '@tarojs/components';
import { loadContentEntriesAdmin, loadImportJobs, loadPublicQuestionBanks, type ContentEntryAdminItem, type ImportJobItem, type PublicQuestionBankItem } from '@/services/tenantAdmin';
import '../admin.css';

export default function TenantContentPage() {
  const [entries, setEntries] = useState<ContentEntryAdminItem[]>([]);
  const [jobs, setJobs] = useState<ImportJobItem[]>([]);
  const [publicBanks, setPublicBanks] = useState<PublicQuestionBankItem[]>([]);
  const [error, setError] = useState('');

  useEffect(() => {
    Promise.all([
      loadContentEntriesAdmin().catch(() => ({ items: [] })),
      loadImportJobs(20).catch(() => ({ items: [] })),
      loadPublicQuestionBanks().catch(() => ({ items: [] })),
    ]).then(([entryPayload, jobPayload, bankPayload]) => {
      setEntries(entryPayload.items || []);
      setJobs(jobPayload.items || []);
      setPublicBanks(bankPayload.items || []);
    }).catch(nextError => setError(nextError instanceof Error ? nextError.message : '内容数据加载失败'));
  }, []);

  return (
    <View className='admin-page'>
      <View className='admin-shell'>
        <View className='admin-header'>
          <Text className='admin-kicker'>Content</Text>
          <Text className='admin-title'>题库内容</Text>
          <Text className='admin-subtitle'>入口、分类、导入任务、公共题库采纳和同步状态。</Text>
        </View>

        <View className='admin-section'>
          <Text className='admin-section-title'>内容入口</Text>
          <View className='admin-list'>
            {entries.map(item => (
              <View className='admin-row' key={item.id}>
                <Text className='admin-row-main'>{item.name}</Text>
                <Text className='admin-row-meta'>{item.entryType || 'entry'} · {item.visibility || 'visible'} · {item.isActive === false ? '停用' : '启用'}</Text>
              </View>
            ))}
          </View>
          {!entries.length ? <View className='admin-empty'>暂无内容入口。</View> : null}
        </View>

        <View className='admin-section'>
          <Text className='admin-section-title'>导入任务</Text>
          <View className='admin-list'>
            {jobs.map(item => (
              <View className='admin-row' key={item.id}>
                <Text className='admin-row-main'>{item.importType || 'import'} · {item.status || 'unknown'}</Text>
                <Text className='admin-row-meta'>总数 {item.totalCount || 0} · 成功 {item.successCount || 0} · 错误 {item.errorCount || 0} · 警告 {item.warningCount || 0}</Text>
              </View>
            ))}
          </View>
          {!jobs.length ? <View className='admin-empty'>暂无导入任务。</View> : null}
        </View>

        <View className='admin-section'>
          <Text className='admin-section-title'>公共题库</Text>
          <View className='admin-list'>
            {publicBanks.map((item, index) => (
              <View className='admin-row' key={item.grantId || item.sourceQuestionBankId || index}>
                <Text className='admin-row-main'>{item.name || '公共题库'}</Text>
                <Text className='admin-row-meta'>{item.regionName || '通用'} · {item.grantScope || '授权'} · {item.adoption ? '已采纳' : '可采纳'}</Text>
              </View>
            ))}
          </View>
          {!publicBanks.length ? <View className='admin-empty'>暂无可见公共题库授权。</View> : null}
        </View>
        {error ? <Text className='admin-error'>{error}</Text> : null}
      </View>
    </View>
  );
}
