<?php
/**
 * Basic tests for event error conditions
 */

class Event_Error_Test extends PHPUnit\Framework\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'))
        ];
        
        $this->assertFalse($this->validate_event_data($event_data), 'Event should not be valid without title');
    }
    
    /**
     * 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'
        ];
        
        $this->assertFalse($this->validate_event_data($event_data), 'Event should not be valid with invalid dates');
        
        // 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'))
        ];
        
        $this->assertFalse($this->validate_event_data($event_data), 'Event should not be valid with past dates');
    }
    
    /**
     * 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'))
        ];
        
        $this->assertTrue($this->validate_event_data($event_data), 'Event should be valid with long title');
        $this->assertLessThanOrEqual(190, strlen($event_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'))
        ];
        
        $this->assertTrue($this->validate_event_data($event_data), 'Event should be valid with all required fields');
    }
    
    /**
     * Helper function to validate event data
     */
    private function validate_event_data($event_data) {
        // Check required fields
        if (empty($event_data['post_title'])) {
            return false;
        }
        
        // Validate dates
        if (!empty($event_data['EventStartDate'])) {
            try {
                $start_date = new DateTime($event_data['EventStartDate']);
                if ($start_date < new DateTime()) {
                    return false;
                }
            } catch (Exception $e) {
                return false;
            }
        }
        
        if (!empty($event_data['EventEndDate'])) {
            try {
                new DateTime($event_data['EventEndDate']);
            } catch (Exception $e) {
                return false;
            }
        }
        
        // Truncate title if needed
        if (strlen($event_data['post_title']) > 190) {
            $event_data['post_title'] = substr($event_data['post_title'], 0, 190);
        }
        
        return true;
    }
}