/* eslint-disable @typescript-eslint/no-explicit-any */
/* Configurations for tables */
import { computed } from "vue";
import {
  SORT_TYPE,
  SORTER_TYPES,
  FILTER_TYPES,
  ROW_SELECTION,
  type ITable,
  type ITableConfig,
} from "@actindo/core-ui";
import {
  sampleEntityApi,
  SAMPLE_STATUS,
  type ISampleEntity,
} from "@/services/api/Sample/Entity";

export enum SAMPLE_TABLE_ACTIONS {
  DELETE = "delete",
  MARK_INIT = "markInit",
  MARK_DONE = "markDone",
  MARK_FAILED = "markFailed",
}

// @todo: sample table configuration (remote API as data source)
export const SAMPLE_TABLE: ITableConfig = {
  data: sampleEntityApi.getMockedList,
  rowSelection: ROW_SELECTION.MULTI,
  hideGlobalSearch: true,
  hideFilters: true,
  hideColumnCustomization: false,
  hidePagination: true,
  sortType: SORT_TYPE.NONE,
  createActions: true,
  tableActions: [
    {
      icon: "delete",
      title: "common.delete",
      event: SAMPLE_TABLE_ACTIONS.DELETE,
      prepareIsDisabled: (table: ITable<object>) =>
        computed(() => !table.selectedRows.value.length),
    },
    {
      icon: "notification_important",
      title: "SampleView.actions.markInit",
      event: SAMPLE_TABLE_ACTIONS.MARK_INIT,
      prepareIsDisabled: (table: ITable<object>) =>
        computed(() => {
          const selectedRows: ISampleEntity[] = table.selectedRows.value;
          return (
            !selectedRows.length ||
            selectedRows.some(({ status }) => status === SAMPLE_STATUS.INIT)
          );
        }),
      prepareIsHidden: (table: ITable<object>) =>
        computed(() => {
          const selectedRows: ISampleEntity[] = table.selectedRows.value;
          return (
            selectedRows.length &&
            selectedRows.every(({ status }) => status === SAMPLE_STATUS.INIT)
          );
        }),
    },
    {
      icon: "check_circle",
      title: "SampleView.actions.markDone",
      event: SAMPLE_TABLE_ACTIONS.MARK_DONE,
      prepareIsDisabled: (table: ITable<object>) =>
        computed(() => {
          const selectedRows: ISampleEntity[] = table.selectedRows.value;
          return (
            !selectedRows.length ||
            selectedRows.some(({ status }) => status === SAMPLE_STATUS.DONE)
          );
        }),
      prepareIsHidden: (table: ITable<object>) =>
        computed(() => {
          const selectedRows: ISampleEntity[] = table.selectedRows.value;
          return (
            selectedRows.length &&
            selectedRows.every(({ status }) => status === SAMPLE_STATUS.DONE)
          );
        }),
    },
    {
      icon: "error",
      title: "SampleView.actions.markFailed",
      event: SAMPLE_TABLE_ACTIONS.MARK_FAILED,
      prepareIsDisabled: (table: ITable<object>) =>
        computed(() => {
          const selectedRows: ISampleEntity[] = table.selectedRows.value;
          return (
            !selectedRows.length ||
            selectedRows.some(({ status }) => status === SAMPLE_STATUS.FAILED)
          );
        }),
      prepareIsHidden: (table: ITable<object>) =>
        computed(() => {
          const selectedRows: ISampleEntity[] = table.selectedRows.value;
          return (
            selectedRows.length &&
            selectedRows.every(({ status }) => status === SAMPLE_STATUS.FAILED)
          );
        }),
    },
  ],
  columns: [
    {
      id: "id",
      header: "common.id",
      isVisible: false,
      minWidth: 200,
    },
    {
      id: "sample.name",
      header: "SampleView.name",
      minWidth: 200,
    },
    {
      id: "key",
      header: "SampleView.key",
      minWidth: 200,
    },
    {
      id: "validFrom",
      header: "SampleView.validFrom",
      width: 200,
      accessor: (row: any) =>
        (row as ISampleEntity).validFrom?.slice(0, 10) || "-",
    },
    {
      id: "status",
      header: "SampleView.status",
      width: 200,
    },
  ],
};

// @todo: sample table configuration (local data as source)
export const LOCAL_SAMPLE_TABLE: ITableConfig = {
  rowSelection: ROW_SELECTION.MULTI,
  hideGlobalSearch: false,
  hideFilters: false,
  hideColumnCustomization: false,
  hidePagination: false,
  sortType: SORT_TYPE.MULTI,
  createActions: true,
  tableActions: [
    {
      icon: "delete",
      title: "common.delete",
      prepareIsDisabled: (table: ITable<object>) =>
        computed(() => !table.selectedRows.value.length),
    },
  ],
  columns: [
    {
      id: "id",
      header: "common.id",
      sortable: SORTER_TYPES.NUMERIC,
      filterable: FILTER_TYPES.NUMERIC,
      minWidth: 200,
    },
    {
      id: "sample.name",
      header: "SampleView.name",
      sortable: SORTER_TYPES.STRING,
      filterable: FILTER_TYPES.STRING,
      minWidth: 200,
    },
    {
      id: "key",
      header: "SampleView.key",
      sortable: SORTER_TYPES.STRING,
      filterable: FILTER_TYPES.STRING,
      minWidth: 200,
    },
    {
      id: "validFrom",
      header: "SampleView.validFrom",
      width: 200,
      sortable: SORTER_TYPES.STRING,
      filterable: FILTER_TYPES.DATE,
      accessor: (row: any) =>
        (row as ISampleEntity).validFrom?.slice(0, 10) || "-",
    },
    {
      id: "status",
      header: "SampleView.status",
      width: 200,
      filterable: FILTER_TYPES.LIST,
      filterListValues: [
        {
          title: "SampleView.statuses.init",
          value: SAMPLE_STATUS.INIT,
        },
        {
          title: "SampleView.statuses.done",
          value: SAMPLE_STATUS.DONE,
        },
        {
          title: "SampleView.statuses.failed",
          value: SAMPLE_STATUS.FAILED,
        },
      ],
    },
  ],
};
