<script lang="ts" setup>
import { Head, useForm } from '@inertiajs/vue3';
import { mdiFolderPlus, mdiArrowLeftBoldOutline, mdiFormTextarea, mdiContentSave } from '@mdi/js';
import LayoutAuthenticated from '@/Layouts/LayoutAuthenticated.vue';
import SectionMain from '@/Components/SectionMain.vue';
import SectionTitleLineWithButton from '@/Components/SectionTitleLineWithButton.vue';
import CardBox from '@/Components/CardBox.vue';
import FormField from '@/Components/FormField.vue';
import FormControl from '@/Components/FormControl.vue';
import BaseButton from '@/Components/BaseButton.vue';
import BaseButtons from '@/Components/BaseButtons.vue';
import { stardust } from '@eidellev/adonis-stardust/client';

const form = useForm({
    label: '',
    name: '',
    description: '',
});

const submit = async () => {
    await form.post(stardust.route('settings.project.store'));
};
</script>

<template>
    <LayoutAuthenticated>
        <Head title="Create New Project" />
        <SectionMain>
            <SectionTitleLineWithButton :icon="mdiFolderPlus" title="Create New Project" main>
                <BaseButton
                    :route-name="stardust.route('settings.project.index')"
                    :icon="mdiArrowLeftBoldOutline"
                    label="Back"
                    color="white"
                    rounded-full
                    small
                />
            </SectionTitleLineWithButton>
            
            <CardBox form @submit.prevent="submit()" class="shadow-lg">
                <div class="grid grid-cols-1 gap-6">
                    <FormField 
                        label="Label" 
                        help="Required. Displayed project label" 
                        :class="{ 'text-red-400': form.errors.label }"
                    >
                        <FormControl 
                            v-model="form.label" 
                            type="text" 
                            placeholder="Enter a descriptive label..." 
                            required 
                            :error="form.errors.label"
                            class="transition-all focus:ring-2 focus:ring-blue-500"
                        >
                            <div class="text-red-400 text-sm mt-1" v-if="form.errors.label">
                                {{ form.errors.label }}
                            </div>
                        </FormControl>
                    </FormField>

                    <FormField 
                        label="Name" 
                        help="Required. Project identifier (slug, lowercase, no spaces)" 
                        :class="{ 'text-red-400': form.errors.name }"
                    >
                        <FormControl 
                            v-model="form.name" 
                            type="text" 
                            placeholder="e.g., my-awesome-project" 
                            required 
                            :error="form.errors.name"
                            class="font-mono transition-all focus:ring-2 focus:ring-blue-500"
                        >
                            <div class="text-red-400 text-sm mt-1" v-if="form.errors.name">
                                {{ form.errors.name }}
                            </div>
                        </FormControl>
                    </FormField>

                    <FormField
                        label="Description"
                        help="Optional. Detailed description of the project"
                        :class="{ 'text-red-400': form.errors.description }"
                    >
                        <FormControl
                            v-model="form.description"
                            :icon="mdiFormTextarea"
                            name="description"
                            type="textarea"
                            placeholder="Describe what this project is about..."
                            :error="form.errors.description"
                            class="transition-all focus:ring-2 focus:ring-blue-500"
                        >
                            <div class="text-red-400 text-sm mt-1" v-if="form.errors.description">
                                {{ form.errors.description }}
                            </div>
                        </FormControl>
                    </FormField>
                </div>

                <template #footer>
                    <BaseButtons class="justify-between">
                        <BaseButton
                            :route-name="stardust.route('settings.project.index')"
                            label="Cancel"
                            color="white"
                            outline
                        />
                        <BaseButton
                            type="submit"
                            color="info"
                            :icon="mdiContentSave"
                            label="Create Project"
                            :class="{ 'opacity-25': form.processing }"
                            :disabled="form.processing"
                            class="transition-all hover:shadow-lg"
                        />
                    </BaseButtons>
                </template>
            </CardBox>

            <!-- Helper Card -->
            <CardBox class="mt-6 bg-gradient-to-br from-blue-50 to-indigo-50 dark:from-slate-800 dark:to-slate-900 border-l-4 border-blue-500">
                <div class="flex items-start gap-4">
                    <div class="flex-shrink-0">
                        <div class="w-10 h-10 rounded-full bg-blue-500 dark:bg-blue-600 flex items-center justify-center">
                            <span class="text-white text-lg">💡</span>
                        </div>
                    </div>
                    <div class="flex-1">
                        <h3 class="text-sm font-semibold text-gray-900 dark:text-white mb-2">
                            Quick Tips
                        </h3>
                        <ul class="text-sm text-gray-700 dark:text-gray-300 space-y-1">
                            <li>• <strong>Label</strong> is what users will see in the interface</li>
                            <li>• <strong>Name</strong> is a technical identifier (use lowercase and hyphens)</li>
                            <li>• <strong>Description</strong> helps team members understand the project's purpose</li>
                        </ul>
                    </div>
                </div>
            </CardBox>
        </SectionMain>
    </LayoutAuthenticated>
</template>