<template>
  <RsModal
    v-model="showModal"
    title="Script Task Configuration"
    size="xl"
    position="center"
    :okCallback="saveAndClose"
    okTitle="Save"
    :cancelCallback="closeModal"
  >
    <template #body>
      <div class="mb-6">
        <div class="flex items-start">
          <div class="mr-4 text-purple-500 flex-shrink-0 mt-1">
            <Icon name="material-symbols:code" class="text-2xl" />
          </div>
          <div>
            <h3 class="text-lg font-semibold mb-1">Configure Script Task</h3>
            <p class="text-sm text-gray-600">
              Execute JavaScript code to transform data, process variables, and apply business logic. 
              Script tasks are useful for data manipulation, calculations, and preparing data for subsequent steps.
            </p>
          </div>
        </div>
      </div>

      <!-- Main configuration area -->
      <ScriptNodeConfiguration 
        :nodeData="localNodeData" 
        :availableVariables="availableVariables"
        @update="handleUpdate"
      />

      <!-- Quick Reference Guide -->
      <div class="mt-6 bg-purple-50 p-4 rounded-md border border-purple-100">
        <h4 class="font-medium text-purple-700 mb-2 flex items-center">
          <Icon name="material-symbols:info-outline" class="mr-1" />
          Quick Reference Guide
        </h4>
        <div class="text-sm text-purple-700">
          <ul class="list-disc list-inside space-y-1">
            <li>Use <code class="bg-purple-100 px-1">processVariables</code> to access and modify process data</li>
            <li>Available functions: <code class="bg-purple-100 px-1">console.log()</code> for debugging</li>
            <li>Transform API responses into usable variables for subsequent steps</li>
            <li>Apply business rules and calculations to your data</li>
            <li>Test your script before saving to ensure it works correctly</li>
          </ul>
        </div>
      </div>
    </template>
  </RsModal>
</template>

<script setup>
import { ref, watch } from 'vue';
import ScriptNodeConfiguration from './ScriptNodeConfiguration.vue';
import { Icon } from '#components';

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  nodeData: {
    type: Object,
    required: true
  },
  availableVariables: {
    type: Array,
    default: () => []
  }
});

const emit = defineEmits(['update:modelValue', 'update']);

const showModal = ref(props.modelValue);
const localNodeData = ref({ ...props.nodeData });

// Watch for changes to modelValue prop to sync modal visibility
watch(() => props.modelValue, (value) => {
  showModal.value = value;
});

// Watch for changes to showModal to emit update:modelValue
watch(() => showModal.value, (value) => {
  emit('update:modelValue', value);
});

// Watch for changes to nodeData prop
watch(() => props.nodeData, (value) => {
  localNodeData.value = { ...value };
}, { deep: true });

function handleUpdate(updatedData) {
  localNodeData.value = { ...updatedData };
}

function saveAndClose() {
  emit('update', localNodeData.value);
  showModal.value = false;
}

function closeModal() {
  showModal.value = false;
}
</script>

<style scoped>
code {
  font-family: monospace;
  border-radius: 0.25rem;
}
</style> 