<?hh // strict

namespace App\Services;

use App\Models\User;
use DateTime;

class ComplianceService
{
    /**
     * Check if user data is compliant with USA employment laws
     */
    public function isUserDataCompliant(User $user): bool
    {
        // Check if required fields for USA compliance are present
        $requiredFields = [
            'name' => $user->getName(),
            'email' => $user->getEmail(),
        ];
        
        foreach ($requiredFields as $field => $value) {
            if (empty($value)) {
                return false;
            }
        }
        
        return true;
    }
    
    /**
     * Ensure job posting is compliant with USA employment laws
     */
    public function isJobPostingCompliant(array $jobData): bool
    {
        // Ensure no discriminatory language in job title or description
        $discriminatoryTerms = [
            'male only', 'female only', 'under 30', 'over 40',
            'no disabled', 'young only', 'recent graduate only', 
            'no seniors', 'must be citizen', // unless citizenship is required by law
        ];
        
        $textToCheck = strtolower($jobData['title'] . ' ' . $jobData['description']);
        
        foreach ($discriminatoryTerms as $term) {
            if (strpos($textToCheck, $term) !== false) {
                return false;
            }
        }
        
        // Ensure required information is present
        $requiredFields = ['title', 'description', 'location'];
        foreach ($requiredFields as $field) {
            if (empty($jobData[$field])) {
                return false;
            }
        }
        
        return true;
    }
    
    /**
     * Log data access for compliance auditing
     */
    public function logDataAccess(string $userId, string $action, string $resource, ?string $tenantId = null): void
    {
        $logEntry = [
            'user_id' => $userId,
            'action' => $action,
            'resource' => $resource,
            'tenant_id' => $tenantId,
            'timestamp' => (new DateTime())->format('Y-m-d H:i:s'),
            'ip_address' => $_SERVER['REMOTE_ADDR'] ?? 'unknown',
            'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? 'unknown',
        ];
        
        // In a real application, this would be stored in a dedicated audit log table
        error_log(json_encode($logEntry));
    }
    
    /**
     * Anonymize user data in accordance with compliance requirements
     */
    public function anonymizeUserData(array &$userData): void
    {
        // Remove or anonymize PII based on retention policies
        if (isset($userData['email'])) {
            $userData['email'] = '[ANONYMIZED]';
        }
        
        if (isset($userData['name'])) {
            $userData['name'] = '[ANONYMIZED]';
        }
        
        if (isset($userData['phone'])) {
            $userData['phone'] = '[ANONYMIZED]';
        }
        
        // Add an anonymization timestamp
        $userData['anonymized_at'] = (new DateTime())->format('Y-m-d H:i:s');
    }
    
    /**
     * Validate that data retention follows USA law requirements
     */
    public function validateDataRetention(string $dataCategory, DateTime $creationDate): bool
    {
        // Different data types have different retention requirements
        $retentionPeriods = [
            'application' => 4 * 365, // 4 years for employment applications
            'interview' => 4 * 365,   // 4 years for interview records
            'offer' => 4 * 365,       // 4 years for offer letters
            'employee' => 4 * 365,    // 4 years for employee records
            'general' => 7 * 365,     // 7 years for general business records
        ];
        
        $daysStored = (new DateTime())->diff($creationDate)->days;
        $maxRetentionDays = $retentionPeriods[$dataCategory] ?? $retentionPeriods['general'];
        
        return $daysStored <= $maxRetentionDays;
    }
}