<template>
  <div class="view">
    <EntityEditForm
      entityLabel="Child"
      :fields="fields"
      :initialData="initialData"
      :isEdit="isEdit"
      :requireDirty="isEdit"
      :loading="loading"
      :error="error"
      @submit="handleSubmit"
      @cancel="handleCancel"
      @add-image="handleAddImage"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import EntityEditForm from '../shared/EntityEditForm.vue'
import { maybeShow as tutorialMaybeShow } from '@/tutorial/controller'
import '@/assets/styles.css'

const router = useRouter()
const props = defineProps<{ id?: string }>()

const isEdit = computed(() => !!props.id)

type Field = {
  name: string
  label: string
  type: 'text' | 'number' | 'image' | 'custom'
  required?: boolean
  maxlength?: number
  min?: number
  max?: number
  imageType?: number
}

type ChildForm = {
  name: string
  age: number | null
  image_id: string | null
}

const fields: Field[] = [
  { name: 'name', label: 'Name', type: 'text', required: true, maxlength: 64 },
  { name: 'age', label: 'Age', type: 'number', required: true, min: 0, max: 120, maxlength: 3 },
  { name: 'image_id', label: 'Image', type: 'image', imageType: 1 },
]

const initialData = ref<ChildForm>({ name: '', age: null, image_id: null })
const localImageFile = ref<File | null>(null)
const loading = ref(false)
const error = ref<string | null>(null)

onMounted(async () => {
  if (!isEdit.value) tutorialMaybeShow('edit-child-name')
  if (isEdit.value && props.id) {
    loading.value = true
    try {
      const resp = await fetch(`/api/child/${props.id}`)
      if (!resp.ok) throw new Error('Failed to load child')
      const data = await resp.json()
      initialData.value = {
        name: data.name ?? '',
        age: data.age === null || data.age === undefined ? null : Number(data.age),
        image_id: data.image_id ?? null,
      }
    } catch {
      error.value = 'Could not load child.'
    } finally {
      loading.value = false
      await nextTick()
    }
  } else {
    try {
      const resp = await fetch('/api/image/list?type=1')
      if (resp.ok) {
        const data = await resp.json()
        const ids = data.ids || []
        if (ids.length > 0) {
          initialData.value = {
            ...initialData.value,
            image_id: ids[0],
          }
        }
      }
    } catch {
      // Ignore default image lookup failures and keep existing behavior.
    }
  }
})

function handleAddImage({ id, file }: { id: string; file: File }) {
  if (id === 'local-upload') {
    localImageFile.value = file
  }
}

async function handleSubmit(form: ChildForm) {
  let imageId = form.image_id
  error.value = null
  if (!form.name.trim()) {
    error.value = 'Child name is required.'
    return
  }
  if (form.age === null || form.age < 0) {
    error.value = 'Age must be a non-negative number.'
    return
  }
  loading.value = true

  // If the selected image is a local upload, upload it first
  if (imageId === 'local-upload' && localImageFile.value) {
    const formData = new FormData()
    formData.append('file', localImageFile.value)
    formData.append('type', '1')
    formData.append('permanent', 'false')
    try {
      const resp = await fetch('/api/image/upload', {
        method: 'POST',
        body: formData,
      })
      if (!resp.ok) throw new Error('Image upload failed')
      const data = await resp.json()
      imageId = data.id
    } catch {
      error.value = 'Failed to upload image.'
      loading.value = false
      return
    }
  }

  // Now update or create the child
  try {
    let resp
    if (isEdit.value && props.id) {
      resp = await fetch(`/api/child/${props.id}/edit`, {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          name: form.name,
          age: form.age,
          image_id: imageId,
        }),
      })
    } else {
      resp = await fetch('/api/child/add', {
        method: 'PUT',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          name: form.name,
          age: form.age,
          image_id: imageId,
        }),
      })
    }
    if (!resp.ok) throw new Error('Failed to save child')
    await router.push({ name: 'ParentChildrenListView' })
  } catch {
    error.value = 'Failed to save child.'
  }
  loading.value = false
}

function handleCancel() {
  router.back()
}
</script>
<style scoped>
.view {
  max-width: 400px;
  margin: 0 auto;
  background: var(--form-bg);
  border-radius: 12px;
  box-shadow: 0 4px 24px var(--form-shadow);
  padding: 2rem 2.2rem 1.5rem 2.2rem;
}
</style>
