import { Head } from '@inertiajs/react';
import { Trash2 } from 'lucide-react';
import PostController from '@/actions/App/Http/Controllers/Admin/PostController';
import { ConfirmDialog } from '@/components/admin/confirm-dialog';
import { ExternalLink } from '@/components/admin/external-link';
import { MediaNote } from '@/components/admin/media-note';
import type { PageMedia } from '@/components/admin/media-note';
import { PageHeader } from '@/components/admin/page-header';
import { PostForm } from '@/components/admin/post-form';
import type { PostFormOptions, PostValues } from '@/components/admin/post-form';
import { Button } from '@/components/ui/button';
import { useEditingLocale } from '@/hooks/use-editing-locale';

type Props = PostFormOptions & {
    post: {
        id: number;
        key: string;
        title: string;
        /** Address of the live post per language (absent where it is not live). */
        publicUrls: Record<string, string>;
        updatedAt: string | null;
        /** Changes whenever the post is saved. */
        version: number;
        /** Cover photo or video found for the post in the media library. */
        media: PageMedia | null;
    };
    values: PostValues;
};

export default function PostEdit({ post, values, ...options }: Props) {
    const editing = useEditingLocale();
    const publicUrl = post.publicUrls[editing.locale.code];

    return (
        <>
            <Head title={`${post.title} · Blog`} />

            {/* Remounted after a save, so the form shows what was stored. */}
            <PostForm
                key={post.version}
                {...options}
                values={values}
                action={PostController.update(post.id)}
                editing={editing}
                footer={
                    <MediaNote
                        media={post.media}
                        collection="blog"
                        name={post.key}
                    />
                }
                header={
                    <PageHeader
                        title={post.title}
                        description={`Yazı anahtarı: ${post.key}${post.updatedAt ? ` · Son güncelleme: ${post.updatedAt}` : ''}`}
                        actions={
                            <>
                                {publicUrl && (
                                    <ExternalLink
                                        href={publicUrl}
                                        className="text-sm"
                                    >
                                        Sitede gör ({editing.locale.short})
                                    </ExternalLink>
                                )}
                                <ConfirmDialog
                                    action={PostController.destroy.form(
                                        post.id,
                                    )}
                                    title="Yazı silinsin mi?"
                                    description={`“${post.title}” bütün dillerdeki içeriğiyle birlikte kalıcı olarak silinir ve sayfası yayından kalkar. Bu işlem geri alınamaz; yalnızca yayından kaldırmak için “Yayında” seçeneğini kapatmanız yeterlidir.`}
                                >
                                    <Button
                                        type="button"
                                        variant="outline"
                                        size="sm"
                                    >
                                        <Trash2 />
                                        Sil
                                    </Button>
                                </ConfirmDialog>
                            </>
                        }
                    />
                }
            />
        </>
    );
}

PostEdit.layout = (props: Props) => ({
    breadcrumbs: [
        { title: 'Blog', href: PostController.index() },
        {
            title: props.post.title,
            href: PostController.edit(props.post.id),
        },
    ],
});
