import {
  VendorComplianceState,
  VendorComplianceAction,
} from './types'

// ==========================================
// INITIAL STATE
// ==========================================

export const initialState: VendorComplianceState = {
  processingActivities: [],
  vendors: [],
  contracts: [],
  findings: [],
  controls: [],
  controlInstances: [],
  riskAssessments: [],
  isLoading: false,
  error: null,
  selectedVendorId: null,
  selectedActivityId: null,
  activeTab: 'overview',
  lastModified: null,
}

// ==========================================
// REDUCER
// ==========================================

export function vendorComplianceReducer(
  state: VendorComplianceState,
  action: VendorComplianceAction
): VendorComplianceState {
  const updateState = (updates: Partial<VendorComplianceState>): VendorComplianceState => ({
    ...state,
    ...updates,
    lastModified: new Date(),
  })

  switch (action.type) {
    // Processing Activities
    case 'SET_PROCESSING_ACTIVITIES':
      return updateState({ processingActivities: action.payload })

    case 'ADD_PROCESSING_ACTIVITY':
      return updateState({
        processingActivities: [...state.processingActivities, action.payload],
      })

    case 'UPDATE_PROCESSING_ACTIVITY':
      return updateState({
        processingActivities: state.processingActivities.map((activity) =>
          activity.id === action.payload.id
            ? { ...activity, ...action.payload.data, updatedAt: new Date() }
            : activity
        ),
      })

    case 'DELETE_PROCESSING_ACTIVITY':
      return updateState({
        processingActivities: state.processingActivities.filter(
          (activity) => activity.id !== action.payload
        ),
      })

    // Vendors
    case 'SET_VENDORS':
      return updateState({ vendors: action.payload })

    case 'ADD_VENDOR':
      return updateState({
        vendors: [...state.vendors, action.payload],
      })

    case 'UPDATE_VENDOR':
      return updateState({
        vendors: state.vendors.map((vendor) =>
          vendor.id === action.payload.id
            ? { ...vendor, ...action.payload.data, updatedAt: new Date() }
            : vendor
        ),
      })

    case 'DELETE_VENDOR':
      return updateState({
        vendors: state.vendors.filter((vendor) => vendor.id !== action.payload),
      })

    // Contracts
    case 'SET_CONTRACTS':
      return updateState({ contracts: action.payload })

    case 'ADD_CONTRACT':
      return updateState({
        contracts: [...state.contracts, action.payload],
      })

    case 'UPDATE_CONTRACT':
      return updateState({
        contracts: state.contracts.map((contract) =>
          contract.id === action.payload.id
            ? { ...contract, ...action.payload.data, updatedAt: new Date() }
            : contract
        ),
      })

    case 'DELETE_CONTRACT':
      return updateState({
        contracts: state.contracts.filter((contract) => contract.id !== action.payload),
      })

    // Findings
    case 'SET_FINDINGS':
      return updateState({ findings: action.payload })

    case 'ADD_FINDINGS':
      return updateState({
        findings: [...state.findings, ...action.payload],
      })

    case 'UPDATE_FINDING':
      return updateState({
        findings: state.findings.map((finding) =>
          finding.id === action.payload.id
            ? { ...finding, ...action.payload.data, updatedAt: new Date() }
            : finding
        ),
      })

    // Controls
    case 'SET_CONTROLS':
      return updateState({ controls: action.payload })

    case 'SET_CONTROL_INSTANCES':
      return updateState({ controlInstances: action.payload })

    case 'UPDATE_CONTROL_INSTANCE':
      return updateState({
        controlInstances: state.controlInstances.map((instance) =>
          instance.id === action.payload.id
            ? { ...instance, ...action.payload.data }
            : instance
        ),
      })

    // Risk Assessments
    case 'SET_RISK_ASSESSMENTS':
      return updateState({ riskAssessments: action.payload })

    case 'UPDATE_RISK_ASSESSMENT':
      return updateState({
        riskAssessments: state.riskAssessments.map((assessment) =>
          assessment.id === action.payload.id
            ? { ...assessment, ...action.payload.data }
            : assessment
        ),
      })

    // UI State
    case 'SET_LOADING':
      return { ...state, isLoading: action.payload }

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

    case 'SET_SELECTED_VENDOR':
      return { ...state, selectedVendorId: action.payload }

    case 'SET_SELECTED_ACTIVITY':
      return { ...state, selectedActivityId: action.payload }

    case 'SET_ACTIVE_TAB':
      return { ...state, activeTab: action.payload }

    default:
      return state
  }
}
