import Link from 'next/link';

import { Plus } from 'lucide-react';
import { getTranslations } from 'next-intl/server';

interface SitePost {
  id: string;
  title: string;
  status: string;
  created_at: string | null;
}

import { formatDate } from '@kit/shared/dates';
import { createSiteBuilderApi } from '@kit/site-builder/api';
import { getSupabaseServerClient } from '@kit/supabase/server-client';
import { Badge } from '@kit/ui/badge';
import { Button } from '@kit/ui/button';
import { Card, CardContent } from '@kit/ui/card';

import { AccountNotFound } from '~/components/account-not-found';
import { CmsPageShell } from '~/components/cms-page-shell';
import { EmptyState } from '~/components/empty-state';

interface Props {
  params: Promise<{ account: string }>;
}

export default async function PostsManagerPage({ params }: Props) {
  const { account } = await params;
  const client = getSupabaseServerClient();
  const t = await getTranslations('siteBuilder');

  const { data: acct } = await client
    .from('accounts')
    .select('id')
    .eq('slug', account)
    .single();
  if (!acct) return <AccountNotFound />;

  const api = createSiteBuilderApi(client);
  const posts = await api.listPosts(acct.id);

  return (
    <CmsPageShell
      account={account}
      title={t('posts.title')}
      description={t('posts.manage')}
    >
      <div className="space-y-6">
        <div className="flex justify-end">
          <Button data-test="site-new-post-btn">
            <Plus className="mr-2 h-4 w-4" />
            {t('posts.newPost')}
          </Button>
        </div>
        {posts.length === 0 ? (
          <EmptyState
            title={t('posts.noPosts2')}
            description={t('posts.noPostDesc')}
            actionLabel={t('posts.createPostLabel')}
          />
        ) : (
          <div className="overflow-x-auto rounded-md border">
            <table className="w-full min-w-[640px] text-sm">
              <thead>
                <tr className="bg-muted/50 border-b">
                  <th scope="col" className="p-3 text-left">
                    {t('posts.colTitle')}
                  </th>
                  <th scope="col" className="p-3 text-left">
                    {t('posts.colStatus')}
                  </th>
                  <th scope="col" className="p-3 text-left">
                    {t('posts.colCreated')}
                  </th>
                </tr>
              </thead>
              <tbody>
                {(posts as SitePost[]).map((post) => (
                  <tr key={post.id} className="hover:bg-muted/30 border-b">
                    <td className="p-3 font-medium">{post.title}</td>
                    <td className="p-3">
                      <Badge
                        variant={
                          post.status === 'published' ? 'default' : 'secondary'
                        }
                      >
                        {post.status}
                      </Badge>
                    </td>
                    <td className="text-muted-foreground p-3 text-xs">
                      {formatDate(post.created_at)}
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        )}
      </div>
    </CmsPageShell>
  );
}
