<?php

namespace Tests\Feature;

use Tests\TestCase;

/**
 * Test that helper functions are properly autoloaded in production
 * This addresses the issue where helpers were in autoload-dev and not available in production
 */
class HelperFunctionsAutoloadTest extends TestCase
{
    /**
     * Test that TimeHelper functions are available (not undefined)
     */
    public function test_time_helper_functions_are_defined(): void
    {
        // Test that all TimeHelper functions exist
        $this->assertTrue(function_exists('getTodayWorkingHours'), 'getTodayWorkingHours() function should be defined');
        $this->assertTrue(function_exists('getWeeklyWorkingHours'), 'getWeeklyWorkingHours() function should be defined');
        $this->assertTrue(function_exists('getMonthlyWorkingHours'), 'getMonthlyWorkingHours() function should be defined');
        $this->assertTrue(function_exists('calculateWorkingHours'), 'calculateWorkingHours() function should be defined');
        $this->assertTrue(function_exists('getWorkingHoursBetween'), 'getWorkingHoursBetween() function should be defined');
        $this->assertTrue(function_exists('formatHours'), 'formatHours() function should be defined');
    }

    /**
     * Test that getTodayWorkingHours can be called without errors
     */
    public function test_get_today_working_hours_can_be_called(): void
    {
        // This should not throw "Call to undefined function" error
        $hours = getTodayWorkingHours();
        
        // Should return a float (even if 0.0 when no attendance records)
        $this->assertIsFloat($hours);
        $this->assertGreaterThanOrEqual(0, $hours);
    }

    /**
     * Test that getWeeklyWorkingHours can be called without errors
     */
    public function test_get_weekly_working_hours_can_be_called(): void
    {
        // This should not throw "Call to undefined function" error
        $hours = getWeeklyWorkingHours();
        
        // Should return a float (even if 0.0 when no attendance records)
        $this->assertIsFloat($hours);
        $this->assertGreaterThanOrEqual(0, $hours);
    }

    /**
     * Test that formatHours function works correctly
     */
    public function test_format_hours_works_correctly(): void
    {
        // Test various hour formats
        $this->assertEquals('0m', formatHours(0));
        $this->assertEquals('1h', formatHours(1));
        $this->assertEquals('8h', formatHours(8));
        $this->assertEquals('8h 30m', formatHours(8.5));
        $this->assertEquals('45m', formatHours(0.75));
    }

    /**
     * Test that calculateWorkingHours handles empty input
     */
    public function test_calculate_working_hours_handles_empty_input(): void
    {
        // Empty array should return 0.0
        $hours = calculateWorkingHours([]);
        $this->assertEquals(0.0, $hours);
        
        // Empty collection should return 0.0
        $hours = calculateWorkingHours(collect([]));
        $this->assertEquals(0.0, $hours);
    }
}
