import { Head, router } from '@inertiajs/react';
import {
    Bell,
    Download,
    FileCode2,
    FilePenLine,
    FolderArchive,
    FolderInput,
    FolderTree,
    LogOut,
    PanelLeftClose,
    PanelLeftOpen,
    type LucideIcon,
} from 'lucide-react';
import { AppSidebar } from '@/components/AppSidebar';
import { Button } from '@/components/ui/button';
import { useAppSidebar } from '@/hooks/useAppSidebar';
import { route } from '@/lib/route';

type Step = {
    title: string;
    body: string;
    Icon: LucideIcon;
    bullets?: readonly string[];
    files?: readonly string[];
    example?: string;
};

const STEPS: readonly Step[] = [
    {
        title: 'Download the code',
        body: 'Open Code → Download code. You get child.zip.',
        Icon: Download,
    },
    {
        title: 'Unzip the package',
        body: 'Extract child.zip. You will see child/BE/ plus a root API stub.',
        Icon: FolderArchive,
    },
    {
        title: 'Copy into your backend',
        body: 'In a project like abc/be + abc/fe, copy child/BE/ files into be. Replace matching files.',
        Icon: FolderInput,
    },
    {
        title: 'Update only two files',
        body: 'Do not overwrite your whole config. Merge into these existing files only:',
        Icon: FilePenLine,
        files: ['.env', '.htaccess'],
        bullets: [
            '.env — add MASTER_RESULT_* keys; keep your other settings.',
            '.htaccess — append the FilesMatch block to protect .env.',
        ],
    },
    {
        title: 'Paste API at project root',
        body: 'Place child/supergame_master_result_api.php in the parent of be and fe.',
        Icon: FileCode2,
        example: 'abc/supergame_master_result_api.php\nabc/be/\nabc/fe/',
    },
    {
        title: 'Rename child → your BE folder',
        body: 'In that root PHP file, replace child with your backend folder name (e.g. be).',
        Icon: FolderTree,
        example:
            "require __DIR__ . '/child/supergame_master_result_api.php';\n\n// if backend folder is be:\nrequire __DIR__ . '/be/supergame_master_result_api.php';",
    },
    {
        title: 'Point FCM paths to this domain',
        body: 'In BE/supergame_master_win_loose.php, open sendResultFcmNotification and set both paths to files that already exist in that BE folder.',
        Icon: Bell,
        files: ['supergame_master_win_loose.php'],
        bullets: [
            'credentialsFile — your Firebase service-account JSON filename.',
            'include_once — keep fmc.php next to that JSON in the same BE folder.',
        ],
        example:
            "$credentialsFile = __DIR__ . '/monu567-c547b-firebase-adminsdk-fbsvc-9e2ace33e5.json';\ninclude_once __DIR__ . '/fmc.php';",
    },
];

