'use client'

import { useTranslations } from 'next-intl'
import { Home, ChevronRight, FileText, Newspaper } from 'lucide-react'
import { Link, useRouter } from '@/i18n/navigation'
import Container from '@/components/frontend/Container'
import Pagination from '@/components/frontend/Pagination'
import AuthorAvatar from '@/components/frontend/blog/AuthorAvatar'
import AuthorSocialLinks from '@/components/frontend/blog/AuthorSocialLinks'
import BlogCard from '@/components/frontend/blog/BlogCard'
import type { BlogAuthorProfile, BlogPostSummary } from '@/lib/db/queries/getBlogPosts'

interface AuthorPageClientProps {
  author: BlogAuthorProfile
  posts: BlogPostSummary[]
  total: number
  page: number
  totalPages: number
  siteName: string
}

export default function AuthorPageClient({ author, posts, total, page, totalPages, siteName }: AuthorPageClientProps) {
  const t = useTranslations('Blog')
  const tp = useTranslations('BlogPage')
  const router = useRouter()

  const handlePageChange = (newPage: number) => {
    router.push({
      pathname: `/blogs/author/${author.slug}`,
      query: newPage > 1 ? { page: String(newPage) } : {},
    })
  }

  return (
    <>
      {/* Profile hero */}
      <section className="relative overflow-hidden rounded-2xl mb-10">
        <div className="absolute inset-0 bg-gradient-primary opacity-90" />
        <div className="absolute -top-16 -end-16 w-72 h-72 bg-white/10 rounded-full blur-3xl" />
        <div className="absolute -bottom-24 -start-10 w-80 h-80 bg-white/5 rounded-full blur-3xl" />
        <div className="relative z-10 px-6 py-10 md:px-10 md:py-14">
          <div className="max-w-4xl mx-auto">
            <nav aria-label="Breadcrumb" className="mb-8">
              <ol className="flex flex-wrap items-center gap-1.5 text-sm text-white/70">
                <li>
                  <Link href="/" className="inline-flex items-center gap-1 hover:text-white transition">
                    <Home size={14} /> <span>{t('breadcrumbHome')}</span>
                  </Link>
                </li>
                <li aria-hidden="true"><ChevronRight size={14} className="text-white/40 rtl:rotate-180" /></li>
                <li><Link href="/blogs" className="hover:text-white transition">{t('breadcrumbBlog')}</Link></li>
                <li aria-hidden="true"><ChevronRight size={14} className="text-white/40 rtl:rotate-180" /></li>
                <li className="text-white font-medium truncate" aria-current="page">{author.name}</li>
              </ol>
            </nav>

            <div className="flex flex-col md:flex-row items-center md:items-start gap-6 md:gap-8 text-center md:text-start">
              <AuthorAvatar
                name={author.name}
                imageUrl={author.imageUrl}
                size={128}
                className="ring-4 ring-white/70 shadow-xl text-3xl"
              />
              <div className="min-w-0 flex-1">
                <p className="text-xs font-semibold uppercase tracking-wider text-white/70 mb-1">{tp('writtenBy')}</p>
                <h1 className="text-3xl md:text-4xl font-bold text-white mb-3">{author.name}</h1>
                {author.bio && (
                  <p className="text-white/85 leading-relaxed whitespace-pre-line max-w-2xl mb-5 mx-auto md:mx-0">
                    {author.bio}
                  </p>
                )}
                <div className="flex flex-wrap items-center justify-center md:justify-start gap-4">
                  <span className="inline-flex items-center gap-2 rounded-full bg-white/15 border border-white/20 px-4 py-2 text-sm font-medium text-white">
                    <FileText size={15} /> {tp('articlesCount', { count: total })}
                  </span>
                  <AuthorSocialLinks author={author} variant="light" />
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>

      <Container className="pb-16">
        {posts.length === 0 ? (
          <div className="text-center py-16 bg-white rounded-2xl border border-gray-100">
            <div className="w-16 h-16 mx-auto mb-4 rounded-full bg-primary-soft text-primary flex items-center justify-center">
              <Newspaper size={28} />
            </div>
            <h2 className="text-xl font-semibold text-dark mb-2">{tp('authorNoPosts')}</h2>
            <p className="text-gray-custom">{tp('authorNoPostsDescription', { name: author.name })}</p>
          </div>
        ) : (
          <>
            <h2 className="text-2xl font-bold text-dark mb-6 flex items-center gap-3">
              <span className="w-1.5 h-7 rounded-full bg-gradient-primary" aria-hidden />
              {tp('latestArticles')}
            </h2>
            <div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
              {posts.map((post) => (
                <BlogCard key={post.id} post={post} siteName={siteName} />
              ))}
            </div>
            {totalPages > 1 && (
              <div className="mt-12">
                <Pagination currentPage={page} totalPages={totalPages} onPageChange={handlePageChange} />
              </div>
            )}
          </>
        )}
      </Container>
    </>
  )
}
