<?php
/**
 * ==========================
 * 🔐 IGNY8 FILE RULE HEADER
 * ==========================
 * @file         : forms-config.php
 * @location     : /modules/config/forms-config.php
 * @type         : Config Array
 * @scope        : Global
 * @allowed      : Form definitions, validation rules, field configurations
 * @reusability  : Globally Reusable
 * @notes        : Central form configuration for all modules
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Get form configuration for a specific table
 * 
 * @param string $table_id Table ID (e.g., 'planner_keywords')
 * @return array|null Form configuration or null if not found
 */
function igny8_get_form_config($table_id) {
    $form_configs = igny8_get_all_form_configs();
    $GLOBALS['igny8_forms_config'] = $form_configs;
    return igny8_get_dynamic_form_config($table_id);
}

/**
 * Get all form configurations
 * 
 * @return array All form configurations
 */
function igny8_get_all_form_configs() {
    return [
        'planner_keywords' => [
            'fields' => [
                [
                    'name' => 'keyword',
                    'type' => 'text',
                    'label' => 'Keyword',
                    'required' => true
                ],
                [
                    'name' => 'search_volume',
                    'type' => 'number',
                    'label' => 'Search Volume',
                    'required' => false
                ],
                [
                    'name' => 'difficulty',
                    'type' => 'select',
                    'label' => 'Difficulty',
                    'options' => [
                        'Very Easy' => 'Very Easy',
                        'Easy' => 'Easy',
                        'Medium' => 'Medium',
                        'Hard' => 'Hard',
                        'Very Hard' => 'Very Hard'
                    ],
                    'required' => false
                ],
                [
                    'name' => 'cpc',
                    'type' => 'number',
                    'label' => 'CPC',
                    'required' => false,
                    'step' => 0.01
                ],
                [
                    'name' => 'intent',
                    'type' => 'select',
                    'label' => 'Intent',
                    'options' => [
                        'informational' => 'Informational',
                        'navigational' => 'Navigational',
                        'transactional' => 'Transactional',
                        'commercial' => 'Commercial'
                    ],
                    'required' => false
                ],
                [
                    'name' => 'status',
                    'type' => 'select',
                    'label' => 'Status',
                    'options' => [
                        'unmapped' => 'Unmapped',
                        'mapped' => 'Mapped',
                        'queued' => 'Queued',
                        'published' => 'Published'
                    ],
                    'required' => true,
                    'default' => 'unmapped'
                ],
                [
                    'name' => 'cluster_id',
                    'type' => 'select',
                    'label' => 'Cluster',
                    'source' => 'igny8_get_cluster_options',
                    'required' => false
                ]
            ],
            'title' => 'Keyword',
            'submit_text' => 'Save Keyword'
        ],
        'planner_clusters' => [
            'fields' => [
                [
                    'name' => 'cluster_name',
                    'type' => 'text',
                    'label' => 'Cluster Name',
                    'required' => true
                ],
                [
                    'name' => 'sector_id',
                    'type' => 'select',
                    'label' => 'Sector',
                    'source' => 'igny8_get_sector_options',
                    'required' => false
                ],
                [
                    'name' => 'status',
                    'type' => 'select',
                    'label' => 'Status',
                    'options' => [
                        'active' => 'Active',
                        'inactive' => 'Inactive',
                        'archived' => 'Archived'
                    ],
                    'required' => true,
                    'default' => 'active'
                ],
            ],
            'title' => 'Cluster',
            'submit_text' => 'Save Cluster'
        ],
        'planner_ideas' => [
            'fields' => [
                [
                    'name' => 'idea_title',
                    'type' => 'text',
                    'label' => 'Idea Title',
                    'required' => true
                ],
                [
                    'name' => 'idea_description',
                    'type' => 'textarea',
                    'label' => 'Description',
                    'required' => false
                ],
                [
                    'name' => 'content_structure',
                    'type' => 'select',
                    'label' => 'Content Structure',
                    'options' => [
                        'cluster_hub' => 'Cluster Hub',
                        'landing_page' => 'Landing Page',
                        'guide_tutorial' => 'Guide Tutorial',
                        'how_to' => 'How To',
                        'comparison' => 'Comparison',
                        'review' => 'Review',
                        'top_listicle' => 'Top Listicle',
                        'question' => 'FAQ',
                        'product_description' => 'Product Description',
                        'service_page' => 'Service Page',
                        'home_page' => 'Home Page'
                    ],
                    'required' => true,
                    'default' => 'cluster_hub'
                ],
                [
                    'name' => 'content_type',
                    'type' => 'select',
                    'label' => 'Content Type',
                    'options' => [
                        'post' => 'Post',
                        'product' => 'Product',
                        'page' => 'Page',
                        'CPT' => 'Custom Post Type'
                    ],
                    'required' => true,
                    'default' => 'post'
                ],
                [
                    'name' => 'target_keywords',
                    'type' => 'textarea',
                    'label' => 'Target Keywords (comma-separated)',
                    'required' => false,
                    'placeholder' => 'Enter target keywords for this idea, separated by commas...',
                    'rows' => 3
                ],
                [
                    'name' => 'image_prompts',
                    'type' => 'textarea',
                    'label' => 'Image Prompts (JSON)',
                    'required' => false,
                    'placeholder' => 'Enter image prompts as JSON...',
                    'rows' => 4,
                    'help_text' => 'JSON format: {"featured_image": "prompt", "in_article_image_1": "prompt", "in_article_image_2": "prompt"}'
                ],
                [
                    'name' => 'keyword_cluster_id',
                    'type' => 'select',
                    'label' => 'Cluster',
                    'source' => 'igny8_get_cluster_options',
                    'required' => false
                ],
                [
                    'name' => 'source',
                    'type' => 'select',
                    'label' => 'Source',
                    'options' => [
                        'AI' => 'AI',
                        'Manual' => 'Manual'
                    ],
                    'required' => true,
                    'default' => 'AI'
                ],
                [
                    'name' => 'status',
                    'type' => 'select',
                    'label' => 'Status',
                    'options' => [
                        'new' => 'New',
                        'scheduled' => 'Scheduled',
                        'published' => 'Published'
                    ],
                    'required' => true,
                    'default' => 'new'
                ],
                [
                    'name' => 'estimated_word_count',
                    'type' => 'number',
                    'label' => 'Estimated Words',
                    'required' => false
                ]
            ],
            'title' => 'Content Idea',
            'submit_text' => 'Save Idea'
        ],
        'writer_tasks' => [
            'fields' => [
                [
                    'name' => 'title',
                    'type' => 'text',
                    'label' => 'Task Title',
                    'required' => true
                ],
                [
                    'name' => 'cluster_id',
                    'type' => 'select',
                    'label' => 'Cluster Name',
                    'source' => 'igny8_get_cluster_options',
                    'required' => false
                ],
                [
                    'name' => 'keywords',
                    'type' => 'text',
                    'label' => 'Keywords',
                    'required' => false
                ],
                [
                    'name' => 'word_count',
                    'type' => 'number',
                    'label' => 'Word Count',
                    'required' => false
                ],
                [
                    'name' => 'status',
                    'type' => 'select',
                    'label' => 'Status',
                    'options' => [
                        'queued' => 'Queued',
                        'in_progress' => 'In Progress',
                        'completed' => 'Completed',
                        'cancelled' => 'Cancelled',
                        'draft' => 'Draft',
                        'review' => 'Review',
                        'published' => 'Published'
                    ],
                    'required' => true,
                    'default' => 'queued'
                ],
                [
                    'name' => 'content_structure',
                    'type' => 'select',
                    'label' => 'Content Structure',
                    'options' => [
                        'cluster_hub' => 'Cluster Hub',
                        'landing_page' => 'Landing Page',
                        'guide_tutorial' => 'Guide Tutorial',
                        'how_to' => 'How To',
                        'comparison' => 'Comparison',
                        'review' => 'Review',
                        'top_listicle' => 'Top Listicle',
                        'question' => 'FAQ',
                        'product_description' => 'Product Description',
                        'service_page' => 'Service Page',
                        'home_page' => 'Home Page'
                    ],
                    'required' => true,
                    'default' => 'cluster_hub'
                ],
                [
                    'name' => 'content_type',
                    'type' => 'select',
                    'label' => 'Content Type',
                    'options' => [
                        'post' => 'Post',
                        'product' => 'Product',
                        'page' => 'Page',
                        'CPT' => 'Custom Post Type'
                    ],
                    'required' => true,
                    'default' => 'post'
                ],
                [
                    'name' => 'created_at',
                    'type' => 'text',
                    'label' => 'Created',
                    'required' => false,
                    'readonly' => true
                ]
            ],
            'title' => 'Queue Task',
            'submit_text' => 'Add to Queue'
        ],

        'writer_drafts' => [
            'fields' => [
                [
                    'name' => 'title',
                    'type' => 'text',
                    'label' => 'Title',
                    'required' => true
                ],
                [
                    'name' => 'status',
                    'type' => 'select',
                    'label' => 'Status',
                    'options' => [
                        'draft' => 'Draft'
                    ],
                    'required' => true,
                    'default' => 'draft'
                ],
                [
                    'name' => 'cluster_id',
                    'type' => 'select',
                    'label' => 'Cluster',
                    'source' => 'igny8_get_cluster_options',
                    'required' => false
                ],
                [
                    'name' => 'content_structure',
                    'type' => 'select',
                    'label' => 'Content Structure',
                    'options' => [
                        'cluster_hub' => 'Cluster Hub',
                        'landing_page' => 'Landing Page',
                        'guide_tutorial' => 'Guide Tutorial',
                        'how_to' => 'How To',
                        'comparison' => 'Comparison',
                        'review' => 'Review',
                        'top_listicle' => 'Top Listicle',
                        'question' => 'FAQ',
                        'product_description' => 'Product Description',
                        'service_page' => 'Service Page',
                        'home_page' => 'Home Page'
                    ],
                    'required' => false,
                    'default' => 'cluster_hub'
                ],
                [
                    'name' => 'content_type',
                    'type' => 'select',
                    'label' => 'Content Type',
                    'options' => [
                        'post' => 'Post',
                        'product' => 'Product',
                        'page' => 'Page',
                        'CPT' => 'Custom Post Type'
                    ],
                    'required' => false,
                    'default' => 'post'
                ],
                [
                    'name' => 'meta_title',
                    'label' => 'Meta Title',
                    'type' => 'text',
                    'placeholder' => 'Enter SEO title...',
                    'maxlength' => 60
                ],
                [
                    'name' => 'meta_description',
                    'label' => 'Meta Description',
                    'type' => 'textarea',
                    'placeholder' => 'Enter meta description...',
                    'maxlength' => 160
                ],
                [
                    'name' => 'keywords',
                    'label' => 'Primary Keywords',
                    'type' => 'text',
                    'placeholder' => 'e.g., duvet covers, king size'
                ],
                [
                    'name' => 'word_count',
                    'label' => 'Word Count',
                    'type' => 'number',
                    'readonly' => true
                ],
                [
                    'name' => 'updated_at',
                    'type' => 'text',
                    'label' => 'Updated',
                    'required' => false,
                    'readonly' => true
                ]
            ],
            'title' => 'Content Draft',
            'submit_text' => 'Save Draft'
        ],

        'writer_published' => [
            'fields' => [
                [
                    'name' => 'title',
                    'type' => 'text',
                    'label' => 'Title',
                    'required' => true
                ],
                [
                    'name' => 'status',
                    'type' => 'select',
                    'label' => 'Status',
                    'options' => [
                        'published' => 'Published'
                    ],
                    'required' => true,
                    'default' => 'published'
                ],
                [
                    'name' => 'cluster_id',
                    'type' => 'select',
                    'label' => 'Cluster',
                    'source' => 'igny8_get_cluster_options',
                    'required' => false
                ],
                [
                    'name' => 'content_structure',
                    'type' => 'select',
                    'label' => 'Content Structure',
                    'options' => [
                        'cluster_hub' => 'Cluster Hub',
                        'landing_page' => 'Landing Page',
                        'guide_tutorial' => 'Guide Tutorial',
                        'how_to' => 'How To',
                        'comparison' => 'Comparison',
                        'review' => 'Review',
                        'top_listicle' => 'Top Listicle',
                        'question' => 'FAQ',
                        'product_description' => 'Product Description',
                        'service_page' => 'Service Page',
                        'home_page' => 'Home Page'
                    ],
                    'required' => false,
                    'default' => 'cluster_hub'
                ],
                [
                    'name' => 'content_type',
                    'type' => 'select',
                    'label' => 'Content Type',
                    'options' => [
                        'post' => 'Post',
                        'product' => 'Product',
                        'page' => 'Page',
                        'CPT' => 'Custom Post Type'
                    ],
                    'required' => false,
                    'default' => 'post'
                ],
                [
                    'name' => 'meta_title',
                    'label' => 'Meta Title',
                    'type' => 'text',
                    'placeholder' => 'Enter SEO title...',
                    'maxlength' => 60
                ],
                [
                    'name' => 'meta_description',
                    'label' => 'Meta Description',
                    'type' => 'textarea',
                    'placeholder' => 'Enter meta description...',
                    'maxlength' => 160
                ],
                [
                    'name' => 'keywords',
                    'label' => 'Primary Keywords',
                    'type' => 'text',
                    'placeholder' => 'e.g., duvet covers, king size'
                ],
                [
                    'name' => 'word_count',
                    'label' => 'Word Count',
                    'type' => 'number',
                    'readonly' => true
                ],
                [
                    'name' => 'updated_at',
                    'type' => 'text',
                    'label' => 'Updated',
                    'required' => false,
                    'readonly' => true
                ]
            ],
            'title' => 'Published Content',
            'submit_text' => 'Save Published Content'
        ],


        'writer_templates' => [
            'fields' => [
                [
                    'name' => 'prompt_name',
                    'type' => 'text',
                    'label' => 'Template Name',
                    'required' => true,
                    'placeholder' => 'Enter template name...'
                ],
                [
                    'name' => 'prompt_type',
                    'type' => 'select',
                    'label' => 'Category',
                    'options' => [
                        'content' => 'Blog',
                        'optimization' => 'Review',
                        'generation' => 'Product',
                        'custom' => 'Custom'
                    ],
                    'required' => true,
                    'default' => 'content'
                ],
                [
                    'name' => 'is_active',
                    'type' => 'select',
                    'label' => 'Status',
                    'options' => [
                        '1' => 'Active',
                        '0' => 'Draft'
                    ],
                    'required' => true,
                    'default' => '1'
                ],
                [
                    'name' => 'prompt_text',
                    'type' => 'textarea',
                    'label' => 'Prompt Body',
                    'required' => true,
                    'rows' => 10,
                    'placeholder' => 'Enter the prompt template...'
                ],
                [
                    'name' => 'variables',
                    'type' => 'textarea',
                    'label' => 'Variables (JSON)',
                    'required' => false,
                    'rows' => 5,
                    'placeholder' => '{"label": "Custom Label", "description": "Template description"}'
                ]
            ],
            'title' => 'Content Template',
            'submit_text' => 'Save Template'
        ],

        'personalize_data' => [
            'fields' => [
                [
                    'name' => 'post_id',
                    'type' => 'number',
                    'label' => 'Post ID',
                    'required' => true
                ],
                [
                    'name' => 'data_type',
                    'type' => 'text',
                    'label' => 'Data Type',
                    'required' => true
                ],
                [
                    'name' => 'data',
                    'type' => 'textarea',
                    'label' => 'Data (JSON)',
                    'required' => true,
                    'rows' => 10
                ]
            ],
            'title' => 'Personalization Data',
            'submit_text' => 'Save Data'
        ],

        'personalize_variations' => [
            'fields' => [
                [
                    'name' => 'post_id',
                    'type' => 'number',
                    'label' => 'Post ID',
                    'required' => true
                ],
                [
                    'name' => 'fields_hash',
                    'type' => 'text',
                    'label' => 'Fields Hash',
                    'required' => true
                ],
                [
                    'name' => 'fields_json',
                    'type' => 'textarea',
                    'label' => 'Fields JSON',
                    'required' => true,
                    'rows' => 5
                ],
                [
                    'name' => 'content',
                    'type' => 'textarea',
                    'label' => 'Content',
                    'required' => true,
                    'rows' => 15
                ]
            ],
            'title' => 'Content Variation',
            'submit_text' => 'Save Variation'
        ]
    ];
}
