/**
 * filekorolheader: 企业管理状态管理 - 企业数据状态管理核心
 * 功能：API数据管理、分页状态、加载状态、错误处理
 * 路径：/central-config/tenant/enterprise-management/components/enterpriseReducer
 * 规范：遵循crop-x/docs/开发项目规范.md，使用useReducer状态管理模式
 */

import { Enterprise } from './enterpriseApi';

export interface FormData {
  name: string;
  code: string;
  type: string;
}

export interface EnterpriseState {
  enterprises: Enterprise[];
  loading: boolean;
  error: string | null;
  pagination: {
    page: number;
    size: number;
    total: number;
    totalPages: number;
    hasNext: boolean;
    hasPrev: boolean;
  };
  filters: {
    search: string;
    audit_status: string;
  };
  showAddDialog: boolean;
  showViewDialog: boolean;
  showStatusDialog: boolean;
  selectedEnterprise: Enterprise | null;
  statusAction: 'enable' | 'disable';
  formData: FormData;
  sortBy?: string;
  sortOrder: 'asc' | 'desc';
}

export type EnterpriseAction =
  | { type: 'SET_LOADING'; payload: boolean }
  | { type: 'SET_ERROR'; payload: string | null }
  | { type: 'SET_ENTERPRISES'; payload: { data: Enterprise[]; pagination: EnterpriseState['pagination'] } }
  | { type: 'SET_FILTERS'; payload: Partial<EnterpriseState['filters']> }
  | { type: 'SET_PAGINATION'; payload: Partial<EnterpriseState['pagination']> }
  | { type: 'SET_SORT'; payload: { sortBy?: string; sortOrder: 'asc' | 'desc' } }
  | { type: 'TOGGLE_ADD_DIALOG'; payload: boolean }
  | { type: 'TOGGLE_VIEW_DIALOG'; payload: boolean }
  | { type: 'TOGGLE_STATUS_DIALOG'; payload: boolean }
  | { type: 'SET_SELECTED_ENTERPRISE'; payload: Enterprise | null }
  | { type: 'SET_STATUS_ACTION'; payload: 'enable' | 'disable' }
  | { type: 'UPDATE_FORM_DATA'; payload: Partial<FormData> }
  | { type: 'RESET_FORM_DATA' }
  | { type: 'REFRESH_DATA' };

// 初始状态
export const initialState: EnterpriseState = {
  enterprises: [],
  loading: false,
  error: null,
  pagination: {
    page: 1,
    size: 10,
    total: 0,
    totalPages: 0,
    hasNext: false,
    hasPrev: false,
  },
  filters: {
    search: '',
    audit_status: '',
  },
  showAddDialog: false,
  showViewDialog: false,
  showStatusDialog: false,
  selectedEnterprise: null,
  statusAction: 'enable',
  formData: {
    name: '',
    code: '',
    type: ''
  },
  sortBy: undefined,
  sortOrder: 'desc',
};

// Reducer
export function enterpriseReducer(state: EnterpriseState, action: EnterpriseAction): EnterpriseState {
  switch (action.type) {
    case 'SET_LOADING':
      return { ...state, loading: action.payload };

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

    case 'SET_ENTERPRISES':
      return {
        ...state,
        enterprises: 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_ADD_DIALOG':
      return {
        ...state,
        showAddDialog: action.payload,
        ...(action.payload === false ? { formData: initialState.formData } : {})
      };

    case 'TOGGLE_VIEW_DIALOG':
      return { ...state, showViewDialog: action.payload };

    case 'TOGGLE_STATUS_DIALOG':
      return { ...state, showStatusDialog: action.payload };

    case 'SET_SELECTED_ENTERPRISE':
      return { ...state, selectedEnterprise: action.payload };

    case 'SET_STATUS_ACTION':
      return { ...state, statusAction: action.payload };

    case 'UPDATE_FORM_DATA':
      return {
        ...state,
        formData: { ...state.formData, ...action.payload }
      };

    case 'RESET_FORM_DATA':
      return { ...state, formData: initialState.formData };

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

    default:
      return state;
  }
}