<?php
namespace HVAC\Tests\Basic;

use PHPUnit\Framework\TestCase;
use DateTime;
use Exception;

/**
 * Basic tests for event error conditions
 */
class EventErrorTest extends TestCase {
    
    /**
     * Test validation of required fields
     */
    public function test_required_fields_validation() {
        // Test missing title
        $event_data = [
            'post_content' => 'Test description',
            'EventStartDate' => date('Y-m-d', strtotime('+1 week')),
            'EventEndDate' => date('Y-m-d', strtotime('+1 week'))
        ];
        
        $result = $this->validate_event_data($event_data);
        $this->assertFalse($result['valid'], 'Event should not be valid without title');
        $this->assertContains('Title is required', $result['errors']);
    }
    
    /**
     * Test date validation
     */
    public function test_date_validation() {
        // Test invalid date format
        $event_data = [
            'post_title' => 'Test Event',
            'post_content' => 'Description',
            'EventStartDate' => 'invalid-date',
            'EventEndDate' => '2025/13/45'
        ];
        
        $result = $this->validate_event_data($event_data);
        $this->assertFalse($result['valid'], 'Event should not be valid with invalid dates');
        $this->assertContains('Invalid date format', $result['errors']);
        
        // Test past dates
        $event_data = [
            'post_title' => 'Past Event',
            'post_content' => 'Description',
            'EventStartDate' => date('Y-m-d', strtotime('-1 week')),
            'EventEndDate' => date('Y-m-d', strtotime('-1 week'))
        ];
        
        $result = $this->validate_event_data($event_data);
        $this->assertFalse($result['valid'], 'Event should not be valid with past dates');
        $this->assertContains('Event dates must be in the future', $result['errors']);
    }
    
    /**
     * Test boundary conditions
     */
    public function test_boundary_conditions() {
        // Test extremely long title
        $event_data = [
            'post_title' => str_repeat('a', 200),
            'post_content' => 'Description',
            'EventStartDate' => date('Y-m-d', strtotime('+1 week')),
            'EventEndDate' => date('Y-m-d', strtotime('+1 week'))
        ];
        
        $result = $this->validate_event_data($event_data);
        $this->assertTrue($result['valid'], 'Event should be valid with long title');
        $this->assertLessThanOrEqual(190, strlen($result['data']['post_title']), 'Title should be truncated');
    }

    /**
     * Test valid event data
     */
    public function test_valid_event_data() {
        $event_data = [
            'post_title' => 'Valid Test Event',
            'post_content' => 'Description',
            'EventStartDate' => date('Y-m-d', strtotime('+1 week')),
            'EventEndDate' => date('Y-m-d', strtotime('+1 week'))
        ];
        
        $result = $this->validate_event_data($event_data);
        $this->assertTrue($result['valid'], 'Event should be valid with all required fields');
        $this->assertEmpty($result['errors'], 'No errors should be present');
    }
    
    /**
     * Helper function to validate event data
     */
    private function validate_event_data($event_data) {
        $errors = [];
        $data = $event_data;
        
        // Check required fields
        if (empty($data['post_title'])) {
            $errors[] = 'Title is required';
            return ['valid' => false, 'errors' => $errors, 'data' => $data];
        }
        
        // Validate dates
        if (!empty($data['EventStartDate'])) {
            try {
                $start_date = new DateTime($data['EventStartDate']);
                if ($start_date < new DateTime()) {
                    $errors[] = 'Event dates must be in the future';
                }
            } catch (Exception $e) {
                $errors[] = 'Invalid date format';
            }
        }
        
        if (!empty($data['EventEndDate'])) {
            try {
                new DateTime($data['EventEndDate']);
            } catch (Exception $e) {
                $errors[] = 'Invalid date format';
            }
        }
        
        // Truncate title if needed
        if (strlen($data['post_title']) > 190) {
            $data['post_title'] = substr($data['post_title'], 0, 190);
        }
        
        return [
            'valid' => empty($errors),
            'errors' => $errors,
            'data' => $data
        ];
    }
}