<script lang="ts" setup>
import { Head, useForm } from '@inertiajs/vue3';
import {
    mdiFolderEdit,
    mdiArrowLeftBoldOutline,
    mdiFormTextarea,
    mdiContentSave,
    mdiTagOutline,
    mdiText,
} 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 props = defineProps({
    project: {
        type: Object,
        required: true,
    },
});

const form = useForm({
    label: props.project.label,
    name: props.project.name,
    description: props.project.description,
});

const submit = async () => {
    await form.put(stardust.route('settings.project.update', [props.project.id]));
};
</script>

<template>
    <LayoutAuthenticated>
        <Head :title="`Edit ${project.label}`" />
        <SectionMain>
            <SectionTitleLineWithButton :icon="mdiFolderEdit" :title="`Edit: ${project.label}`" 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="relative overflow-hidden shadow-lg">
                <!-- Subtle accent bar -->
                <div class="absolute inset-x-0 top-0 h-1 bg-gradient-to-r from-blue-500 via-indigo-500 to-blue-500"></div>

                <div class="grid grid-cols-1 gap-7 pt-2">
                    <!-- Label (read-only) -->
                    <FormField label="Label" help="Lowercase letters, numbers, and hyphens only">
                        <FormControl
                            v-model="form.label"
                            :icon="mdiTagOutline"
                            type="text"
                            :is-read-only="true"
                            class="font-mono bg-gray-100 dark:bg-slate-800 cursor-not-allowed opacity-75"
                        />
                    </FormField>

                    <!-- Name -->
                    <FormField label="Name" help="Required. Project title shown to users" :errors="form.errors.name">
                        <FormControl
                            v-model="form.name"
                            :icon="mdiText"
                            type="text"
                            placeholder="Enter Name"
                            required
                            class="transition-all duration-200 focus-within:ring-2 focus-within:ring-blue-500/40"
                        />
                    </FormField>

                    <!-- Description -->
                    <FormField
                        label="Description"
                        help="Optional. Detailed description of the project"
                        :errors="form.errors.description"
                    >
                        <FormControl
                            v-model="form.description"
                            :icon="mdiFormTextarea"
                            name="description"
                            type="textarea"
                            placeholder="Enter project description..."
                            class="transition-all duration-200 focus-within:ring-2 focus-within:ring-blue-500/40"
                        />
                    </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="Save Changes"
                            :class="{ 'opacity-25': form.processing }"
                            :disabled="form.processing"
                            class="transition-all hover:shadow-lg hover:-translate-y-0.5"
                        />
                    </BaseButtons>
                </template>
            </CardBox>

            <!-- Project Info Card -->
            <CardBox class="mt-6 bg-gradient-to-br from-gray-50 to-gray-100 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-12 h-12 rounded-lg bg-blue-500 dark:bg-blue-600 flex items-center justify-center shadow-md shadow-blue-500/30"
                        >
                            <span class="text-white text-xl font-bold">
                                {{ project.label.charAt(0).toUpperCase() }}
                            </span>
                        </div>
                    </div>
                    <div class="flex-1 min-w-0">
                        <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-1">
                            {{ project.label }}
                        </h3>
                        <p class="text-sm font-mono text-gray-600 dark:text-gray-400 mb-2">
                            {{ project.name }}
                        </p>
                        <div class="flex items-center gap-4 text-xs text-gray-500 dark:text-gray-500">
                            <span>
                                <span class="font-medium">Created:</span>
                                {{ new Date(project.created_at).toLocaleDateString() }}
                            </span>
                            <span>
                                <span class="font-medium">Updated:</span>
                                {{ new Date(project.updated_at).toLocaleDateString() }}
                            </span>
                        </div>
                    </div>
                </div>
            </CardBox>
        </SectionMain>
    </LayoutAuthenticated>
</template>