<?php
/**
 * Base Form Manager 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 Form Manager
 *
 * Provides common functionality for form managers.
 *
 * @since 1.0.0
 */
abstract class BaseFormManager {
    
    /**
     * Field registration component
     *
     * @var BaseFieldRegistration
     */
    protected $field_registration;

    /**
     * Field renderer component
     *
     * @var FieldRenderer
     */
    protected $field_renderer;

    /**
     * Item field renderer component
     *
     * @var ItemFieldRenderer
     */
    protected $item_field_renderer;

    /**
     * Form processor component
     *
     * @var FormProcessor
     */
    protected $form_processor;

    /**
     * Constructor
     *
     * @since 1.0.0
     */
    public function __construct() {
        $this->field_renderer = new FieldRenderer();
        $this->item_field_renderer = new ItemFieldRenderer();
        $this->form_processor = new FormProcessor();
        
        $this->initializeDefaults();
    }

    /**
     * Initialize default tabs, fields, and templates
     *
     * @since 1.0.0
     * @return void
     */
    abstract protected function initializeDefaults(): void;

    /**
     * Get all registered tabs
     *
     * @since 1.0.0
     * @return array
     */
    public function getTabs(): array {
        return $this->field_registration->getTabs();
    }

    /**
     * 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 {
        return $this->field_registration->getFields($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 {
        return $this->field_registration->getFieldsBySection($tab_id, $section);
    }

    /**
     * Get all registered templates
     *
     * @since 1.0.0
     * @return array
     */
    public function getTemplates(): array {
        return $this->field_registration->getTemplates();
    }

    /**
     * Get all registered item fields
     *
     * @since 1.0.0
     * @return array
     */
    public function getItemFields(): array {
        return $this->field_registration->getItemFields();
    }

    /**
     * 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 {
        return $this->field_registration->getItemFieldsByContainerClass($container_class);
    }

    /**
     * Render a field based on its type
     *
     * @since 1.0.0
     * @param array $field Field configuration
     * @param mixed $object Object or null
     * @return string Rendered HTML
     */
    public function renderField(array $field, $object = null): string {
        return $this->field_renderer->renderField($field, $object);
    }

    /**
     * Generate item template HTML
     *
     * @since 1.0.0
     * @return string
     */
    public function generateItemTemplateHtml(): string {
        return $this->item_field_renderer->generateItemTemplateHtml($this->field_registration);
    }

    /**
     * Process form data
     *
     * @since 1.0.0
     * @param array $form_data Form data
     * @return array Processed data
     */
    public function processFormData(array $form_data): array {
        return $this->form_processor->process($form_data);
    }

    /**
     * Get all fields from all tabs
     *
     * @since 1.0.0
     * @return array All fields
     */
    public function getAllFields(): array {
        $all_fields = [];
        $tabs = $this->getTabs();
        
        foreach ($tabs as $tab) {
            $tab_fields = $this->getFields($tab['id']);
            $all_fields = array_merge($all_fields, $tab_fields);
        }
        
        return $all_fields;
    }

    /**
     * Get field by name
     *
     * @since 1.0.0
     * @param string $field_name Field name
     * @return array|null Field configuration or null
     */
    public function getFieldByName(string $field_name): ?array {
        $all_fields = $this->getAllFields();
        
        foreach ($all_fields as $field) {
            if (($field['name'] ?? '') === $field_name) {
                return $field;
            }
        }
        
        return null;
    }

    /**
     * Get item field by name
     *
     * @since 1.0.0
     * @param string $field_name Field name
     * @return array|null Field configuration or null
     */
    public function getItemFieldByName(string $field_name): ?array {
        $item_fields = $this->getItemFields();
        
        foreach ($item_fields as $field) {
            if (($field['name'] ?? '') === $field_name) {
                return $field;
            }
        }
        
        return null;
    }
} 