'use client'

// React Imports
import { useCallback, useEffect, useMemo, useState } from 'react'

// Next Imports
import Link from 'next/link'
import { useParams } from 'next/navigation'

// MUI Imports
import Button from '@mui/material/Button'
import Card from '@mui/material/Card'
import CardHeader from '@mui/material/CardHeader'
import Checkbox from '@mui/material/Checkbox'
import Chip from '@mui/material/Chip'
import IconButton from '@mui/material/IconButton'
import MenuItem from '@mui/material/MenuItem'
import { styled } from '@mui/material/styles'
import type { TextFieldProps } from '@mui/material/TextField'
import Typography from '@mui/material/Typography'

// Third-party Imports
import type { RankingInfo } from '@tanstack/match-sorter-utils'
import { rankItem } from '@tanstack/match-sorter-utils'
import type { ColumnDef, FilterFn } from '@tanstack/react-table'
import { createColumnHelper, flexRender, getCoreRowModel, useReactTable } from '@tanstack/react-table'
import classnames from 'classnames'

// Type Imports
import type { Locale } from '@configs/i18n'

// Component Imports
import CustomTextField from '@core/components/mui/TextField'
import OptionMenu from '@core/components/option-menu'

// Style Imports
import tableStyles from '@core/styles/table.module.css'
import { Box, CircularProgress, TablePagination } from '@mui/material'
import { useDispatch } from 'react-redux'
import TablePaginationComponent from '@/components/TablePaginationComponent'
import Loading from '@/components/layout/shared/Loading'
import { getLocalizedUrl } from '@/utils/i18n'
import StatusFilterTabs from '@/components/StatusFilterTab'

// Fixed Asset Type
export type FixedAssetType = {
  id: number
  assetName: string
  puchaseBill: string // Code Purchase
  reference: string
  date: string
  price: number
}

declare module '@tanstack/table-core' {
  interface FilterFns {
    fuzzy: FilterFn<unknown>
  }
  interface FilterMeta {
    itemRank: RankingInfo
  }
}

type FixedAssetTypeWithAction = FixedAssetType & {
  actions?: string
}

// Dummy data for fixed assets
const fixedAssetData: FixedAssetType[] = [
  {
    id: 1,
    assetName: 'Laptop Dell XPS 13',
    puchaseBill: 'PB-2024-001',
    reference: 'REF-001',
    date: '2024-01-15',
    price: 15000000
  },
  {
    id: 2,
    assetName: 'Office Furniture Set',
    puchaseBill: 'PB-2024-002',
    reference: 'REF-002',
    date: '2024-02-10',
    price: 8500000
  },
  {
    id: 3,
    assetName: 'Printer Canon ImageClass',
    puchaseBill: 'PB-2024-003',
    reference: 'REF-003',
    date: '2024-02-20',
    price: 3200000
  },
  {
    id: 4,
    assetName: 'Air Conditioning Unit',
    puchaseBill: 'PB-2024-004',
    reference: 'REF-004',
    date: '2024-03-05',
    price: 12000000
  },
  {
    id: 5,
    assetName: 'Conference Room TV',
    puchaseBill: 'PB-2024-005',
    reference: 'REF-005',
    date: '2024-03-15',
    price: 7500000
  },
  {
    id: 6,
    assetName: 'MacBook Pro 16"',
    puchaseBill: 'PB-2024-006',
    reference: 'REF-006',
    date: '2024-04-01',
    price: 28000000
  },
  {
    id: 7,
    assetName: 'Standing Desk Electric',
    puchaseBill: 'PB-2024-007',
    reference: 'REF-007',
    date: '2024-04-15',
    price: 4500000
  },
  {
    id: 8,
    assetName: 'Server HP ProLiant',
    puchaseBill: 'PB-2024-008',
    reference: 'REF-008',
    date: '2024-05-01',
    price: 45000000
  }
]

// Styled Components
const Icon = styled('i')({})

const fuzzyFilter: FilterFn<any> = (row, columnId, value, addMeta) => {
  // Rank the item
  const itemRank = rankItem(row.getValue(columnId), value)

  // Store the itemRank info
  addMeta({
    itemRank
  })

  // Return if the item should be filtered in/out
  return itemRank.passed
}

