import EditModal from './Component/Edit';
import AppLayout from '@/layouts/app-layout';
import { Head, router, usePage } from '@inertiajs/react';
import {
    ArrowLeft,
    BoomBox,
    Calendar,
    Check,
    CheckCircle2,
    ClipboardList,
    Copy,
    Globe,
    History,
    Info,
    Pencil,
    Radio,
    XCircle,
} from 'lucide-react';
import { ReactNode, useState } from 'react';

import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';

declare const route: (...args: any[]) => string;

interface ProductChannelData {
    id: number;
    uid?: string;
    name: string;
    label?: string;
    icon?: string;
    status: number;
    created_at: string;
    updated_at: string;
}

type ShowProps = {
    item: ProductChannelData;
};

function Show() {
    const { item } = usePage<ShowProps>().props;
    const [copiedField, setCopiedField] = useState<string | null>(null);
    const [activeTab, setActiveTab] = useState('overview');
    const [isEditOpen, setIsEditOpen] = useState(false);

    const handleCopy = (text: string, field: string) => {
        navigator.clipboard.writeText(text).then(() => {
            setCopiedField(field);
            setTimeout(() => setCopiedField(null), 1500);
        });
    };

    const isActive = item.status === 1;

    const formatDate = (dateStr: string) =>
        new Date(dateStr).toLocaleDateString('en-US', {
            year: 'numeric',
            month: 'long',
            day: 'numeric',
        });

    return (
        <>
            <Head title={`${item.name} - Product Channel`} />
            <Tabs
                defaultValue="overview"
                value={activeTab}
                onValueChange={setActiveTab}
                className="flex h-screen flex-col overflow-hidden bg-background"
            >
                {/* Fixed Header */}
                <div className="shrink-0 border-b bg-background">
                    <div className="flex items-center gap-1 overflow-x-auto border-b">
                        <TabsList className="my-2 h-auto flex-nowrap gap-1 bg-transparent p-0 px-3 text-[15px]">
                            <TabsTrigger
                                value="overview"
                                className="flex h-10 items-center gap-2 rounded-lg border-2 border-transparent px-3 py-2.5 font-medium whitespace-nowrap data-[state=active]:border-primary data-[state=active]:bg-primary data-[state=active]:text-white data-[state=inactive]:text-slate-600 sm:px-4"
                            >
                                <Info className="h-3 w-3 sm:h-4 sm:w-4" />
                                <span className="hidden sm:inline">Overview</span>
                            </TabsTrigger>
                            <TabsTrigger
                                value="history"
                                className="flex h-10 items-center gap-2 rounded-lg border-2 border-transparent px-3 py-2.5 font-medium whitespace-nowrap data-[state=active]:border-primary data-[state=active]:bg-primary data-[state=active]:text-white data-[state=inactive]:text-slate-600 sm:px-4"
                            >
                                <History className="h-3 w-3 sm:h-4 sm:w-4" />
                                <span className="hidden sm:inline">History</span>
                            </TabsTrigger>
                        </TabsList>
                    </div>
                </div>

                {/* Scrollable Content */}
                <div className="no-scrollbar flex-1 overflow-x-hidden overflow-y-auto">
                    <div className="h-full p-2 sm:p-4">
                        <TabsContent value="overview" className="mt-0 flex flex-col gap-4 lg:flex-row lg:gap-6">
                            {/* Sidebar */}
                            <aside className="flex w-full shrink-0 flex-col lg:w-80 lg:max-h-[calc(100vh-8rem)]">
                                <div className="mb-4 shrink-0 rounded-xl border border-slate-200/80 bg-background p-4 shadow-sm">
                                    {/* Channel Header */}
                                    <div className="mb-5 flex items-start gap-4">
                                        <div className="flex h-16 w-16 shrink-0 items-center justify-center rounded-2xl bg-primary/10 ring-2 ring-slate-100">
                                            {item.icon ? (
                                                <span className="text-2xl">{item.icon}</span>
                                            ) : (
                                                <Radio className="h-8 w-8 text-primary" />
                                            )}
                                        </div>
                                        <div className="min-w-0 flex-1 space-y-1">
                                            <div className="flex items-start justify-between">
                                                <div className="group flex items-center gap-2">
                                                    <h2 className="truncate text-xl font-bold text-slate-900">{item.name}</h2>
                                                    {copiedField === 'name' ? (
                                                        <Check className="h-3.5 w-3.5 text-green-600" />
                                                    ) : (
                                                        <button
                                                            onClick={() => handleCopy(item.name, 'name')}
                                                            className="opacity-0 transition-opacity group-hover:opacity-100"
                                                        >
                                                            <Copy className="h-3.5 w-3.5 text-slate-400 hover:text-primary" />
                                                        </button>
                                                    )}
                                                </div>
                                                <button
                                                    className="cursor-pointer"
                                                    onClick={() => setIsEditOpen(true)}
                                                >
                                                    <Pencil className="h-4 w-4 text-slate-500 hover:text-primary" />
                                                </button>
                                            </div>
                                            {item.uid && (
                                                <div className="group flex items-center gap-2">
                                                    <BoomBox className="h-4 w-4 text-slate-400" />
                                                    <p className="truncate text-xs font-medium text-slate-500">UID: {item.uid}</p>
                                                    {copiedField === 'uid' ? (
                                                        <Check className="h-3 w-3 text-green-600" />
                                                    ) : (
                                                        <button
                                                            onClick={() => handleCopy(item.uid!, 'uid')}
                                                            className="opacity-0 transition-opacity group-hover:opacity-100"
                                                        >
                                                            <Copy className="h-3 w-3 text-slate-400 hover:text-primary" />
                                                        </button>
                                                    )}
                                                </div>
                                            )}
                                            {item.label && (
                                                <p className="text-xs text-slate-400">{item.label}</p>
                                            )}
                                            <div className="mt-1">
                                                <Badge
                                                    variant="outline"
                                                    className={isActive ? 'border-green-500 text-green-700' : 'border-gray-400 text-gray-500'}
                                                >
                                                    {isActive ? (
                                                        <CheckCircle2 className="mr-1 h-3 w-3" />
                                                    ) : (
                                                        <XCircle className="mr-1 h-3 w-3" />
                                                    )}
                                                    {isActive ? 'Active' : 'Inactive'}
                                                </Badge>
                                            </div>
                                        </div>
                                    </div>

                                    {/* Quick Actions */}
                                    <div className="flex gap-2">
                                        <Button
                                            size="sm"
                                            variant="outline"
                                            className="flex-1"
                                            onClick={() => setIsEditOpen(true)}
                                        >
                                            <Pencil className="mr-1 h-3 w-3" />
                                            Edit
                                        </Button>
                                        <Button
                                            size="sm"
                                            variant="outline"
                                            className="flex-1"
                                            onClick={() => router.visit(route('product-channel.index'))}
                                        >
                                            <ArrowLeft className="mr-1 h-3 w-3" />
                                            Back
                                        </Button>
                                    </div>
                                </div>

                                {/* Dates Card */}
                                <div className="rounded-xl border border-slate-200/80 bg-background p-4 shadow-sm">
                                    <h3 className="mb-3 text-sm font-semibold text-slate-700">Timeline</h3>
                                    <div className="space-y-3">
                                        <div className="flex items-center gap-3">
                                            <Calendar className="h-4 w-4 text-slate-400" />
                                            <div>
                                                <p className="text-xs text-slate-500">Created</p>
                                                <p className="text-sm font-medium text-slate-700">{formatDate(item.created_at)}</p>
                                            </div>
                                        </div>
                                        <div className="flex items-center gap-3">
                                            <Calendar className="h-4 w-4 text-slate-400" />
                                            <div>
                                                <p className="text-xs text-slate-500">Last Updated</p>
                                                <p className="text-sm font-medium text-slate-700">{formatDate(item.updated_at)}</p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </aside>

                            {/* Main Content */}
                            <div className="min-w-0 flex-1 space-y-4">
                                <div className="rounded-xl border border-slate-200/80 bg-background p-6 shadow-sm">
                                    <div className="mb-4 flex items-center gap-2">
                                        <ClipboardList className="h-4 w-4 text-primary" />
                                        <h3 className="text-base font-semibold text-slate-800">Channel Information</h3>
                                    </div>
                                    <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                                        <div className="rounded-lg bg-slate-50 p-3">
                                            <p className="mb-0.5 text-xs font-medium uppercase tracking-wide text-slate-500">Channel Name</p>
                                            <p className="text-sm font-semibold text-slate-800">{item.name}</p>
                                        </div>
                                        <div className="rounded-lg bg-slate-50 p-3">
                                            <p className="mb-0.5 text-xs font-medium uppercase tracking-wide text-slate-500">Status</p>
                                            <Badge
                                                variant="outline"
                                                className={isActive ? 'border-green-500 text-green-700' : 'border-gray-400 text-gray-500'}
                                            >
                                                {isActive ? 'Active' : 'Inactive'}
                                            </Badge>
                                        </div>
                                        {item.label && (
                                            <div className="rounded-lg bg-slate-50 p-3">
                                                <p className="mb-0.5 text-xs font-medium uppercase tracking-wide text-slate-500">Label</p>
                                                <p className="text-sm font-medium text-slate-700">{item.label}</p>
                                            </div>
                                        )}
                                        {item.icon && (
                                            <div className="rounded-lg bg-slate-50 p-3">
                                                <p className="mb-0.5 text-xs font-medium uppercase tracking-wide text-slate-500">Icon</p>
                                                <p className="text-2xl">{item.icon}</p>
                                            </div>
                                        )}
                                        <div className="rounded-lg bg-slate-50 p-3">
                                            <p className="mb-0.5 text-xs font-medium uppercase tracking-wide text-slate-500">Created</p>
                                            <p className="text-sm text-slate-700">{formatDate(item.created_at)}</p>
                                        </div>
                                        <div className="rounded-lg bg-slate-50 p-3">
                                            <p className="mb-0.5 text-xs font-medium uppercase tracking-wide text-slate-500">Last Updated</p>
                                            <p className="text-sm text-slate-700">{formatDate(item.updated_at)}</p>
                                        </div>
                                    </div>
                                </div>

                                {/* Channel Info */}
                                <div className="rounded-xl border border-slate-200/80 bg-background p-6 shadow-sm">
                                    <div className="mb-4 flex items-center gap-2">
                                        <Globe className="h-4 w-4 text-primary" />
                                        <h3 className="text-base font-semibold text-slate-800">About Sales Channels</h3>
                                    </div>
                                    <p className="text-sm text-slate-600 leading-relaxed">
                                        Sales channels define where products are sold (e.g., Online Store, Physical Store, Wholesale).
                                        Products can be assigned to specific channels with different pricing.
                                    </p>
                                </div>
                            </div>
                        </TabsContent>

                        <TabsContent value="history" className="mt-0">
                            <div className="rounded-lg border border-slate-200 bg-white p-6 text-center sm:p-12">
                                <History className="mx-auto mb-4 h-16 w-16 text-slate-300" />
                                <h3 className="mb-2 text-lg font-medium text-slate-900">Activity History</h3>
                                <p className="text-sm text-slate-500">History and activity log will be displayed here.</p>
                            </div>
                        </TabsContent>
                    </div>
                </div>
            </Tabs>

        <EditModal
            open={isEditOpen}
            onOpenChange={setIsEditOpen}
            defaultValues={item as any}
            onSuccess={() => router.reload()}
        />
        </>
    );
}

Show.layout = (page: ReactNode) => (
    <AppLayout
        breadcrumbs={[
            { title: 'Home', href: '/' },
            { title: 'Product Settings', href: '#' },
            { title: 'Product Channels', href: route('product-channel.index') },
            { title: 'Channel Details', href: '#' },
        ]}
        title="Channel Details"
    >
        {page}
    </AppLayout>
);

export default Show;
