import {
  FieldDataType,
  ModelsFieldsTypeMap,
  ModelType,
} from '@gadmin2n/react-common';
import { CrudFilters, CrudSorting } from '@refinedev/core';
import { FormProps, SelectProps, TableProps } from 'antd';
import { DefaultOptionType } from 'antd/es/select';

export interface ListPageConfig {
  resourceName: string;
  model: ModelType;
  tableConfig: {
    fields: string[];
    toolbar: {
      searchBar: {
        fields: string[];
        placeholder?: string;
        containsFields?: string[];
      };
    };
    header: {
      filter: string[];
      sorter: string[];
    };
    rowSelection: {
      actions: RowAction[];
    };
    rowActions: {
      actions: RowAction[];
    };
  };
  prismaSelect?: any;
  modelsMap: ModelsFieldsTypeMap;
  modelsEnum: Record<string, Record<string, string>>;
}

export interface RowAction {
  action:
    | 'CHANGE_STATUS'
    | 'DELETE'
    | 'EXPORT'
    | 'CUSTOM'
    | 'INSERT'
    | 'JUMP'
    | 'EDIT'
    | 'DETAIL';
  desc: string;
  values?: Record<string, any>;
  when?: Record<string, any> | '*' | ((record: any) => boolean);
  fields?: string[];
}

export interface BatchOperationsConfig {
  resourceName: string;
  apiUrl: string;
  requestHeaders?: () => Record<string, string>;
  onSuccess?: () => void;
}

export interface BatchOperationsResult {
  selectedRowKeys: React.Key[];
  setSelectedRowKeys: React.Dispatch<React.SetStateAction<React.Key[]>>;
  rowSelection: {
    selectedRowKeys: React.Key[];
    onChange: (keys: React.Key[]) => void;
  };
  hasSelected: boolean;
  updateSelectedItems: (values: any) => Promise<void>;
  deleteSelectedItems: () => Promise<void>;
  updateManyIsLoading: boolean;
  deleteManyIsLoading: boolean;
}

export interface SearchFormConfig {
  searchFields: string[];
  dateRangeFields?: string[];
  model: ModelType;
  tableConfig: ListPageConfig['tableConfig'];
}

export interface ListPageContextValue {
  resourceName: string;
  t: (key: string, defaultValue?: string) => string;
  model: ModelType;
  modelsMap: ModelsFieldsTypeMap;
  modelsEnum: Record<string, Record<string, string>>;
  tableConfig: ListPageConfig['tableConfig'];
  apiUrl: string;
  relationSelectProps: Record<
    string,
    SelectProps<{ value: string; label: string }, DefaultOptionType>
  >;
}

export interface UseListPageStateResult {
  // Table related
  tableProps: TableProps<any>;
  columns: FieldDataType[];
  model: ModelType;
  sorter: CrudSorting | undefined;
  filters: CrudFilters | undefined;

  // Search related
  searchFormProps: FormProps;

  // Selection related
  selectedRowKeys: React.Key[];
  setSelectedRowKeys: React.Dispatch<React.SetStateAction<React.Key[]>>;
  rowSelection: {
    selectedRowKeys: React.Key[];
    onChange: (keys: React.Key[]) => void;
  };
  hasSelected: boolean;

  // Batch operations
  updateSelectedItems: (values: any) => Promise<void>;
  deleteSelectedItems: () => Promise<void>;
  updateManyIsLoading: boolean;
  deleteManyIsLoading: boolean;

  // Export/Import
  triggerExportAll: () => void;
  triggerExportSelected: () => void;
  exportLoading: boolean;
  exportSelectedLoading: boolean;
  importProps: any;

  // Row operations
  updateRowData: (id: number, values: any) => void;
  isUpdateRowLoading: boolean;

  // Context
  context: ListPageContextValue;

  // Refetch
  refetch: () => void;
}