export default function CodeSetup({
    auth,
}: {
    auth: { user: { first_name: string; last_name: string; email: string } | null };
}) {
    const { collapsed: sidebarCollapsed, mobileOpen, toggleSidebar, closeMobile, showOpenIcon } =
        useAppSidebar();

    return (
        <>
            <Head title="Setup steps" />
            <div className="flex min-h-svh gap-3 bg-background p-3">
                <AppSidebar
                    auth={auth}
                    collapsed={sidebarCollapsed}
                    mobileOpen={mobileOpen}
                    onMobileClose={closeMobile}
                    active="code-setup"
                />

                <div className="flex min-w-0 flex-1 flex-col gap-3">
                    <header className="flex h-12 shrink-0 items-center justify-between gap-3 rounded-2xl border border-border bg-card px-4 shadow-sm">
                        <div className="flex min-w-0 items-center gap-3">
                            <Button
                                type="button"
                                variant="ghost"
                                size="icon"
                                className="cursor-pointer"
                                aria-label={showOpenIcon ? 'Open sidebar' : 'Close sidebar'}
                                onClick={toggleSidebar}
                            >
                                {showOpenIcon ? (
                                    <PanelLeftOpen className="size-4" aria-hidden />
                                ) : (
                                    <PanelLeftClose className="size-4" aria-hidden />
                                )}
                            </Button>

                            <div className="min-w-0">
                                <div className="truncate text-sm font-semibold">
                                    Setup steps
                                </div>
                            </div>
                        </div>

                        <Button
                            type="button"
                            variant="ghost"
                            size="sm"
                            className="h-8 gap-1.5 text-xs"
                            onClick={() => router.post(route('logout'))}
                        >
                            <LogOut className="size-4" aria-hidden />
                            Sign out
                        </Button>
                    </header>

                    <main className="relative flex min-h-0 flex-1 flex-col overflow-auto rounded-2xl border border-border bg-card shadow-sm">
                        <div
                            className="pointer-events-none absolute inset-0 opacity-[0.45]"
                            style={{
                                backgroundImage:
                                    'radial-gradient(ellipse 80% 50% at 0% 0%, oklch(0.488 0.243 264.376 / 0.08), transparent 55%), radial-gradient(ellipse 60% 40% at 100% 0%, oklch(0.205 0 0 / 0.04), transparent 50%)',
                            }}
                            aria-hidden
                        />

                        <div className="relative border-b border-border/80 px-5 py-6 sm:px-8">
                            <p className="text-[11px] font-medium tracking-[0.14em] text-muted-foreground uppercase">
                                Code install
                            </p>
                            <h1 className="mt-1 font-heading text-2xl font-semibold tracking-tight sm:text-3xl">
                                Steps to setup code
                            </h1>
                            <p className="mt-2 max-w-xl text-sm text-muted-foreground">
                                Wire master result posting into a child domain — seven short steps.
                            </p>
                        </div>

                        <ol className="relative grid gap-3 p-4 sm:grid-cols-2 sm:p-6 xl:grid-cols-3">
                            {STEPS.map((step, index) => {
                                const Icon = step.Icon;
                                const isLast = index === STEPS.length - 1;

                                return (
                                    <li
                                        key={step.title}
                                        className={
                                            isLast
                                                ? 'flex flex-col rounded-xl border border-border/80 bg-background/80 p-4 transition-colors duration-200 hover:border-foreground/20 hover:bg-background sm:col-span-2 xl:col-span-2'
                                                : 'flex flex-col rounded-xl border border-border/80 bg-background/80 p-4 transition-colors duration-200 hover:border-foreground/20 hover:bg-background'
                                        }
                                    >
                                        <div className="flex items-start justify-between gap-3">
                                            <div className="flex size-9 items-center justify-center rounded-lg bg-[oklch(0.205_0_0)] text-white">
                                                <Icon className="size-4" aria-hidden />
                                            </div>
                                            <span className="font-mono text-[11px] font-medium tracking-wider text-muted-foreground">
                                                {String(index + 1).padStart(2, '0')}
                                            </span>
                                        </div>

                                        <h2 className="mt-3 text-sm font-semibold tracking-tight">
                                            {step.title}
                                        </h2>
                                        <p className="mt-1.5 text-xs leading-relaxed text-muted-foreground">
                                            {step.body}
                                        </p>

                                        {step.files && (
                                            <div className="mt-3 flex flex-wrap gap-1.5">
                                                {step.files.map((file) => (
                                                    <span
                                                        key={file}
                                                        className="rounded-md border border-border bg-muted/60 px-2 py-0.5 font-mono text-[11px] font-medium"
                                                    >
                                                        {file}
                                                    </span>
                                                ))}
                                            </div>
                                        )}

                                        {step.bullets && (
                                            <ul className="mt-3 space-y-1.5 border-t border-border/70 pt-3">
                                                {step.bullets.map((item) => (
                                                    <li
                                                        key={item}
                                                        className="text-[11px] leading-relaxed text-muted-foreground"
                                                    >
                                                        {item}
                                                    </li>
                                                ))}
                                            </ul>
                                        )}

                                        {step.example && (
                                            <pre className="mt-3 overflow-x-auto rounded-lg border border-white/5 bg-[oklch(0.205_0_0)] px-3 py-2.5 font-mono text-[10px] leading-relaxed text-white/80">
                                                {step.example}
                                            </pre>
                                        )}
                                    </li>
                                );
                            })}
                        </ol>
                    </main>
                </div>
            </div>
        </>
    );
}
