import { useState, useCallback } from "react";
import { Button } from "@/Components/ui/button";
import { Input } from "@/Components/ui/input";
import { Label } from "@/Components/ui/label";
import {
    Filter,
    Package,
    RotateCcw,
    BarChart3,
    AlertTriangle,
    CheckCircle2,
    Clock,
    ArrowUpDown,
    ArrowUp,
    ArrowDown,
    XCircle
} from 'lucide-react';
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
import { Head, router } from "@inertiajs/react";
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from "@/Components/ui/table";
import Pagination from "@/Components/shared/Pagination";
import { SearchableSelect } from "@/Components/ui/searchable-select";
import { PageProps } from "@/types/global";

import {
    Tooltip,
    TooltipContent,
    TooltipProvider,
    TooltipTrigger,
} from "@/Components/ui/tooltip";
import { StatusBadge, StatusVariant } from "@/Components/shared/StatusBadge";

interface AnalysisItem {
    id: number;
    code: string;
    name: string;
    category_name: string;
    current_stock: string; // decimal string from DB
    sales_30d: string;
    last_sale_date: string | null;
    turnover_days: number;
    turnover_days_display: string;
    status: 'dead' | 'slow' | 'normal' | 'out_of_stock';
    status_label: string;
}

interface KPIProps {
    total_stock_value: number;
    dead_stock_value: number;
    dead_stock_count: number;
    avg_turnover_days: number;
}

interface PagePropsWithData extends PageProps {
    analysisData: {
        data: AnalysisItem[];
        links: any[];
        total: number;
        from: number;
        to: number;
        current_page: number;
    };
    kpis: KPIProps;
    warehouses: { id: number; name: string }[];
    categories: { id: number; name: string }[];
    filters: {
        warehouse_id?: string;
        category_id?: string;
        search?: string;
        per_page?: string;
        sort_by?: string;
        sort_order?: 'asc' | 'desc';
        status?: string;
    };
}

// Define status mapping
const getStatusVariant = (status: string): StatusVariant => {
    switch (status) {
        case 'dead': return 'destructive';
        case 'slow': return 'warning';
        case 'normal': return 'success';
        case 'out_of_stock': return 'neutral';
        default: return 'neutral';
    }
};

const getStatusLabel = (status: string): string => {
    switch (status) {
        case 'dead': return '滯銷';
        case 'slow': return '週轉慢';
        case 'normal': return '正常';
        case 'out_of_stock': return '缺貨';
        default: return status;
    }
};

const statusOptions = [
    { label: "全部狀態", value: "all" },
    { label: "滯銷 (>90天)", value: "dead" },
    { label: "週轉慢 (>60天)", value: "slow" },
    { label: "正常", value: "normal" }
];

