'use client';

import {
  ChangeEventHandler,
  useCallback,
  useEffect,
  useMemo,
  useState,
} from 'react';
import useSWR from 'swr';
import { Icon } from '@iconify/react';
import { ColumnDef, ColumnSort, SortingState } from '@tanstack/react-table';
import { useFormik } from 'formik';
import Button from '@/components/Button';
import Table from '@/components/Table';
import RequirePermission from '@/components/helper/RequirePermission';
import DebouncedTextInput from '@/components/input/DebouncedTextInput';
import SelectInput, { useSelect } from '@/components/input/SelectInput';
import { OptionType } from '@/components/input/SelectInput';
import ButtonFilter from '@/components/helper/ButtonFilter';
import Modal, { useModal } from '@/components/Modal';
import { isResponseSuccess } from '@/lib/api-helper';
import { cn, formatNumber, formatDate, formatCurrency } from '@/lib/helper';
import { InventoryAdjustmentApi } from '@/services/api/inventory';
import { WarehouseApi, ProductApi } from '@/services/api/master-data';
import { useTableFilter } from '@/services/hooks/useTableFilter';
import ConfirmationModal from '@/components/modal/ConfirmationModal';
import PopoverButton from '@/components/popover/PopoverButton';
import PopoverContent from '@/components/popover/PopoverContent';
import toast from 'react-hot-toast';
import { InventoryAdjustment } from '@/types/api/inventory/adjustment';
import { Warehouse } from '@/types/api/master-data/warehouse';
import { TRANSACTION_SUBTYPE_OPTIONS } from '@/config/constant';
import { Product } from '@/types/api/master-data/product';
import StatusBadge from '@/components/helper/StatusBadge';
import InventoryAdjustmentTableSkeleton from '@/components/pages/inventory/adjustment/skeleton/InventoryAdjustmentTableSkeleton';

import {
  AdjustmentFilterSchema,
  AdjustmentFilterType,
} from '@/components/pages/inventory/adjustment/filter/AdjustmentFilter';
import SelectInputRadio from '@/components/input/SelectInputRadio';
import { CellContext } from '@tanstack/react-table';

const RowOptionsMenu = ({
  popoverPosition = 'bottom',
  props,
  deleteClickHandler,
}: {
  popoverPosition: 'bottom' | 'top';
  props: CellContext<InventoryAdjustment, unknown>;
  deleteClickHandler: () => void;
}) => {
  const popoverId = `adjustment#${props.row.original.id}`;
  const popoverAnchorName = `--anchor-adjustment#${props.row.original.id}`;

  const closePopover = () => {
    document.getElementById(popoverId)?.hidePopover();
  };

  return (
    <div className='relative'>
      <PopoverButton
        tabIndex={0}
        variant='ghost'
        color='none'
        popoverTarget={popoverId}
        anchorName={popoverAnchorName}
      >
        <Icon icon='material-symbols:more-vert' width={16} height={16} />
      </PopoverButton>

      <PopoverContent
        id={popoverId}
        anchorName={popoverAnchorName}
        position={popoverPosition === 'bottom' ? 'bottom-start' : 'left'}
        className='w-full max-w-40 rounded-xl border border-base-content/5 shadow-sm'
      >
        <div className='flex flex-col bg-base-100 rounded-xl'>
          <RequirePermission permissions='lti.inventory.delete'>
            <Button
              onClick={() => {
                deleteClickHandler();
                closePopover();
              }}
              variant='ghost'
              color='error'
              className='p-3 justify-start text-sm font-semibold w-full focus-visible:text-error-content hover:text-error-content'
            >
              <Icon icon='mdi:delete-outline' width={20} height={20} />
              Delete
            </Button>
          </RequirePermission>
        </div>
      </PopoverContent>
    </div>
  );
};

