<?php

/**
 * Form Helper
 * Provides helper functions for forms
 */
class FormHelper {
    
    /**
     * Generate CSRF token field
     * 
     * @return string HTML input field
     */
    public static function csrfField() {
        $token = generateCsrfToken();
        return '<input type="hidden" name="csrf_token" value="' . htmlspecialchars($token) . '">';
    }
    
    /**
     * Generate form opening tag with CSRF protection
     * 
     * @param string $action Form action URL
     * @param string $method Form method (GET/POST)
     * @param array $attributes Additional attributes
     * @return string HTML form tag
     */
    public static function open($action, $method = 'POST', $attributes = []) {
        $method = strtoupper($method);
        $attrs = '';
        
        foreach ($attributes as $key => $value) {
            $attrs .= ' ' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
        }
        
        $html = '<form action="' . htmlspecialchars($action) . '" method="' . $method . '"' . $attrs . '>';
        
        // Add CSRF token for POST requests
        if ($method === 'POST') {
            $html .= self::csrfField();
        }
        
        return $html;
    }
    
    /**
     * Generate form closing tag
     * 
     * @return string HTML closing form tag
     */
    public static function close() {
        return '</form>';
    }
    
    /**
     * Generate text input field
     * 
     * @param string $name Field name
     * @param string $value Field value
     * @param array $attributes Additional attributes
     * @return string HTML input field
     */
    public static function text($name, $value = '', $attributes = []) {
        return self::input('text', $name, $value, $attributes);
    }
    
    /**
     * Generate email input field
     * 
     * @param string $name Field name
     * @param string $value Field value
     * @param array $attributes Additional attributes
     * @return string HTML input field
     */
    public static function email($name, $value = '', $attributes = []) {
        return self::input('email', $name, $value, $attributes);
    }
    
    /**
     * Generate password input field
     * 
     * @param string $name Field name
     * @param array $attributes Additional attributes
     * @return string HTML input field
     */
    public static function password($name, $attributes = []) {
        return self::input('password', $name, '', $attributes);
    }
    
    /**
     * Generate generic input field
     * 
     * @param string $type Input type
     * @param string $name Field name
     * @param string $value Field value
     * @param array $attributes Additional attributes
     * @return string HTML input field
     */
    private static function input($type, $name, $value = '', $attributes = []) {
        $attrs = '';
        
        foreach ($attributes as $key => $val) {
            $attrs .= ' ' . htmlspecialchars($key) . '="' . htmlspecialchars($val) . '"';
        }
        
        return '<input type="' . htmlspecialchars($type) . '" name="' . htmlspecialchars($name) . '" value="' . htmlspecialchars($value) . '"' . $attrs . '>';
    }
}
