import { mockDb } from '@/lib/mockDb'
import { deleteBlogPostAction } from '@/app/actions'
import { Link } from '@/i18n/routing'
import DeleteButton from '@/components/DeleteButton'
import { Edit, Trash, Plus, FileText, CheckCircle, XCircle } from 'lucide-react'

export default async function AdminBlogPage() {
  const posts = await mockDb.getBlogPosts()

  return (
    <div className="space-y-6">
      <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
        <div>
          <h2 className="text-3xl font-heading font-extrabold text-pine lowercase">blog yazıları</h2>
          <p className="text-ink/65 text-xs font-medium mt-1">
            Marmaris Local rehberi için hazırlanan gezi, gastronomi ve tanıtım yazıları.
          </p>
        </div>
        <Link
          href="/admin/blog/new"
          className="inline-flex items-center gap-1.5 bg-turquoise hover:bg-turquoise/90 text-paper text-xs font-bold py-3 px-5 rounded-xl shadow-sm transition active:scale-95 duration-150"
        >
          <Plus className="w-4 h-4" />
          Yeni Yazı Ekle
        </Link>
      </div>

      <div className="bg-paper border border-pine/8 rounded-2xl shadow-sm overflow-hidden">
        <div className="overflow-x-auto">
          <table className="min-w-full divide-y divide-pine/8 text-sm">
            <thead className="bg-stone-deep/40 text-shutter font-mono text-[10px] uppercase tracking-wider">
              <tr>
                <th className="px-6 py-4 text-left">Görsel</th>
                <th className="px-6 py-4 text-left">Yazı Başlığı (TR)</th>
                <th className="px-6 py-4 text-left">URL Slug</th>
                <th className="px-6 py-4 text-left">Etiketler</th>
                <th className="px-6 py-4 text-left">Yayın Durumu</th>
                <th className="px-6 py-4 text-right">İşlemler</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-dashed divide-pine/8 text-ink/80">
              {posts.length === 0 ? (
                <tr>
                  <td colSpan={6} className="px-6 py-12 text-center text-xs text-shutter font-medium">
                    Henüz blog yazısı eklenmemiş.
                  </td>
                </tr>
              ) : (
                posts.map((post) => {
                  return (
                    <tr key={post.id} className="hover:bg-stone/20 transition duration-150">
                      <td className="px-6 py-4 whitespace-nowrap">
                        <div className="h-12 w-16 relative rounded-lg overflow-hidden bg-stone border border-pine/8">
                          {post.coverImage ? (
                            <img src={post.coverImage} alt={post.titleTr} className="object-cover w-full h-full" />
                          ) : (
                            <div className="w-full h-full flex items-center justify-center text-shutter bg-stone/50">
                              <FileText className="w-5 h-5" />
                            </div>
                          )}
                        </div>
                      </td>
                      <td className="px-6 py-4 font-medium">
                        <div className="font-heading font-bold text-pine lowercase text-sm">{post.titleTr}</div>
                      </td>
                      <td className="px-6 py-4 font-mono text-xs text-turquoise">
                        /blog/{post.slug}
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap">
                        <div className="flex flex-wrap gap-1">
                          {post.tags.map(tag => (
                            <span key={tag} className="bg-paper text-shutter border border-pine/10 px-2 py-0.5 rounded text-[10px] font-mono">
                              {tag}
                            </span>
                          ))}
                        </div>
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap">
                        {post.publishedAt ? (
                          <span className="inline-flex items-center gap-1 rounded-full bg-paper px-2.5 py-0.5 text-[10px] font-mono font-bold text-turquoise border border-turquoise/20 uppercase tracking-wider">
                            <CheckCircle className="w-3.5 h-3.5" />
                            Yayında
                          </span>
                        ) : (
                          <span className="inline-flex items-center gap-1 rounded-full bg-paper px-2.5 py-0.5 text-[10px] font-mono font-medium text-shutter/65 border border-pine/8 uppercase tracking-wider">
                            <XCircle className="w-3.5 h-3.5" />
                            Taslak
                          </span>
                        )}
                      </td>
                      <td className="px-6 py-4 text-right whitespace-nowrap">
                        <div className="flex justify-end gap-2">
                          <Link
                            href={`/admin/blog/${post.id}`}
                            className="p-1.5 bg-paper text-shutter hover:text-turquoise hover:bg-turquoise/5 rounded-lg border border-pine/10 hover:border-turquoise/25 transition shadow-sm"
                            title="Düzenle"
                          >
                            <Edit className="w-4 h-4" />
                          </Link>

<form action={async () => {
                            'use server'
                            await deleteBlogPostAction(post.id)
                          }}>
                            <DeleteButton />
                          </form>
                        </div>
                      </td>
                    </tr>
                  )
                })
              )}
            </tbody>
          </table>
        </div>

        <Link
          href="/admin/blog/new"
          className="inline-flex items-center gap-1.5 bg-turquoise hover:bg-turquoise/90 text-paper text-xs font-bold py-3 px-5 rounded-xl shadow-sm transition active:scale-95 duration-150"
        >
          <Plus className="w-4 h-4" />
          Yeni Yazı Ekle
        </Link>
      </div>
    </div>
  )
}
