import { mockDb } from '@/lib/mockDb'
import { markMessageReadAction } from '@/app/actions'
import { Check, MailOpen, Mail } from 'lucide-react'

export default async function AdminMessagesPage() {
  const messages = await mockDb.getMessages()

  return (
    <div className="space-y-6">
      <div>
        <h2 className="text-3xl font-heading font-extrabold text-pine lowercase">iletisim mesajları</h2>
        <p className="text-ink/65 text-xs font-medium mt-1">
          Kullanıcılar tarafından `/iletisim` formu üzerinden gönderilen genel mesajlar.
        </p>
      </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">Tarih</th>
                <th className="px-6 py-4 text-left">Gönderen</th>
                <th className="px-6 py-4 text-left">Konu</th>
                <th className="px-6 py-4 text-left">Mesaj</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">
              {messages.length === 0 ? (
                <tr>
                  <td colSpan={5} className="px-6 py-12 text-center text-xs text-shutter font-medium">
                    Gelen iletişim mesajı bulunmamaktadır.
                  </td>
                </tr>
              ) : (
                messages.map((msg) => {
                  return (
                    <tr key={msg.id} className={`hover:bg-stone/20 transition duration-150 ${!msg.isRead ? 'bg-turquoise/5' : ''}`}>
                      <td className="px-6 py-4 whitespace-nowrap text-xs text-shutter font-mono">
                        {new Date(msg.createdAt).toLocaleString('tr-TR')}
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap">
                        <div className="text-pine font-heading font-bold text-xs lowercase">{msg.name}</div>
                        <a href={`mailto:${msg.email}`} className="text-xs text-turquoise hover:underline">
                          {msg.email}
                        </a>
                      </td>
                      <td className="px-6 py-4 whitespace-nowrap">
                        <span className="inline-flex items-center rounded-md bg-paper px-2.5 py-0.5 text-[10px] font-mono font-bold text-shutter border border-pine/10 uppercase tracking-wider">
                          {msg.subject}
                        </span>
                      </td>
                      <td className="px-6 py-4 max-w-md">
                        <p className="text-xs break-words leading-relaxed whitespace-pre-line font-medium">{msg.message}</p>
                      </td>
                      <td className="px-6 py-4 text-right whitespace-nowrap">
                        {!msg.isRead ? (
                          <form action={async () => {
                            'use server'
                            await markMessageReadAction(msg.id)
                          }}>
                            <button
                              type="submit"
                              className="inline-flex items-center gap-1.5 px-3 py-1.5 bg-paper hover:bg-turquoise/5 text-turquoise rounded-lg border border-turquoise/20 text-xs font-bold transition shadow-sm"
                            >
                              <MailOpen className="w-3.5 h-3.5" />
                              Okundu İşaretle
                            </button>
                          </form>
                        ) : (
                          <span className="text-shutter/60 inline-flex items-center gap-1 text-xs font-semibold">
                            <Check className="w-4 h-4 text-turquoise" />
                            Okundu
                          </span>
                        )}
                      </td>
                    </tr>
                  )
                })
              )}
            </tbody>
          </table>
        </div>
      </div>
    </div>
  )
}
