/**
 * filekorolheader: 搜索表单分页状态管理 - 管理组件的状态和actions
 * 功能：状态管理、数据更新、分页控制、搜索过滤
 * 路径：/components/common/searchFormPagination/components/searchFormPaginationReducer
 * 规范：遵循crop-x/docs/开发项目规范.md，使用useReducer模式管理复杂状态
 */
'use client';

// 状态接口定义
export interface SearchFormPaginationState {
  // 数据相关
  data: any[];
  loading: boolean;
  error: string | null;

  // 搜索过滤
  filters: Record<string, string>;

  // 分页相关
  pagination: {
    page: number;
    size: number;
    total: number;
    totalPages: number;
    hasNext: boolean;
    hasPrev: boolean;
  };

  // 排序相关
  sortBy?: string;
  sortOrder: 'asc' | 'desc';
}

// Action类型定义
export type SearchFormPaginationAction =
  | { type: 'SET_DATA'; payload: any[] }
  | { type: 'SET_LOADING'; payload: boolean }
  | { type: 'SET_ERROR'; payload: string | null }
  | { type: 'SET_FILTERS'; payload: Record<string, string> }
  | { type: 'UPDATE_FILTER'; payload: { key: string; value: string } }
  | { type: 'CLEAR_FILTERS' }
  | { type: 'SET_PAGINATION'; payload: SearchFormPaginationState['pagination'] }
  | { type: 'SET_PAGINATION_PAGE'; payload: number }
  | { type: 'SET_PAGINATION_SIZE'; payload: number }
  | { type: 'SET_SORT_BY'; payload: string }
  | { type: 'SET_SORT_ORDER'; payload: 'asc' | 'desc' }
  | { type: 'SET_SORT'; payload: { sortBy?: string; sortOrder: 'asc' | 'desc' } }
  | { type: 'TOGGLE_SORT'; payload: string }
  | { type: 'SET_DATA_AND_PAGINATION'; payload: { data: any[]; pagination: SearchFormPaginationState['pagination'] } }
  | { type: 'RESET_STATE' };

// 初始状态
export const initialState: SearchFormPaginationState = {
  data: [],
  loading: false,
  error: null,
  filters: {},
  pagination: {
    page: 1,
    size: 10,
    total: 0,
    totalPages: 0,
    hasNext: false,
    hasPrev: false,
  },
  sortBy: undefined,
  sortOrder: 'asc',
};

// Reducer函数
export function SearchFormPaginationReducer(
  state: SearchFormPaginationState,
  action: SearchFormPaginationAction
): SearchFormPaginationState {
  switch (action.type) {
    case 'SET_DATA':
      return {
        ...state,
        data: action.payload,
      };

    case 'SET_LOADING':
      return {
        ...state,
        loading: action.payload,
      };

    case 'SET_ERROR':
      return {
        ...state,
        error: action.payload,
        loading: false,
      };

    case 'SET_FILTERS':
      return {
        ...state,
        filters: action.payload,
      };

    case 'UPDATE_FILTER':
      return {
        ...state,
        filters: {
          ...state.filters,
          [action.payload.key]: action.payload.value,
        },
      };

    case 'CLEAR_FILTERS':
      return {
        ...state,
        filters: {},
      };

    case 'SET_PAGINATION':
      return {
        ...state,
        pagination: action.payload,
      };

    case 'SET_PAGINATION_PAGE':
      return {
        ...state,
        pagination: {
          ...state.pagination,
          page: action.payload,
        },
      };

    case 'SET_PAGINATION_SIZE':
      return {
        ...state,
        pagination: {
          ...state.pagination,
          size: action.payload,
        },
      };

    case 'SET_SORT_BY':
      return {
        ...state,
        sortBy: action.payload,
      };

    case 'SET_SORT_ORDER':
      return {
        ...state,
        sortOrder: action.payload,
      };

    case 'SET_SORT':
      return {
        ...state,
        sortBy: action.payload.sortBy,
        sortOrder: action.payload.sortOrder,
      };

    case 'TOGGLE_SORT':
      const columnKey = action.payload;
      let newSortOrder: 'asc' | 'desc';

      if (state.sortBy === columnKey) {
        // 如果点击的是当前排序列，切换排序方向
        newSortOrder = state.sortOrder === 'desc' ? 'asc' : 'desc';
      } else {
        // 如果点击的是新列，设置为升序
        newSortOrder = 'asc';
      }

      return {
        ...state,
        sortBy: columnKey,
        sortOrder: newSortOrder,
      };

    case 'SET_DATA_AND_PAGINATION':
      return {
        ...state,
        data: action.payload.data,
        pagination: action.payload.pagination,
        loading: false,
        error: null,
      };

    case 'RESET_STATE':
      return {
        ...initialState,
        filters: state.filters, // 保留搜索过滤条件
      };

    default:
      console.warn('Unknown action type:', (action as any).type);
      return state;
  }
}