import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';

export const fetchGameConfigs = createAsyncThunk(
  'config/fetchGameConfigs',
  async () => {
    // In a real app, this would fetch from an API
    // Simulating API response for demonstration
    return {
      fps: 120,
      graphicsQuality: 'low',
      textureQuality: 'medium',
      shadowQuality: 'low',
      antiAliasing: false,
      vsync: false,
      renderDistance: 75
    };
  }
);

const configSlice = createSlice({
  name: 'config',
  initialState: {
    configs: null,
    loading: false,
    error: null,
    generatedConfig: null
  },
  reducers: {
    updateConfig: (state, action) => {
      state.configs = { ...state.configs, ...action.payload };
    },
    generateGameConfig: (state) => {
      if (state.configs) {
        // Generate the configuration file content
        const configContent = `
[PERFORMANCE]
MaxFPS=${state.configs.fps}
GraphicsQuality=${state.configs.graphicsQuality}
TextureQuality=${state.configs.textureQuality}
ShadowQuality=${state.configs.shadowQuality}
AntiAliasing=${state.configs.antiAliasing ? 1 : 0}
VSync=${state.configs.vsync ? 1 : 0}
RenderDistance=${state.configs.renderDistance}
EnableLagReduction=1
MemoryOptimization=1
PriorityMode=high
ThreadedRendering=1
        `.trim();
        
        state.generatedConfig = configContent;
      }
    }
  },
  extraReducers: (builder) => {
    builder
      .addCase(fetchGameConfigs.pending, (state) => {
        state.loading = true;
      })
      .addCase(fetchGameConfigs.fulfilled, (state, action) => {
        state.loading = false;
        state.configs = action.payload;
      })
      .addCase(fetchGameConfigs.rejected, (state, action) => {
        state.loading = false;
        state.error = action.error.message;
      });
  }
});

export const { updateConfig, generateGameConfig }