'use client';

import { ReactNode } from 'react';

export interface LayerSamplingState {
  selectedField: string;
  selectedNutrient: 'organic' | 'nitrogen' | 'phosphorus' | 'potassium';
  viewMode: '3d' | 'slice' | 'contour';
  depthSlice: number[];
  rotationAngle: number;
  zoomLevel: number;
}

export type LayerSamplingAction =
  | { type: 'SET_SELECTED_FIELD'; payload: string }
  | { type: 'SET_SELECTED_NUTRIENT'; payload: 'organic' | 'nitrogen' | 'phosphorus' | 'potassium' }
  | { type: 'SET_VIEW_MODE'; payload: '3d' | 'slice' | 'contour' }
  | { type: 'SET_DEPTH_SLICE'; payload: number[] }
  | { type: 'SET_ROTATION_ANGLE'; payload: number }
  | { type: 'SET_ZOOM_LEVEL'; payload: number }
  | { type: 'ROTATE' }
  | { type: 'ZOOM_IN' }
  | { type: 'ZOOM_OUT' };

export const initialState: LayerSamplingState = {
  selectedField: 'field-1',
  selectedNutrient: 'organic',
  viewMode: '3d',
  depthSlice: [20],
  rotationAngle: 45,
  zoomLevel: 100,
};

export function layerSamplingReducer(state: LayerSamplingState, action: LayerSamplingAction): LayerSamplingState {
  switch (action.type) {
    case 'SET_SELECTED_FIELD':
      return { ...state, selectedField: action.payload };

    case 'SET_SELECTED_NUTRIENT':
      return { ...state, selectedNutrient: action.payload };

    case 'SET_VIEW_MODE':
      return { ...state, viewMode: action.payload };

    case 'SET_DEPTH_SLICE':
      return { ...state, depthSlice: action.payload };

    case 'SET_ROTATION_ANGLE':
      return { ...state, rotationAngle: action.payload };

    case 'SET_ZOOM_LEVEL':
      return { ...state, zoomLevel: action.payload };

    case 'ROTATE':
      return { ...state, rotationAngle: (state.rotationAngle + 45) % 360 };

    case 'ZOOM_IN':
      return { ...state, zoomLevel: Math.min(200, state.zoomLevel + 10) };

    case 'ZOOM_OUT':
      return { ...state, zoomLevel: Math.max(50, state.zoomLevel - 10) };

    default:
      return state;
  }
}