import { Link } from 'react-router-dom'
import { useRuns } from '../hooks/useRuns'
import type { RunStatus } from '../types'

const statusStyles: Record<RunStatus, string> = {
  active: 'bg-status-active-bg text-status-active border border-status-active/20',
  completed: 'bg-status-completed-bg text-status-completed border border-status-completed/20',
  failed: 'bg-status-failed-bg text-status-failed border border-status-failed/20',
}

export function RunList() {
  const { data: runs, isLoading, error } = useRuns()

  return (
    <div className="max-w-4xl mx-auto p-8">
      <div className="flex items-center justify-between mb-6">
        <h1 className="text-3xl font-bold text-text-primary">Your Runs</h1>
        <Link
          to="/runs/new"
          className="px-4 py-2 bg-accent-600 text-white rounded-lg font-medium hover:bg-accent-500 focus:outline-none focus:ring-2 focus:ring-accent-400 focus:ring-offset-2 focus:ring-offset-surface-0 transition-all active:scale-[0.98]"
        >
          Start New Run
        </Link>
      </div>

      {isLoading && (
        <div className="flex items-center justify-center py-12">
          <div className="w-8 h-8 border-4 border-accent-400 border-t-transparent rounded-full animate-spin" />
        </div>
      )}

      {error && (
        <div className="rounded-lg bg-status-failed-bg border border-status-failed/20 p-4 text-status-failed">
          Failed to load runs. Please try again.
        </div>
      )}

      {runs && runs.length === 0 && (
        <div className="text-center py-16">
          <p className="text-lg text-text-secondary mb-4">
            No runs yet. Start your first Nuzlocke!
          </p>
          <Link
            to="/runs/new"
            className="inline-block px-6 py-2 bg-accent-600 text-white rounded-lg font-medium hover:bg-accent-500 transition-all active:scale-[0.98]"
          >
            Start New Run
          </Link>
        </div>
      )}

      {runs && runs.length > 0 && (
        <div className="space-y-2">
          {runs.map((run) => (
            <Link
              key={run.id}
              to={`/runs/${run.id}`}
              className="block bg-surface-1 rounded-xl border border-border-default hover:border-border-accent transition-all hover:-translate-y-0.5 p-4"
            >
              <div className="flex items-center justify-between">
                <div>
                  <h2 className="text-lg font-semibold text-text-primary">{run.name}</h2>
                  <p className="text-sm text-text-secondary">
                    Started{' '}
                    {new Date(run.startedAt).toLocaleDateString(undefined, {
                      year: 'numeric',
                      month: 'short',
                      day: 'numeric',
                    })}
                  </p>
                </div>
                <span
                  className={`px-2.5 py-0.5 rounded-full text-xs font-medium capitalize ${statusStyles[run.status]}`}
                >
                  {run.status}
                </span>
              </div>
            </Link>
          ))}
        </div>
      )}
    </div>
  )
}
