import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
import { Head, Link } from "@inertiajs/react";
import { PageProps } from "@/types/global";
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from "@/Components/ui/table";
import { Button } from "@/Components/ui/button";
import { Badge } from "@/Components/ui/badge";
import { ArrowLeft, FileText, Package } from "lucide-react";
import Pagination from "@/Components/shared/Pagination";
import { formatDate } from "@/utils/format";

interface Transaction {
    id: number;
    inventory_id: number;
    type: string;
    quantity: number;
    unit_cost: number;
    total_cost: number;
    actual_time: string;
    note: string | null;
    batch_no: string | null;
    user_id: number;
    created_at: string;
    warehouse_name: string;
    user_name: string;
}

interface ShowProps extends PageProps {
    product: {
        id: number;
        code: string;
        name: string;
        unit_name: string;
    };
    transactions: {
        data: Transaction[];
        links: any[];
        total: number;
        from: number;
        to: number;
        current_page: number;
        last_page: number;
        per_page: number;
    };
    filters: {
        date_from: string;
        date_to: string;
        warehouse_id: string;
    };
    /** 報表頁面的完整篩選狀態（用於返回時恢復） */
    reportFilters: {
        date_from: string;
        date_to: string;
        warehouse_id: string;
        category_id: string;
        search: string;
        per_page: string;
        report_page: string;
    };
    warehouses: { id: number; name: string }[];
}

