<?php
/**
 * Base Field Registration Class
 *
 * @package     EasyInvoice
 * @author      Your Name
 * @copyright   Copyright (c) 2023, Your Company
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       1.0.0
 */

namespace EasyInvoice\Forms;

/**
 * Base Field Registration
 *
 * Provides common functionality for field registration.
 *
 * @since 1.0.0
 */
abstract class BaseFieldRegistration {
    
    /**
     * Registered tabs
     *
     * @var array
     */
    protected $tabs = [];

    /**
     * Registered fields
     *
     * @var array
     */
    protected $fields = [];

    /**
     * Registered templates
     *
     * @var array
     */
    protected $templates = [];

    /**
     * Registered item fields
     *
     * @var array
     */
    protected $item_fields = [];

    /**
     * Register a new tab
     *
     * @since 1.0.0
     * @param string $id Tab ID
     * @param array $config Tab configuration
     * @return void
     */
    public function registerTab(string $id, array $config): void {
        // Apply filter to allow pro plugins to modify individual tab config
        $config = apply_filters($this->getTabFilterName(), $config, $id);
        
        // Skip registration if tab was filtered out (set to null/false)
        if (empty($config)) {
            return;
        }
        
        $this->tabs[$id] = array_merge([
            'id' => $id,
            'label' => '',
            'icon' => 'fas fa-tab',
            'active' => false,
            'order' => 0
        ], $config);
    }

    /**
     * Register a new field
     *
     * @since 1.0.0
     * @param string $tab_id Tab ID to register field for
     * @param array $config Field configuration
     * @return void
     */
    public function registerField(string $tab_id, array $config): void {
        // Apply filter to allow pro plugins to modify individual field config
        $config = apply_filters($this->getFieldFilterName(), $config, $tab_id);
        
        // Skip registration if field was filtered out (set to null/false)
        if (empty($config)) {
            return;
        }
        
        // Use field name as ID
        $field_id = $config['name'] ?? uniqid('field_');
        
        if (!isset($this->fields[$tab_id])) {
            $this->fields[$tab_id] = [];
        }
        
        $this->fields[$tab_id][$field_id] = array_merge([
            'id' => $field_id,
            'type' => 'text',
            'label' => '',
            'name' => '',
            'default_value' => '',
            'placeholder' => '',
            'required' => false,
            'grid_cols' => 'sm:col-span-6',
            'order' => 0
        ], $config);
    }

    /**
     * Register a new template
     *
     * @since 1.0.0
     * @param string $id Template ID
     * @param array $config Template configuration
     * @return void
     */
    public function registerTemplate(string $id, array $config): void {
        // Apply filter to allow pro plugins to modify individual template config
        $config = apply_filters($this->getTemplateFilterName(), $config, $id);
        
        // Skip registration if template was filtered out (set to null/false)
        if (empty($config)) {
            return;
        }
        
        $this->templates[$id] = array_merge([
            'id' => $id,
            'name' => '',
            'description' => '',
            'icon' => 'fas fa-file',
            'order' => 0
        ], $config);
    }

    /**
     * Register a new item field
     *
     * @since 1.0.0
     * @param array $config Item field configuration
     * @return void
     */
    public function registerItemField(array $config): void {
        // Apply filter to allow pro plugins to modify individual item field config
        $config = apply_filters($this->getItemFieldFilterName(), $config);
        
        // Skip registration if item field was filtered out (set to null/false)
        if (empty($config)) {
            return;
        }
        
        // Use field name as ID
        $field_id = $config['name'] ?? uniqid('item_field_');
        $this->item_fields[$field_id] = array_merge([
            'id' => $field_id,
            'type' => 'text',
            'label' => '',
            'name' => '',
            'default_value' => '',
            'placeholder' => '',
            'required' => false,
            'container_class' => 'sm:col-span-6',
            'order' => 0
        ], $config);
    }

    /**
     * Get all registered tabs
     *
     * @since 1.0.0
     * @return array
     */
    public function getTabs(): array {
        // Sort tabs by order
        uasort($this->tabs, function($a, $b) {
            return $a['order'] <=> $b['order'];
        });
        
        return $this->tabs;
    }

    /**
     * Get fields for a specific tab
     *
     * @since 1.0.0
     * @param string $tab_id Tab ID
     * @return array
     */
    public function getFields(string $tab_id): array {
        if (!isset($this->fields[$tab_id])) {
            return [];
        }
        
        // Sort fields by order
        uasort($this->fields[$tab_id], function($a, $b) {
            return $a['order'] <=> $b['order'];
        });
        
        return $this->fields[$tab_id];
    }

    /**
     * Get fields by section within a tab
     *
     * @since 1.0.0
     * @param string $tab_id Tab ID
     * @param string $section Section name
     * @return array
     */
    public function getFieldsBySection(string $tab_id, string $section): array {
        $fields = $this->getFields($tab_id);
        $section_fields = [];
        
        foreach ($fields as $field) {
            if (($field['section'] ?? '') === $section) {
                $section_fields[] = $field;
            }
        }
        
        return $section_fields;
    }

    /**
     * Get all registered templates
     *
     * @since 1.0.0
     * @return array
     */
    public function getTemplates(): array {
        // Sort templates by order
        uasort($this->templates, function($a, $b) {
            return $a['order'] <=> $b['order'];
        });
        
        return $this->templates;
    }

    /**
     * Get all registered item fields
     *
     * @since 1.0.0
     * @return array
     */
    public function getItemFields(): array {
        // Sort item fields by order
        uasort($this->item_fields, function($a, $b) {
            return $a['order'] <=> $b['order'];
        });
        
        return $this->item_fields;
    }

    /**
     * Get item fields by container class
     *
     * @since 1.0.0
     * @param string $container_class Container class to filter by
     * @return array
     */
    public function getItemFieldsByContainerClass(string $container_class): array {
        $fields = $this->getItemFields();
        $filtered_fields = [];
        
        foreach ($fields as $field) {
            if (isset($field['container_class']) && $field['container_class'] === $container_class) {
                $filtered_fields[] = $field;
            }
        }
        
        return $filtered_fields;
    }

    /**
     * Get the filter name for tabs
     *
     * @since 1.0.0
     * @return string
     */
    abstract protected function getTabFilterName(): string;

    /**
     * Get the filter name for fields
     *
     * @since 1.0.0
     * @return string
     */
    abstract protected function getFieldFilterName(): string;

    /**
     * Get the filter name for templates
     *
     * @since 1.0.0
     * @return string
     */
    abstract protected function getTemplateFilterName(): string;

    /**
     * Get the filter name for item fields
     *
     * @since 1.0.0
     * @return string
     */
    abstract protected function getItemFieldFilterName(): string;
} 