<template>
    <div class="row">
        <div class="col">
            <loading-component style="height: 50px; top: 0;" key="1" color="success" v-show="isLoading"></loading-component>
            <div class="row" v-show="!isLoading">
                <div class="col">
                   <div class="row m-b-20">
                        <div class="col">
                            <h6 class="all-caps m-b-5 bold no-margin">
                                {{ data ? 'Update' : 'Create' }}
                                <span v-if="!data">
                                    {{ selectedModel === 'chatgpt' ? 'ChatGPT' : 'Claude' }}
                                </span>
                                Prompt
                            </h6>
                        </div>
                    </div>
                    <div class="row m-b-5 animate__animated animate__fadeInUpBig animate__fast" v-if="error">
                        <div class="col">
                            <small class="bold fs-10 text-danger">{{ error }}</small>
                        </div>
                    </div>
                    <div class="row m-b-10" v-if="!data">
                        <div class="col">
                            <label>AI Model</label>
                            <div>
                                <div class="form-check form-check-inline">
                                    <input
                                        class="form-check-input"
                                        type="radio"
                                        id="chatgptRadio"
                                        value="chatgpt"
                                        v-model="selectedModel"
                                    />
                                    <label class="form-check-label" for="chatgptRadio">
                                        ChatGPT
                                    </label>
                                </div>
                                <div class="form-check form-check-inline">
                                    <input
                                        class="form-check-input"
                                        type="radio"
                                        id="claudeRadio"
                                        value="claude"
                                        v-model="selectedModel"
                                    />
                                    <label class="form-check-label" for="claudeRadio">
                                        Claude
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row m-b-10">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.key">
                                <label>Title</label>
                                <input type="text" class="form-control" v-model="parameters.key" :disabled="!!data">
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col">
                            <validation-wrapper-component :validator="v$.parameters.value">
                                <label>Prompt</label>
                                <textarea class="form-control h-75" v-model="parameters.value" rows="20"></textarea>
                            </validation-wrapper-component>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col p-r-5">
                            <div class="btn btn-sm btn-default bg-master-lightest btn-block b-rad-none" :data-dismiss="closable ? 'modal' : ''" @click="$emit('close')">Cancel</div>
                        </div>
                        <div class="col p-l-5">
                            <div class="btn btn-sm btn-success btn-block b-rad-none" @click="submitForm">{{ data ? 'Update' : 'Create' }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { useFormHandler } from '@/composables/useFormHandler';
import route from '@/general/functions/router';
import { useVuelidate } from '@vuelidate/core';
import { required } from '@vuelidate/validators';

interface Props {
    section: string;
    data?: any;
    closable?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
    closable: true
});

defineEmits(['close']);

const defaults = {
    key: '',
    value: '',
};

const { parameters, submit, error, isLoading, closeModal, formHandler } = useFormHandler({
    section: props.section,
    data: props.data || defaults
});

const selectedModel = ref<'chatgpt' | 'claude'>('claude');

const rules = {
    parameters: {
        key: { required },
        value: { required }
    }
};

const v$ = useVuelidate(rules, { parameters });

const resetForm = () => {
    Object.assign(parameters.value, defaults);
    v$.value.$reset();
};

const submitForm = async () => {
    const isValid = await v$.value.$validate();
    if (!isValid) return;

    let routeUrl = '';
    let method: 'post' | 'put' = 'post';

    if (props.data) {
        if (props.data.key.startsWith('CHATGPT_PROMPT_')) {
            routeUrl = route('api.kvp.chatgpt.prompt.update', props.data.id);
            method = 'put';
        } else if (props.data.key.startsWith('CLAUDE_PROMPT_')) {
            routeUrl = route('api.kvp.claude.prompt.update', props.data.id);
            method = 'put';
        }
    } else if (selectedModel.value === 'chatgpt') {
        routeUrl = route('api.kvp.chatgpt.prompt.create');
    } else {
        routeUrl = route('api.kvp.claude.prompt.create');
    }

    await submit(routeUrl, method, props.section, true, false, parameters.value, {
        successHandler: () => {
            closeModal();
            formHandler();
            resetForm();
        },
    });
};
</script>