export default function InventoryReportShow({ product, transactions, filters, reportFilters, warehouses }: ShowProps) {

    // 類型 Badge 顏色映射
    const getTypeBadgeVariant = (type: string) => {
        switch (type) {
            case '入庫':
            case '手動入庫':
            case '調撥入庫':
                return "default";
            case '出庫':
            case '調撥出庫':
                return "destructive";
            default:
                return "secondary";
        }
    };

    return (
        <AuthenticatedLayout
            breadcrumbs={[
                { label: "報表管理", href: "#" },
                { label: "庫存報表", href: route("inventory.report.index", reportFilters) },
                { label: `${product.name} - 庫存異動明細`, href: "#", isPage: true }
            ]}
        >
            <Head title={`${product.name} - 庫存異動明細`} />

            <div className="container mx-auto p-6 max-w-7xl">
                {/* 返回按鈕 */}
                <div className="mb-6">
                    <Link href={route('inventory.report.index', reportFilters)}>
                        <Button
                            variant="outline"
                            className="gap-2 button-outlined-primary"
                        >
                            <ArrowLeft className="h-4 w-4" />
                            返回庫存報表
                        </Button>
                    </Link>
                </div>

                {/* 頁面標題 */}
                <div className="mb-6">
                    <div className="mb-4">
                        <h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
                            <FileText className="h-6 w-6 text-primary-main" />
                            庫存異動明細
                        </h1>
                        <p className="text-gray-500 mt-1">
                            查看商品「{product.name}」的所有庫存異動紀錄
                        </p>
                    </div>
                </div>

                {/* 商品資訊 & 篩選條件卡片 */}
                <div className="bg-white rounded-xl border border-gray-200 shadow-sm p-6 mb-6">
                    <div className="flex flex-col md:flex-row justify-between gap-6">

                        {/* 商品資訊 */}
                        <div className="space-y-3">
                            <div className="flex items-center gap-3">
                                <h3 className="text-xl font-bold text-grey-0">{product.name}</h3>
                                <Badge variant="outline" className="text-sm px-2 py-0.5 bg-gray-50">
                                    {product.code}
                                </Badge>
                            </div>
                            <div className="flex items-center gap-6 text-sm text-gray-500">
                                <span className="flex items-center gap-1.5">
                                    <Package className="h-4 w-4" />
                                    單位: {product.unit_name}
                                </span>
                            </div>
                        </div>

                        {/* 目前篩選條件 (唯讀) */}
                        <div className="bg-gray-50 rounded-lg p-4 space-y-2 min-w-[280px]">
                            <h4 className="text-xs font-semibold text-gray-500 uppercase tracking-wider mb-2">
                                目前篩選條件
                            </h4>
                            <div className="space-y-1.5 text-sm">
                                <div className="flex justify-between">
                                    <span className="text-gray-500">日期範圍:</span>
                                    <span className="font-medium text-grey-0">
                                        {filters.date_from && filters.date_to
                                            ? `${filters.date_from} ~ ${filters.date_to}`
                                            : filters.date_from ? `${filters.date_from} 起`
                                                : filters.date_to ? `${filters.date_to} 止`
                                                    : '全部期間'}
                                    </span>
                                </div>
                                <div className="flex justify-between">
                                    <span className="text-gray-500">倉庫:</span>
                                    <span className="font-medium text-grey-0">
                                        {filters.warehouse_id
                                            ? warehouses.find(w => w.id.toString() === filters.warehouse_id)?.name || '未指定'
                                            : '全部倉庫'}
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                {/* 異動紀錄表格 */}
                <div className="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden">
                    <div className="p-6">
                        <div className="flex items-center justify-between mb-4">
                            <h3 className="text-lg font-medium text-grey-0 flex items-center gap-2">
                                <FileText className="h-5 w-5 text-gray-400" />
                                異動紀錄
                            </h3>
                            <span className="text-sm text-gray-500">
                                共 {transactions.total} 筆紀錄
                            </span>
                        </div>

                        <Table>
                            <TableHeader className="bg-gray-50">
                                <TableRow>
                                    <TableHead className="w-[50px] text-center">#</TableHead>
                                    <TableHead className="w-[160px]">異動時間</TableHead>
                                    <TableHead>類型</TableHead>
                                    <TableHead>倉庫</TableHead>
                                    <TableHead className="text-right">異動數量</TableHead>
                                    <TableHead>批號</TableHead>
                                    <TableHead>經手人</TableHead>
                                    <TableHead className="w-[200px]">備註</TableHead>
                                </TableRow>
                            </TableHeader>
                            <TableBody>
                                {transactions.data.length === 0 ? (
                                    <TableRow>
                                        <TableCell colSpan={8} className="text-center py-8 text-gray-500">
                                            無符合條件的資料
                                        </TableCell>
                                    </TableRow>
                                ) : (
                                    transactions.data.map((tx, index) => (
                                        <TableRow key={tx.id}>
                                            <TableCell className="text-gray-500 font-medium text-center">
                                                {(transactions.from || 0) + index}
                                            </TableCell>
                                            <TableCell className="font-medium text-gray-700">
                                                {formatDate(tx.actual_time)}
                                            </TableCell>
                                            <TableCell>
                                                <Badge variant={getTypeBadgeVariant(tx.type)}>
                                                    {tx.type}
                                                </Badge>
                                            </TableCell>
                                            <TableCell>{tx.warehouse_name}</TableCell>
                                            <TableCell className={`text-right font-medium ${tx.quantity > 0 ? 'text-emerald-600' :
                                                tx.quantity < 0 ? 'text-red-600' : 'text-gray-500'
                                                }`}>
                                                {tx.quantity > 0 ? '+' : ''}{tx.quantity}
                                            </TableCell>
                                            <TableCell className="text-gray-500">{tx.batch_no || '-'}</TableCell>
                                            <TableCell>{tx.user_name || '-'}</TableCell>
                                            <TableCell className="text-gray-500 truncate max-w-[200px]" title={tx.note || ''}>
                                                {tx.note || '-'}
                                            </TableCell>
                                        </TableRow>
                                    ))
                                )}
                            </TableBody>
                        </Table>
                    </div>

                    {/* 底部分頁列 */}
                    <div className="px-6 pb-6">
                        <div className="mt-4 flex flex-col sm:flex-row items-center justify-between gap-4">
                            <span className="text-sm text-gray-500">共 {transactions.total} 筆紀錄</span>
                            <Pagination links={transactions.links} />
                        </div>
                    </div>
                </div>
            </div>
        </AuthenticatedLayout>
    );
}