export default function InventoryAnalysisIndex({ analysisData, kpis, warehouses, categories, filters }: PagePropsWithData) {
    const [warehouseId, setWarehouseId] = useState(filters.warehouse_id || "all");
    const [categoryId, setCategoryId] = useState(filters.category_id || "all");
    const [search, setSearch] = useState(filters.search || "");
    const [status, setStatus] = useState(filters.status || "all");
    const [perPage, setPerPage] = useState(filters.per_page?.toString() || "10");

    const handleFilter = useCallback(() => {
        router.get(
            route("inventory.analysis.index"),
            {
                warehouse_id: warehouseId === "all" ? "" : warehouseId,
                category_id: categoryId === "all" ? "" : categoryId,
                status: status === "all" ? "" : status,
                search: search,
                per_page: perPage,
                sort_by: filters.sort_by,
                sort_order: filters.sort_order,
            },
            { preserveState: true, preserveScroll: true }
        );
    }, [warehouseId, categoryId, status, search, perPage, filters.sort_by, filters.sort_order]);

    const handleClearFilters = () => {
        setWarehouseId("all");
        setCategoryId("all");
        setStatus("all");
        setSearch("");
        setPerPage("10");
        router.get(route("inventory.analysis.index"));
    };

    const handleSort = (field: string) => {
        let newSortBy: string | undefined = field;
        let newSortOrder: 'asc' | 'desc' | undefined = 'asc';

        if (filters.sort_by === field) {
            if (filters.sort_order === 'asc') {
                newSortOrder = 'desc';
            } else {
                newSortBy = undefined;
                newSortOrder = undefined;
            }
        } else {
            // Default sort order for numeric fields might be desc
            if (['turnover_days', 'current_stock', 'sales_30d'].includes(field)) {
                newSortOrder = 'desc';
            }
        }

        router.get(
            route("inventory.analysis.index"),
            {
                warehouse_id: warehouseId === "all" ? "" : warehouseId,
                category_id: categoryId === "all" ? "" : categoryId,
                status: status === "all" ? "" : status,
                search: search,
                per_page: perPage,
                sort_by: newSortBy,
                sort_order: newSortOrder,
            },
            { preserveState: true, preserveScroll: true }
        );
    };

    const handlePerPageChange = (value: string) => {
        setPerPage(value);
        // Trigger filter immediately
        router.get(
            route("inventory.analysis.index"),
            {
                warehouse_id: warehouseId === "all" ? "" : warehouseId,
                category_id: categoryId === "all" ? "" : categoryId,
                status: status === "all" ? "" : status,
                search: search,
                per_page: value,
                sort_by: filters.sort_by,
                sort_order: filters.sort_order,
            },
            { preserveState: true, preserveScroll: true }
        );
    };

    const SortIcon = ({ field }: { field: string }) => {
        if (filters.sort_by !== field) {
            return <ArrowUpDown className="h-4 w-4 text-gray-300 ml-1" />;
        }
        if (filters.sort_order === "asc") {
            return <ArrowUp className="h-4 w-4 text-primary-main ml-1" />;
        }
        return <ArrowDown className="h-4 w-4 text-primary-main ml-1" />;
    };

    return (
        <AuthenticatedLayout breadcrumbs={[{ label: "報表管理", href: "#" }, { label: "庫存分析", href: route("inventory.analysis.index"), isPage: true }]}>
            <Head title="庫存分析" />

            <div className="container mx-auto p-6 max-w-7xl">
                {/* Header */}
                <div className="flex flex-col md:flex-row md:items-center justify-between gap-4 mb-6">
                    <div>
                        <h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
                            <BarChart3 className="h-6 w-6 text-primary-main" />
                            庫存分析
                        </h1>
                        <p className="text-gray-500 mt-1">
                            分析商品庫存週轉率、滯銷品項與庫存健康度
                        </p>
                    </div>
                </div>

                {/* KPI Cards */}
                <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
                    <div className="bg-white p-4 rounded-xl shadow-sm border border-gray-100 flex items-center gap-4">
                        <div className="p-3 bg-blue-50 rounded-lg text-blue-600">
                            <Clock className="w-6 h-6" />
                        </div>
                        <div>
                            <p className="text-sm text-gray-500 font-medium">平均週轉天數</p>
                            <p className="text-2xl font-bold text-gray-900">{kpis.avg_turnover_days} <span className="text-sm font-normal text-gray-500">天</span></p>
                        </div>
                    </div>

                    <div className="bg-white p-4 rounded-xl shadow-sm border border-gray-100 flex items-center gap-4">
                        <div className="p-3 bg-red-50 rounded-lg text-red-600">
                            <AlertTriangle className="w-6 h-6" />
                        </div>
                        <div>
                            <p className="text-sm text-gray-500 font-medium">滯銷品項數</p>
                            <p className="text-2xl font-bold text-gray-900">{kpis.dead_stock_count} <span className="text-sm font-normal text-gray-500">項</span></p>
                        </div>
                    </div>

                    <div className="bg-white p-4 rounded-xl shadow-sm border border-gray-100 flex items-center gap-4">
                        <div className="p-3 bg-orange-50 rounded-lg text-orange-600">
                            <XCircle className="w-6 h-6" />
                        </div>
                        <div>
                            <p className="text-sm text-gray-500 font-medium">滯銷庫存成本</p>
                            <TooltipProvider>
                                <Tooltip>
                                    <TooltipTrigger>
                                        <p className="text-2xl font-bold text-gray-900 cursor-help">${Number(kpis.dead_stock_value).toLocaleString()}</p>
                                    </TooltipTrigger>
                                    <TooltipContent>
                                        <p>在此定義為庫存大於 0 且超過 90 天未銷售的商品成本總和</p>
                                    </TooltipContent>
                                </Tooltip>
                            </TooltipProvider>
                        </div>
                    </div>

                    <div className="bg-white p-4 rounded-xl shadow-sm border border-gray-100 flex items-center gap-4">
                        <div className="p-3 bg-emerald-50 rounded-lg text-emerald-600">
                            <CheckCircle2 className="w-6 h-6" />
                        </div>
                        <div>
                            <p className="text-sm text-gray-500 font-medium">庫存總成本</p>
                            <p className="text-2xl font-bold text-gray-900">${Number(kpis.total_stock_value).toLocaleString()}</p>
                        </div>
                    </div>
                </div>

                {/* Filters */}
                <div className="bg-white rounded-xl shadow-sm border border-grey-4 p-5 mb-6">
                    <div className="grid grid-cols-1 md:grid-cols-12 gap-4 items-end">
                        {/* Search */}
                        <div className="md:col-span-3 space-y-1">
                            <Label className="text-xs text-grey-2 font-medium">關鍵字</Label>
                            <Input
                                placeholder="搜尋商品代碼或名稱..."
                                value={search}
                                onChange={(e) => setSearch(e.target.value)}
                                className="h-9 bg-white"
                                onKeyDown={(e) => e.key === 'Enter' && handleFilter()}
                            />
                        </div>

                        {/* Warehouse & Category */}
                        <div className="md:col-span-2 space-y-1">
                            <Label className="text-xs text-grey-2 font-medium">倉庫</Label>
                            <SearchableSelect
                                value={warehouseId}
                                onValueChange={setWarehouseId}
                                options={[{ label: "全部倉庫", value: "all" }, ...warehouses.map(w => ({ label: w.name, value: w.id.toString() }))]}
                                className="w-full h-9"
                                placeholder="選擇倉庫..."
                            />
                        </div>
                        <div className="md:col-span-2 space-y-1">
                            <Label className="text-xs text-grey-2 font-medium">分類</Label>
                            <SearchableSelect
                                value={categoryId}
                                onValueChange={setCategoryId}
                                options={[{ label: "全部分類", value: "all" }, ...categories.map(c => ({ label: c.name, value: c.id.toString() }))]}
                                className="w-full h-9"
                                placeholder="選擇分類..."
                            />
                        </div>

                        <div className="md:col-span-2 space-y-1">
                            <Label className="text-xs text-grey-2 font-medium">狀態</Label>
                            <SearchableSelect
                                value={status}
                                onValueChange={setStatus}
                                options={statusOptions}
                                className="w-full h-9"
                                placeholder="選擇狀態..."
                                showSearch={false}
                            />
                        </div>



                        {/* Action Buttons Integrated */}
                        <div className="md:col-span-3 flex items-center gap-2">
                            <Button
                                variant="outline"
                                onClick={handleClearFilters}
                                className="flex-1 items-center gap-2 button-outlined-primary h-9"
                            >
                                <RotateCcw className="h-4 w-4" />
                                重置
                            </Button>
                            <Button
                                onClick={handleFilter}
                                className="flex-1 button-filled-primary h-9 gap-2"
                            >
                                <Filter className="h-4 w-4" /> 查詢
                            </Button>
                        </div>
                    </div>
                </div>

                {/* Results Table */}
                <div className="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden">
                    <Table>
                        <TableHeader className="bg-gray-50">
                            <TableRow>
                                <TableHead className="w-[120px] cursor-pointer" onClick={() => handleSort('products.code')}>
                                    <div className="flex items-center">商品代碼 <SortIcon field="products.code" /></div>
                                </TableHead>
                                <TableHead className="cursor-pointer" onClick={() => handleSort('products.name')}>
                                    <div className="flex items-center">商品名稱 <SortIcon field="products.name" /></div>
                                </TableHead>
                                <TableHead className="w-[100px]">分類</TableHead>
                                <TableHead className="text-right w-[100px] cursor-pointer" onClick={() => handleSort('current_stock')}>
                                    <div className="flex items-center justify-end">現有庫存 <SortIcon field="current_stock" /></div>
                                </TableHead>
                                <TableHead className="text-right w-[100px] cursor-pointer" onClick={() => handleSort('sales_30d')}>
                                    <div className="flex items-center justify-end">30天銷量 <SortIcon field="sales_30d" /></div>
                                </TableHead>
                                <TableHead className="text-right w-[120px] cursor-pointer" onClick={() => handleSort('turnover_days')}>
                                    <div className="flex items-center justify-end">週轉天數 <SortIcon field="turnover_days" /></div>
                                </TableHead>
                                <TableHead className="text-right w-[120px] cursor-pointer" onClick={() => handleSort('last_sale_date')}>
                                    <div className="flex items-center justify-end">最後銷售 <SortIcon field="last_sale_date" /></div>
                                </TableHead>
                                <TableHead className="w-[100px] text-center">狀態</TableHead>
                            </TableRow>
                        </TableHeader>
                        <TableBody>
                            {analysisData.data.length === 0 ? (
                                <TableRow>
                                    <TableCell colSpan={8}>
                                        <div className="flex flex-col items-center justify-center space-y-2 py-8 text-gray-400">
                                            <Package className="h-10 w-10 opacity-20" />
                                            <p>無符合條件的資料</p>
                                        </div>
                                    </TableCell>
                                </TableRow>
                            ) : (
                                analysisData.data.map((row) => (
                                    <TableRow key={row.id} className="hover:bg-gray-50/50 transition-colors">
                                        <TableCell className="font-medium text-gray-900">
                                            {row.code}
                                        </TableCell>
                                        <TableCell className="text-gray-700">
                                            {row.name}
                                        </TableCell>
                                        <TableCell className="text-gray-500">{row.category_name || '-'}</TableCell>
                                        <TableCell className="text-right font-medium">
                                            {Number(row.current_stock).toLocaleString()}
                                        </TableCell>
                                        <TableCell className="text-right text-gray-600">
                                            {Number(row.sales_30d).toLocaleString()}
                                        </TableCell>
                                        <TableCell className="text-right font-bold text-gray-800">
                                            {row.turnover_days_display}
                                        </TableCell>
                                        <TableCell className="text-right text-gray-500 text-sm">
                                            {row.last_sale_date ? row.last_sale_date.split(' ')[0] : '從未銷售'}
                                        </TableCell>
                                        <TableCell className="text-center">
                                            <StatusBadge variant={getStatusVariant(row.status)}>
                                                {getStatusLabel(row.status)}
                                            </StatusBadge>
                                        </TableCell>
                                    </TableRow>
                                ))
                            )}
                        </TableBody>
                    </Table>
                </div>

                {/* Pagination Footer */}
                <div className="mt-6 flex flex-col sm:flex-row items-center justify-between gap-4">
                    <div className="flex items-center gap-4">
                        <div className="flex items-center gap-2 text-sm text-gray-500">
                            <span>每頁顯示</span>
                            <SearchableSelect
                                value={perPage}
                                onValueChange={handlePerPageChange}
                                options={[
                                    { label: "10", value: "10" },
                                    { label: "20", value: "20" },
                                    { label: "50", value: "50" },
                                    { label: "100", value: "100" }
                                ]}
                                className="w-[100px] h-8"
                                showSearch={false}
                            />
                            <span>筆</span>
                        </div>
                        <span className="text-sm text-gray-500">共 {analysisData.total} 筆資料</span>
                    </div>
                    <div className="w-full sm:w-auto flex justify-center sm:justify-end">
                        <Pagination links={analysisData.links} />
                    </div>
                </div>
            </div>
        </AuthenticatedLayout>
    );
}
