// services/scenarioService.js
import { getCSRFToken } from '@/services/token.js';

export const scenarioService = (contextPath = '/') => {
  const headers = {
    'Content-Type': 'application/json',
    'X-XSRF-TOKEN': getCSRFToken()
  };

  return {
    async loadScenarios() {
      const response = await fetch(`${contextPath}mgmt/api_scenario/list.do`, { headers });
      if (!response.ok) throw new Error('Failed to fetch scenarios');
      return await response.json();
    },

    async createScenario(name) {
      const response = await fetch(`${contextPath}mgmt/api_scenario/create.do`, {
        method: 'POST',
        headers,
        body: JSON.stringify({ name })
      });

      if (!response.ok) throw new Error('Failed to create scenario');
      return await response.json();
    },

    async updateScenario(scenario) {
      const response = await fetch(`${contextPath}mgmt/api_scenario/update.do`, {
        method: 'POST',
        headers,
        body: JSON.stringify(scenario)
      });

      if (!response.ok) throw new Error('Failed to update scenario');
      return scenario;
    },

    async deleteScenario(scenario) {
      const response = await fetch(`${contextPath}mgmt/api_scenario/delete.do`, {
        method: 'POST',
        headers,
        body: JSON.stringify(scenario)
      });

      if (!response.ok) throw new Error('Failed to delete scenario');
      return true;
    }
  };
};
