/**
 * filekorolheader: 认证状态管理 - 登录注册页面状态管理核心
 * 功能：状态管理、表单验证、倒计时管理、错误处理
 * 路径：/login/components/authReducer.tsx
 * 规范：遵循crop-x/docs/开发项目规范.md，使用useReducer状态管理，shadcn语义化样式
 */

'use client';

import { Enterprise } from '@/types/auth';

// 状态接口定义
export interface AuthState {
  loading: boolean;
  showPassword: boolean;
  showConfirmPassword: boolean;
  sendingCode: boolean;
  countdown: number;
  error: string;
  success: string;
  enterprises: Enterprise[];
  passwordForm: {
    username: string;
    password: string;
    captcha: string;
  };
  phoneForm: {
    phone: string;
    code: string;
    captcha: string;
  };
  registerForm: {
    username: string;
    password: string;
    confirmPassword: string;
    phone: string;
    code: string;
    realName: string;
    email: string;
    enterpriseId: string;
    captcha: string;
  };
  validation: {
    username: string;
    password: string;
    confirmPassword: string;
    phone: string;
    realName: string;
  };
}

// Action类型定义
export type AuthAction =
  | { type: 'SET_LOADING'; payload: boolean }
  | { type: 'TOGGLE_PASSWORD_VISIBILITY' }
  | { type: 'TOGGLE_CONFIRM_PASSWORD_VISIBILITY' }
  | { type: 'SET_SENDING_CODE'; payload: boolean }
  | { type: 'SET_COUNTDOWN'; payload: number }
  | { type: 'SET_ERROR'; payload: string }
  | { type: 'SET_SUCCESS'; payload: string }
  | { type: 'SET_ENTERPRISES'; payload: Enterprise[] }
  | { type: 'UPDATE_PASSWORD_FORM'; payload: Partial<AuthState['passwordForm']> }
  | { type: 'UPDATE_PHONE_FORM'; payload: Partial<AuthState['phoneForm']> }
  | { type: 'UPDATE_REGISTER_FORM'; payload: Partial<AuthState['registerForm']> }
  | { type: 'UPDATE_VALIDATION'; payload: Partial<AuthState['validation']> }
  | { type: 'CLEAR_VALIDATION' }
  | { type: 'RESET_FORMS' };

// Reducer函数
export function authReducer(state: AuthState, action: AuthAction): AuthState {
  switch (action.type) {
    case 'SET_LOADING':
      return { ...state, loading: action.payload };
    case 'TOGGLE_PASSWORD_VISIBILITY':
      return { ...state, showPassword: !state.showPassword };
    case 'TOGGLE_CONFIRM_PASSWORD_VISIBILITY':
      return { ...state, showConfirmPassword: !state.showConfirmPassword };
    case 'SET_SENDING_CODE':
      return { ...state, sendingCode: action.payload };
    case 'SET_COUNTDOWN':
      return { ...state, countdown: action.payload };
    case 'SET_ERROR':
      return { ...state, error: action.payload, success: '' };
    case 'SET_SUCCESS':
      return { ...state, success: action.payload, error: '' };
    case 'SET_ENTERPRISES':
      return { ...state, enterprises: action.payload };
    case 'UPDATE_PASSWORD_FORM':
      return { ...state, passwordForm: { ...state.passwordForm, ...action.payload } };
    case 'UPDATE_PHONE_FORM':
      return { ...state, phoneForm: { ...state.phoneForm, ...action.payload } };
    case 'UPDATE_REGISTER_FORM':
      return { ...state, registerForm: { ...state.registerForm, ...action.payload } };
    case 'UPDATE_VALIDATION':
      return { ...state, validation: { ...state.validation, ...action.payload } };
    case 'CLEAR_VALIDATION':
      return { ...state, validation: { username: '', password: '', confirmPassword: '', phone: '', realName: '' } };
    case 'RESET_FORMS':
      return {
        ...state,
        passwordForm: { username: '', password: '', captcha: '' },
        phoneForm: { phone: '', code: '', captcha: '' },
        registerForm: {
          username: '',
          password: '',
          confirmPassword: '',
          phone: '',
          code: '',
          realName: '',
          email: '',
          enterpriseId: '',
          captcha: '',
        },
        validation: { username: '', password: '', confirmPassword: '', phone: '', realName: '' },
        error: '',
        success: '',
      };
    default:
      return state;
  }
}

// 初始状态
export const initialAuthState: AuthState = {
  loading: false,
  showPassword: false,
  showConfirmPassword: false,
  sendingCode: false,
  countdown: 0,
  error: '',
  success: '',
  enterprises: [],
  passwordForm: {
    username: '',
    password: '',
    captcha: '',
  },
  phoneForm: {
    phone: '',
    code: '',
    captcha: '',
  },
  registerForm: {
    username: '',
    password: '',
    confirmPassword: '',
    phone: '',
    code: '',
    realName: '',
    email: '',
    enterpriseId: '',
    captcha: '',
  },
  validation: {
    username: '',
    password: '',
    confirmPassword: '',
    phone: '',
    realName: '',
  },
};