// composables/useTemplateSchema.js
import { templateSchemas } from '@/config/templateSchemas'

export const useTemplateSchema = () => {
  const generateContentStructure = (templateSlug) => {
    const schema = templateSchemas[templateSlug]
    if (!schema) return {}
    
    const content = {}
    
    Object.keys(schema.sections).forEach(sectionKey => {
      content[sectionKey] = {}
      
      Object.keys(schema.sections[sectionKey].fields).forEach(fieldKey => {
        const field = schema.sections[sectionKey].fields[fieldKey]
        
        // Set default values based on field type
        switch (field.type) {
          case 'image':
            content[sectionKey][fieldKey] = null
            content[sectionKey][`${fieldKey}Preview`] = null
            content[sectionKey][`${fieldKey}FileName`] = null
            content[sectionKey][`${fieldKey}Id`] = null // Track media library ID
            break
          case 'repeater':
            // Initialize repeater as empty array
            content[sectionKey][fieldKey] = []
            break
          case 'number':
            content[sectionKey][fieldKey] = field.default || 0
            break
          default:
            content[sectionKey][fieldKey] = field.default || ''
        }
      })
    })
    
    return content
  }
  
  const getTemplateSchema = (templateSlug) => {
    return templateSchemas[templateSlug] || null
  }
  
  return {
    generateContentStructure,
    getTemplateSchema
  }
}
