/**
 * filekorolheader: 审核历史状态管理 - 审核记录数据状态管理核心
 * 功能：API数据管理、分页状态、加载状态、错误处理、筛选状态
 * 路径：/central-config/tenant/audit-history/components/auditHistoryReducer
 * 规范：遵循crop-x/docs/开发项目规范.md，使用useReducer状态管理模式
 */

import { AuditRecord } from '../types';

export interface FilterOptions {
  searchKeyword: string;
  resultFilter: string;
  typeFilter: string;
  dateRange: string;
}

export interface AuditHistoryState {
  records: AuditRecord[];
  loading: boolean;
  error: string | null;
  pagination: {
    page: number;
    size: number;
    total: number;
    totalPages: number;
    hasNext: boolean;
    hasPrev: boolean;
  };
  filters: FilterOptions;
  showDetailDialog: boolean;
  selectedRecord: AuditRecord | null;
  sortBy?: string;
  sortOrder: 'asc' | 'desc';
}

export type AuditHistoryAction =
  | { type: 'SET_LOADING'; payload: boolean }
  | { type: 'SET_ERROR'; payload: string | null }
  | { type: 'SET_RECORDS'; payload: { data: AuditRecord[]; pagination: AuditHistoryState['pagination'] } }
  | { type: 'SET_FILTERS'; payload: Partial<FilterOptions> }
  | { type: 'SET_PAGINATION'; payload: Partial<AuditHistoryState['pagination']> }
  | { type: 'SET_SORT'; payload: { sortBy?: string; sortOrder: 'asc' | 'desc' } }
  | { type: 'TOGGLE_DETAIL_DIALOG'; payload: boolean }
  | { type: 'SET_SELECTED_RECORD'; payload: AuditRecord | null }
  | { type: 'REFRESH_DATA' };

// 初始状态
export const initialState: AuditHistoryState = {
  records: [],
  loading: false,
  error: null,
  pagination: {
    page: 1,
    size: 10,
    total: 0,
    totalPages: 0,
    hasNext: false,
    hasPrev: false,
  },
  filters: {
    searchKeyword: '',
    resultFilter: 'all',
    typeFilter: 'all',
    dateRange: 'all',
  },
  showDetailDialog: false,
  selectedRecord: null,
  sortBy: undefined,
  sortOrder: 'desc',
};

// Reducer
export function auditHistoryReducer(state: AuditHistoryState, action: AuditHistoryAction): AuditHistoryState {
  switch (action.type) {
    case 'SET_LOADING':
      return { ...state, loading: action.payload };

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

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

    case 'SET_FILTERS':
      return {
        ...state,
        filters: { ...state.filters, ...action.payload },
        pagination: { ...state.pagination, page: 1 }, // 重置到第一页
      };

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

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

    case 'TOGGLE_DETAIL_DIALOG':
      return { ...state, showDetailDialog: action.payload };

    case 'SET_SELECTED_RECORD':
      return { ...state, selectedRecord: action.payload };

    case 'REFRESH_DATA':
      return {
        ...state,
        error: null, // 清除错误状态
      };

    default:
      return state;
  }
}