<?php

require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/Logger.php';

/**
 * Validator class for input validation
 */
class Validator {
    // Error messages
    private $errors = [];
    
    /**
     * Validate a sender ID
     * 
     * @param string $senderId Sender ID to validate
     * @return bool Validation result
     */
    public function validateSenderId($senderId) {
        // Check length (3-11 characters)
        if (strlen($senderId) < 3 || strlen($senderId) > 11) {
            $this->errors['sender_id'] = 'Sender ID must be between 3 and 11 characters';
            return false;
        }
        
        // Check alphanumeric with space allowed
        if (!preg_match('/^[A-Za-z0-9 ]+$/', $senderId)) {
            $this->errors['sender_id'] = 'Sender ID must contain only letters, numbers, and spaces';
            return false;
        }
        
        return true;
    }
    
    /**
     * Validate request ID format
     * 
     * @param string $requestId Request ID to validate
     * @return bool Validation result
     */
    public function validateRequestId($requestId) {
        // Check if the request ID starts with REQ_ and followed by numeric digits
        if (!preg_match('/^REQ_\d+$/', $requestId)) {
            $this->errors['request_id'] = 'Invalid request ID format';
            return false;
        }
        
        return true;
    }
    
    /**
     * Validate pagination parameters
     * 
     * @param int $page Page number
     * @param int $limit Page size
     * @return bool Validation result
     */
    public function validatePagination($page, $limit) {
        // Page must be positive integer
        if (!is_numeric($page) || $page < 1 || floor($page) != $page) {
            $this->errors['page'] = 'Page must be a positive integer';
            return false;
        }
        
        // Limit must be between 1 and 100
        if (!is_numeric($limit) || $limit < 1 || $limit > 100 || floor($limit) != $limit) {
            $this->errors['limit'] = 'Limit must be an integer between 1 and 100';
            return false;
        }
        
        return true;
    }
    
    /**
     * Validate date format (YYYY-MM-DD)
     * 
     * @param string $date Date string to validate
     * @param string $field Field name for error message
     * @return bool Validation result
     */
    public function validateDate($date, $field) {
        if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
            $this->errors[$field] = "{$field} must be in YYYY-MM-DD format";
            return false;
        }
        
        $dateTime = DateTime::createFromFormat('Y-m-d', $date);
        if (!$dateTime || $dateTime->format('Y-m-d') !== $date) {
            $this->errors[$field] = "Invalid date for {$field}";
            return false;
        }
        
        return true;
    }
    
    /**
     * Validate required fields
     * 
     * @param array $data Input data
     * @param array $requiredFields List of required fields
     * @return bool Validation result
     */
    public function validateRequiredFields($data, $requiredFields) {
        $valid = true;
        
        foreach ($requiredFields as $field) {
            if (!isset($data[$field]) || trim($data[$field]) === '') {
                $this->errors[$field] = "Field {$field} is required";
                $valid = false;
            }
        }
        
        return $valid;
    }
    
    /**
     * Get validation errors
     * 
     * @return array List of validation errors
     */
    public function getErrors() {
        return $this->errors;
    }
    
    /**
     * Check if validation has errors
     * 
     * @return bool True if there are validation errors
     */
    public function hasErrors() {
        return !empty($this->errors);
    }
    
    /**
     * Get first error message
     * 
     * @return string First error message
     */
    public function getFirstError() {
        if (empty($this->errors)) {
            return '';
        }
        
        return reset($this->errors);
    }
} 