import { Button, ErrorState } from '../ui'
import { t } from '@/lib/i18n'
import type { SyncProgress } from './types'

export function SyncErrorBanner(params: { error: string | null; onDismiss: () => void }): React.ReactElement | null {
  if (!params.error) {
    return null
  }
  return (
    <div className="mb-4">
      <ErrorState message={params.error} />
      <Button type="button" variant="ghost" size="small" onClick={params.onDismiss} className="mt-2">
        ×
      </Button>
    </div>
  )
}

export function SyncResyncButton(params: { isSyncing: boolean; onClick: () => void }): React.ReactElement | null {
  if (params.isSyncing) {
    return null
  }
  return (
    <Button
      type="button"
      variant="primary"
      size="small"
      onClick={params.onClick}
    >
      {t('settings.sync.resync')}
    </Button>
  )
}

export function SyncDateRange(params: { totalDays: number; startDate: string; endDate: string }): React.ReactElement | null {
  if (params.totalDays <= 0) {
    return null
  }
  return (
    <div className="mb-2">
      <p className="text-sm text-cyber-accent">
        {t('settings.sync.daysRange', { startDate: params.startDate, endDate: params.endDate, days: params.totalDays })}
      </p>
    </div>
  )
}

export function SyncProgressSection(params: { isSyncing: boolean; syncProgress: SyncProgress; progressPercentage: number }): React.ReactElement | null {
  if (!params.isSyncing || !params.syncProgress) {
    return null
  }
  return (
    <div className="space-y-2">
      <div className="flex items-center justify-between text-sm">
        <span className="text-cyber-accent">
          {t('settings.sync.progress', { current: params.syncProgress.currentStep, total: params.syncProgress.totalSteps })}
        </span>
        <span className="text-neon-cyan font-semibold">{Math.round(params.progressPercentage)}%</span>
      </div>
      <div className="w-full bg-cyber-dark rounded-full h-2 overflow-hidden">
        <div className="bg-neon-cyan h-full transition-all duration-300" style={{ width: `${params.progressPercentage}%` }} />
      </div>
    </div>
  )
}

export function SyncStatusMessage(params: { isSyncing: boolean; totalDays: number; isRecentlySynced: boolean }): React.ReactElement | null {
  if (params.isSyncing || params.totalDays !== 0) {
    return null
  }
  if (params.isRecentlySynced) {
    return <p className="text-sm text-green-400">{t('settings.sync.completed')}</p>
  }
  return <p className="text-sm text-cyber-accent">{t('settings.sync.ready')}</p>
}