const InventoryAdjustmentTable = () => {
  const {
    state: tableFilterState,
    updateFilter,
    setPage,
    setPageSize,
    toQueryString: getTableFilterQueryString,
  } = useTableFilter<{
    search: string;
    productCategorySort: string;
    productSort: string;
    warehouseSort: string;
    stockSort: string;
    productFilter?: OptionType<string>;
    warehouseFilter?: OptionType<string>;
    transactionTypeFilter?: OptionType<string>;
  }>({
    initial: {
      search: '',
      productCategorySort: '',
      productSort: '',
      warehouseSort: '',
      stockSort: '',
      productFilter: undefined,
      warehouseFilter: undefined,
      transactionTypeFilter: undefined,
    },
    paramMap: {
      page: 'page',
      pageSize: 'limit',
      productCategorySort: 'sort_product_category',
      productSort: 'sort_product',
      warehouseSort: 'sort_warehouse',
      stockSort: 'sort_stock',
      productFilter: 'product_id',
      warehouseFilter: 'warehouse_id',
      transactionTypeFilter: 'transaction_type',
    },
    persist: true,
    storeName: 'inventory-adjustment-table',
  });

  // ===== FILTER MODAL STATE =====
  const filterModal = useModal();

  // ===== FORMIK SETUP =====
  const formik = useFormik<AdjustmentFilterType>({
    initialValues: {
      product: tableFilterState.productFilter,
      warehouse: tableFilterState.warehouseFilter,
      transaction_type: tableFilterState.transactionTypeFilter,
    },
    validationSchema: AdjustmentFilterSchema,
    onSubmit: (values, { setSubmitting }) => {
      updateFilter('productFilter', values.product || undefined, true);
      updateFilter('warehouseFilter', values.warehouse || undefined, true);
      updateFilter(
        'transactionTypeFilter',
        values.transaction_type || undefined,
        true
      );
      filterModal.closeModal();
      setSubmitting(false);
    },
    onReset: () => {
      updateFilter('productFilter', undefined, true);
      updateFilter('warehouseFilter', undefined, true);
      updateFilter('transactionTypeFilter', undefined, true);
      filterModal.closeModal();
    },
  });

  // ===== PRODUCT OPTIONS =====
  const {
    setInputValue: setProductInputValue,
    options: productOptions,
    isLoadingOptions: isLoadingProductOptions,
    loadMore: loadMoreProducts,
  } = useSelect<Product>(
    filterModal.open ? ProductApi.basePath : null,
    'id',
    'name',
    'search'
  );

  // ===== WAREHOUSE OPTIONS =====
  const {
    setInputValue: setWarehouseInputValue,
    options: warehouseOptions,
    isLoadingOptions: isLoadingWarehouseOptions,
    loadMore: loadMoreWarehouses,
  } = useSelect<Warehouse>(
    filterModal.open ? WarehouseApi.basePath : null,
    'id',
    'name',
    'search'
  );

  // ===== TRANSACTION TYPE OPTIONS =====
  const transactionTypeOptions = useMemo(() => {
    return [
      { value: 'increase', label: 'Increase' },
      { value: 'decrease', label: 'Decrease' },
    ];
  }, []);

  // ===== FILTER HANDLERS =====
  const handleFilterProductChange = (val: OptionType | OptionType[] | null) => {
    formik.setFieldValue('product', val);
  };

  const handleFilterWarehouseChange = (
    val: OptionType | OptionType[] | null
  ) => {
    formik.setFieldValue('warehouse', val);
  };

  const handleFilterTransactionTypeChange = (
    val: OptionType | OptionType[] | null
  ) => {
    formik.setFieldValue('transaction_type', val);
  };

  // ===== HANDLE FILTER MODAL OPEN =====
  const handleFilterModalOpen = () => {
    formik.setValues({
      product: tableFilterState.productFilter ?? undefined,
      warehouse: tableFilterState.warehouseFilter ?? undefined,
      transaction_type: tableFilterState.transactionTypeFilter ?? undefined,
    });
    filterModal.openModal();
  };

  const {
    data: inventoryAdjustments,
    isLoading,
    mutate: refreshAdjustments,
  } = useSWR(
    `${InventoryAdjustmentApi.basePath}${getTableFilterQueryString()}`,
    InventoryAdjustmentApi.getAllFetcher
  );

  const singleDeleteHandler = async () => {
    setIsDeleteLoading(true);

    const response = await InventoryAdjustmentApi.delete(
      selectedAdjustment?.id as number
    );

    singleDeleteModal.closeModal();
    setIsDeleteLoading(false);

    if (isResponseSuccess(response)) {
      toast.success(response?.message || 'Successfully delete Adjustment!');
      refreshAdjustments();
    } else {
      toast.error(response?.message || 'Failed to delete Adjustment');
    }
  };

  const [sorting, setSorting] = useState<SortingState>([]);
  const [selectedAdjustment, setSelectedAdjustment] = useState<
    InventoryAdjustment | undefined
  >(undefined);
  const [isDeleteLoading, setIsDeleteLoading] = useState(false);
  const singleDeleteModal = useModal();

  const searchChangeHandler: ChangeEventHandler<HTMLInputElement> = (e) => {
    updateFilter('search', e.target.value, true);
  };

  const inventoryAdjustmentsColumns: ColumnDef<InventoryAdjustment>[] = useMemo(
    () => [
      {
        id: 'adj_number',
        header: 'No. Referensi',
        accessorFn: (row) => row.adj_number ?? '-',
      },
      {
        id: 'location',
        header: 'Lokasi',
        accessorFn: (row) => row.location?.name ?? '-',
      },
      {
        id: 'project_flock',
        header: 'Flock',
        accessorFn: (row) => row.project_flock?.flock_name ?? '-',
      },
      {
        id: 'warehouse_name',
        header: 'Gudang',
        accessorFn: (row) => row.product_warehouse?.warehouse?.name ?? '-',
      },
      {
        id: 'product_name',
        header: 'Nama Produk',
        accessorFn: (row) => row.product_warehouse?.product?.name ?? '-',
      },
      {
        id: 'quantity',
        header: 'Kuantitas',
        accessorFn: (row) => row.qty ?? '-',
        cell: (row) => {
          const value = row.row.original.increase + row.row.original.decrease;
          return <div className='text-center'>{formatNumber(value)}</div>;
        },
      },
      {
        id: 'price',
        header: 'Harga',
        accessorFn: (row) => (row.price ? formatCurrency(row.price) : '-'),
      },
      {
        id: 'grand_total',
        header: 'Grand Total',
        accessorFn: (row) =>
          row.grand_total ? formatCurrency(row.grand_total) : '-',
      },
      {
        id: 'transaction_type',
        header: 'Tipe Transaksi',
        accessorFn: (row) => row.transaction_subtype ?? '-',
        cell: (row) => {
          const subtype = row.row.original.transaction_subtype;
          const increase = row.row.original.increase;

          const getSubtypeLabel = (subtypeValue: string): string => {
            if (subtypeValue === TRANSACTION_SUBTYPE_OPTIONS.PEMBELIAN.value) {
              return TRANSACTION_SUBTYPE_OPTIONS.PEMBELIAN.label;
            }
            if (subtypeValue === TRANSACTION_SUBTYPE_OPTIONS.PENJUALAN.value) {
              return TRANSACTION_SUBTYPE_OPTIONS.PENJUALAN.label;
            }
            const recordingOption = TRANSACTION_SUBTYPE_OPTIONS.RECORDING.find(
              (opt) => opt.value === subtypeValue
            );
            if (recordingOption) {
              return recordingOption.label;
            }
            if (subtypeValue === 'RECORDING_DEPLETION_OUT') {
              return 'Recording Depletion';
            }
            return subtypeValue || '-';
          };

          const label = getSubtypeLabel(subtype);

          return (
            <StatusBadge
              color={
                increase > 0 ? 'success' : increase <= 0 ? 'error' : 'neutral'
              }
              text={label}
              className={{
                badge: 'whitespace-nowrap',
              }}
            />
          );
        },
      },
      {
        id: 'created_at',
        header: 'Tanggal',
        accessorFn: (row) =>
          row.created_at ? formatDate(row.created_at, 'DD MMM YYYY') : '-',
      },
      {
        id: 'created_by',
        header: 'Oleh',
        accessorFn: (row) => row.created_user?.name ?? '-',
      },
      {
        id: 'actions',
        header: 'Aksi',
        cell: (props: CellContext<InventoryAdjustment, unknown>) => {
          const currentPageSize =
            props.table.getPaginationRowModel().rows.length;
          const currentPageRows = props.table.getPaginationRowModel().flatRows;
          const currentRowRelativeIndex =
            currentPageRows.findIndex((r) => r.id === props.row.id) + 1;

          const isLast2Rows = currentRowRelativeIndex > currentPageSize - 2;

          const deleteClickHandler = () => {
            setSelectedAdjustment(props.row.original);
            singleDeleteModal.openModal();
          };

          return (
            <RowOptionsMenu
              props={props}
              deleteClickHandler={deleteClickHandler}
              popoverPosition={isLast2Rows ? 'top' : 'bottom'}
            />
          );
        },
      },
    ],
    [
      tableFilterState.pageSize,
      tableFilterState.page,
      singleDeleteModal,
      setSelectedAdjustment,
    ]
  );

  const updateSortingFilter = useCallback(
    (
      sortName: Exclude<keyof typeof tableFilterState, 'page' | 'pageSize'>,
      sortFilter: ColumnSort | undefined
    ) => {
      if (!sortFilter) {
        updateFilter(sortName, '');
      } else {
        updateFilter(sortName, sortFilter.desc ? 'desc' : 'asc');
      }
    },
    [updateFilter]
  );

  useEffect(() => {
    const productCategorySortFilter = sorting.find(
      (sortItem) => sortItem.id === 'productCategory'
    );
    const productSortFilter = sorting.find(
      (sortItem) => sortItem.id === 'product'
    );
    const warehouseSortFilter = sorting.find(
      (sortItem) => sortItem.id === 'warehouse'
    );
    const stockSortFilter = sorting.find((sortItem) => sortItem.id === 'stock');

    updateSortingFilter('productCategorySort', productCategorySortFilter);
    updateSortingFilter('productSort', productSortFilter);
    updateSortingFilter('warehouseSort', warehouseSortFilter);
    updateSortingFilter('stockSort', stockSortFilter);
  }, [sorting, updateSortingFilter]);

  return (
    <>
      <div className='w-full'>
        {/* Header Section */}
        <div className='w-full p-3 flex flex-row justify-between gap-3 flex-wrap border-b border-base-content/10'>
          {/* Action Buttons */}
          <div className='w-fit flex flex-row gap-3 flex-wrap'>
            <RequirePermission permissions='lti.inventory.create'>
              <Button
                href='/inventory/adjustment/add'
                color='primary'
                className='px-3 py-2.5 w-fit text-sm text-base-100 rounded-lg shadow-sm'
              >
                <Icon icon='heroicons:plus' width={20} height={20} />
                Add Adjustment
              </Button>
            </RequirePermission>
          </div>

          {/* Search and Filter */}
          <div className='flex flex-1 flex-row justify-start sm:justify-end items-center gap-3 flex-wrap'>
            <DebouncedTextInput
              name='search'
              placeholder='Search'
              value={tableFilterState.search ?? ''}
              onChange={searchChangeHandler}
              startAdornment={
                <Icon
                  icon='heroicons:magnifying-glass'
                  width={20}
                  height={20}
                />
              }
              className={{
                wrapper: 'w-full min-w-24 max-w-3xs',
                inputWrapper: 'rounded-xl! shadow-button-soft',
                input:
                  'placeholder:font-semibold placeholder:text-base-content/50',
              }}
            />

            <ButtonFilter
              values={tableFilterState}
              excludeFields={[
                'page',
                'pageSize',
                'search',
                'productCategorySort',
                'productSort',
                'warehouseSort',
                'stockSort',
                'productName',
                'warehouseName',
              ]}
              onClick={handleFilterModalOpen}
              className='px-3 py-2.5'
            />
          </div>
        </div>

        {/* Table Section */}
        <div className='flex flex-col mb-4'>
          {isLoading ? (
            <div className='w-full flex flex-row justify-center items-center p-4'>
              <span className='loading loading-spinner loading-xl' />
            </div>
          ) : !isResponseSuccess(inventoryAdjustments) ||
            inventoryAdjustments.data?.length === 0 ? (
            <div className='p-3'>
              <InventoryAdjustmentTableSkeleton
                columns={inventoryAdjustmentsColumns}
                icon={
                  <Icon
                    icon='heroicons:document-text'
                    className='text-white'
                    width={20}
                    height={20}
                  />
                }
              />
            </div>
          ) : (
            <Table<InventoryAdjustment>
              data={
                isResponseSuccess(inventoryAdjustments)
                  ? inventoryAdjustments?.data
                  : []
              }
              columns={inventoryAdjustmentsColumns}
              pageSize={tableFilterState.pageSize}
              page={
                isResponseSuccess(inventoryAdjustments)
                  ? inventoryAdjustments?.meta?.page
                  : 0
              }
              totalItems={
                isResponseSuccess(inventoryAdjustments)
                  ? inventoryAdjustments?.meta?.total_results
                  : 0
              }
              onPageChange={setPage}
              onPageSizeChange={setPageSize}
              isLoading={isLoading}
              sorting={sorting}
              setSorting={setSorting}
              className={{
                containerClassName: cn('p-3 mb-0'),
                headerColumnClassName: 'text-nowrap',
              }}
            />
          )}
        </div>
      </div>

      {/* Filter Modal */}
      <Modal
        ref={filterModal.ref}
        className={{
          modal: 'p-0',
          modalBox: 'p-0 rounded-[0.875rem] xl:max-w-4/12 max-w-sm',
        }}
      >
        {/* Modal Header */}
        <div className='flex items-center justify-between gap-2 border-b border-base-content/10 p-4'>
          <div className='flex items-center gap-2 text-primary'>
            <Icon icon='heroicons:funnel' width={20} height={20} />
            <h3 className='font-medium text-sm'>Filter Data</h3>
          </div>
          <Button
            variant='link'
            onClick={filterModal.closeModal}
            className='text-base-content/50 hover:text-base-content transition-colors cursor-pointer'
          >
            <Icon icon='heroicons:x-mark' width={20} height={20} />
          </Button>
        </div>
        <form onSubmit={formik.handleSubmit} onReset={formik.handleReset}>
          <div className='p-4 flex flex-col gap-1.5'>
            <SelectInput
              label='Produk'
              placeholder='Pilih Produk'
              options={productOptions}
              value={formik.values.product}
              onChange={handleFilterProductChange}
              onInputChange={setProductInputValue}
              isLoading={isLoadingProductOptions}
              isClearable
              onMenuScrollToBottom={loadMoreProducts}
              className={{ wrapper: 'w-full' }}
            />
            <SelectInput
              label='Gudang'
              placeholder='Pilih Gudang'
              options={warehouseOptions}
              value={formik.values.warehouse}
              onChange={handleFilterWarehouseChange}
              onInputChange={setWarehouseInputValue}
              isLoading={isLoadingWarehouseOptions}
              isClearable
              onMenuScrollToBottom={loadMoreWarehouses}
              className={{ wrapper: 'w-full' }}
            />
            <SelectInputRadio
              label='Tipe Transaksi'
              placeholder='Pilih Tipe Transaksi'
              options={transactionTypeOptions}
              value={formik.values.transaction_type}
              onChange={handleFilterTransactionTypeChange}
              isClearable
              className={{ wrapper: 'w-full' }}
            />
          </div>

          {/* Modal Footer */}
          <div className='flex justify-between items-center gap-4 p-4 border-t border-base-content/10 bg-gray-50'>
            <Button
              type='reset'
              variant='soft'
              className='rounded-lg text-base-content/65 bg-transparent border-none hover:bg-base-content/10 hover:text-base-content/65 transition-colors px-3 py-2'
            >
              Reset Filter
            </Button>
            <Button
              type='submit'
              className='min-w-40 text-sm rounded-lg py-3 text-white font-semibold'
              disabled={!formik.isValid || formik.isSubmitting}
            >
              Apply Filter
            </Button>
          </div>
        </form>
      </Modal>

      <ConfirmationModal
        ref={singleDeleteModal.ref}
        type='error'
        text={`Apakah anda yakin ingin menghapus data Adjustment ini?`}
        secondaryButton={{
          text: 'Tidak',
        }}
        primaryButton={{
          text: 'Ya',
          color: 'error',
          isLoading: isDeleteLoading,
          onClick: singleDeleteHandler,
        }}
      />
    </>
  );
};

export default InventoryAdjustmentTable;