const DebouncedInput = ({
  value: initialValue,
  onChange,
  debounce = 500,
  ...props
}: {
  value: string | number
  onChange: (value: string | number) => void
  debounce?: number
} & Omit<TextFieldProps, 'onChange'>) => {
  // States
  const [value, setValue] = useState(initialValue)

  useEffect(() => {
    setValue(initialValue)
  }, [initialValue])

  useEffect(() => {
    const timeout = setTimeout(() => {
      onChange(value)
    }, debounce)

    return () => clearTimeout(timeout)
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [value])

  return <CustomTextField {...props} value={value} onChange={e => setValue(e.target.value)} />
}

// Format currency
const formatCurrency = (amount: number) => {
  return new Intl.NumberFormat('id-ID', {
    style: 'currency',
    currency: 'IDR',
    minimumFractionDigits: 0
  }).format(amount)
}

// Format date
const formatDate = (dateString: string) => {
  return new Date(dateString).toLocaleDateString('id-ID', {
    day: '2-digit',
    month: '2-digit',
    year: 'numeric'
  })
}

// Column Definitions
const columnHelper = createColumnHelper<FixedAssetTypeWithAction>()

const FixedAssetTable = () => {
  const dispatch = useDispatch()

  // States
  const [addAssetOpen, setAddAssetOpen] = useState(false)
  const [rowSelection, setRowSelection] = useState({})
  const [currentPage, setCurrentPage] = useState(0)
  const [pageSize, setPageSize] = useState(10)
  const [openConfirm, setOpenConfirm] = useState(false)
  const [assetId, setAssetId] = useState('')
  const [search, setSearch] = useState('')
  const [filteredData, setFilteredData] = useState<FixedAssetType[]>([])
  const [statusFilter, setStatusFilter] = useState<string>('Draft')

  // Hooks
  const { lang: locale } = useParams()

  // Initialize data on component mount
  useEffect(() => {
    console.log('Initial fixedAssetData:', fixedAssetData)
    setFilteredData(fixedAssetData)
  }, [])

  // Filter data based on search
  useEffect(() => {
    let filtered = [...fixedAssetData]

    // Filter by search
    if (search) {
      filtered = filtered.filter(
        asset =>
          asset.assetName.toLowerCase().includes(search.toLowerCase()) ||
          asset.puchaseBill.toLowerCase().includes(search.toLowerCase()) ||
          asset.reference.toLowerCase().includes(search.toLowerCase())
      )
    }

    console.log('Filtered data:', filtered) // Debug log
    setFilteredData(filtered)
    setCurrentPage(0)
  }, [search])

  const totalCount = filteredData.length
  const paginatedData = useMemo(() => {
    const startIndex = currentPage * pageSize
    return filteredData.slice(startIndex, startIndex + pageSize)
  }, [filteredData, currentPage, pageSize])

  // Calculate total value from filtered data
  const totalValue = useMemo(() => {
    return filteredData.reduce((sum, asset) => sum + asset.price, 0)
  }, [filteredData])

  const handlePageChange = useCallback((event: unknown, newPage: number) => {
    setCurrentPage(newPage)
  }, [])

  const handlePageSizeChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
    const newPageSize = parseInt(event.target.value, 10)
    setPageSize(newPageSize)
    setCurrentPage(0)
  }, [])

  const handleDelete = () => {
    setOpenConfirm(false)
  }

  const handleAssetClick = (assetId: string) => {
    console.log('Navigasi ke detail Asset:', assetId)
  }

  const handleStatusFilter = (status: string) => {
    setStatusFilter(status)
  }

  const columns = useMemo<ColumnDef<FixedAssetTypeWithAction, any>[]>(
    () => [
      {
        id: 'select',
        header: ({ table }) => (
          <Checkbox
            {...{
              checked: table.getIsAllRowsSelected(),
              indeterminate: table.getIsSomeRowsSelected(),
              onChange: table.getToggleAllRowsSelectedHandler()
            }}
          />
        ),
        cell: ({ row }) => (
          <Checkbox
            {...{
              checked: row.getIsSelected(),
              disabled: !row.getCanSelect(),
              indeterminate: row.getIsSomeSelected(),
              onChange: row.getToggleSelectedHandler()
            }}
          />
        )
      },
      columnHelper.accessor('puchaseBill', {
        header: 'Kode Pembelian',
        cell: ({ row }) => (
          <Button
            variant='text'
            color='primary'
            className='p-0 min-w-0 font-medium normal-case justify-start'
            component={Link}
            href={getLocalizedUrl(`/apps/fixed-asset/${row.original.id}/detail`, locale as Locale)}
            sx={{
              textTransform: 'none',
              fontWeight: 500,
              '&:hover': {
                textDecoration: 'underline',
                backgroundColor: 'transparent'
              }
            }}
          >
            {row.original.puchaseBill}
          </Button>
        )
      }),
      columnHelper.accessor('assetName', {
        header: 'Nama Aset',
        cell: ({ row }) => (
          <Typography color='text.primary' className='font-medium'>
            {row.original.assetName}
          </Typography>
        )
      }),
      columnHelper.accessor('reference', {
        header: 'Referensi',
        cell: ({ row }) => <Typography color='text.secondary'>{row.original.reference || '-'}</Typography>
      }),
      columnHelper.accessor('date', {
        header: 'Tanggal Pembelian',
        cell: ({ row }) => <Typography>{formatDate(row.original.date)}</Typography>
      }),
      columnHelper.accessor('price', {
        header: 'Harga',
        cell: ({ row }) => (
          <Typography className='font-medium text-primary'>{formatCurrency(row.original.price)}</Typography>
        )
      })
    ],
    [locale]
  )

  const table = useReactTable({
    data: paginatedData as FixedAssetType[],
    columns,
    filterFns: {
      fuzzy: fuzzyFilter
    },
    state: {
      rowSelection,
      pagination: {
        pageIndex: currentPage,
        pageSize
      }
    },
    enableRowSelection: true,
    onRowSelectionChange: setRowSelection,
    getCoreRowModel: getCoreRowModel(),
    manualPagination: true,
    pageCount: Math.ceil(totalCount / pageSize)
  })

  return (
    <>
      <Card>
        {/* Header */}
        <div className='p-6 border-bs'>
          <StatusFilterTabs
            statusOptions={['Draft', 'Terdaftar', 'Terjual/Dilepaskan']}
            selectedStatus={statusFilter}
            onStatusChange={handleStatusFilter}
          />
        </div>

        <div className='flex justify-between flex-col items-start md:flex-row md:items-center p-6 border-bs gap-4'>
          <DebouncedInput
            value={search}
            onChange={value => setSearch(value as string)}
            placeholder='Cari Aset Tetap'
            className='max-sm:is-full'
          />
          <div className='flex flex-col sm:flex-row max-sm:is-full items-start sm:items-center gap-4'>
            <CustomTextField
              select
              value={pageSize}
              onChange={handlePageSizeChange}
              className='max-sm:is-full sm:is-[70px]'
            >
              <MenuItem value={10}>10</MenuItem>
              <MenuItem value={25}>25</MenuItem>
              <MenuItem value={50}>50</MenuItem>
            </CustomTextField>
            <Button
              color='secondary'
              variant='tonal'
              startIcon={<i className='tabler-upload' />}
              className='max-sm:is-full'
            >
              Ekspor
            </Button>
            <Button
              variant='contained'
              component={Link}
              className='max-sm:is-full is-auto'
              startIcon={<i className='tabler-plus' />}
              href={getLocalizedUrl('/apps/fixed-assets/add', locale as Locale)}
            >
              Tambah Aset
            </Button>
          </div>
        </div>

        <div className='overflow-x-auto'>
          <table className={tableStyles.table}>
            <thead>
              {table.getHeaderGroups().map(headerGroup => (
                <tr key={headerGroup.id}>
                  {headerGroup.headers.map(header => (
                    <th key={header.id}>
                      {header.isPlaceholder ? null : (
                        <>
                          <div
                            className={classnames({
                              'flex items-center': header.column.getIsSorted(),
                              'cursor-pointer select-none': header.column.getCanSort()
                            })}
                            onClick={header.column.getToggleSortingHandler()}
                          >
                            {flexRender(header.column.columnDef.header, header.getContext())}
                            {{
                              asc: <i className='tabler-chevron-up text-xl' />,
                              desc: <i className='tabler-chevron-down text-xl' />
                            }[header.column.getIsSorted() as 'asc' | 'desc'] ?? null}
                          </div>
                        </>
                      )}
                    </th>
                  ))}
                </tr>
              ))}
            </thead>
            {filteredData.length === 0 ? (
              <tbody>
                <tr>
                  <td colSpan={table.getVisibleFlatColumns().length} className='text-center'>
                    Tidak ada data tersedia
                  </td>
                </tr>
              </tbody>
            ) : (
              <tbody>
                {table.getRowModel().rows.map(row => {
                  return (
                    <tr key={row.id} className={classnames({ selected: row.getIsSelected() })}>
                      {row.getVisibleCells().map(cell => (
                        <td key={cell.id}>{flexRender(cell.column.columnDef.cell, cell.getContext())}</td>
                      ))}
                    </tr>
                  )
                })}

                {/* Total Row */}
                <tr className='border-t-2 bg-gray-50'>
                  <td className='font-bold text-lg py-4'>
                    <Typography variant='h6' className='font-bold'>
                      Total Nilai Aset
                    </Typography>
                  </td>
                  <td></td>
                  <td></td>
                  <td></td>
                  <td></td>
                  <td className='font-bold text-lg py-4'>
                    <Typography variant='h6' className='font-bold text-primary'>
                      {formatCurrency(totalValue)}
                    </Typography>
                  </td>
                  <td></td>
                </tr>
              </tbody>
            )}
          </table>
        </div>

        <TablePagination
          component={() => (
            <TablePaginationComponent
              pageIndex={currentPage}
              pageSize={pageSize}
              totalCount={totalCount}
              onPageChange={handlePageChange}
            />
          )}
          count={totalCount}
          rowsPerPage={pageSize}
          page={currentPage}
          onPageChange={handlePageChange}
          onRowsPerPageChange={handlePageSizeChange}
          rowsPerPageOptions={[10, 25, 50]}
        />
      </Card>
    </>
  )
}

export default FixedAssetTable
