<?php

namespace Tests\Feature;

use App\Http\Controllers\EventoController;
use App\Models\Evento;
use Tests\TestCase;

class CalendarEventsTest extends TestCase
{
    public function test_find_first_sunday_of_june_2026(): void
    {
        $controller = app(EventoController::class);
        $june2026 = \Carbon\Carbon::create(2026, 6, 1);
        
        $method = new \ReflectionMethod($controller, 'findNthWeekdayOfMonth');
        $method->setAccessible(true);
        
        $result = $method->invoke($controller, $june2026, 1, 0);
        
        $this->assertNotNull($result);
        $this->assertEquals('2026-06-07', $result->format('Y-m-d'));
        $this->assertEquals('Sunday', $result->format('l'));
    }
    
    public function test_find_second_saturday_of_june_2026(): void
    {
        $controller = app(EventoController::class);
        $june2026 = \Carbon\Carbon::create(2026, 6, 1);
        
        $method = new \ReflectionMethod($controller, 'findNthWeekdayOfMonth');
        $method->setAccessible(true);
        
        $result = $method->invoke($controller, $june2026, 2, 6);
        
        $this->assertNotNull($result);
        $this->assertEquals('2026-06-13', $result->format('Y-m-d'));
    }
    
    public function test_find_fifth_sunday_returns_null_when_not_in_month(): void
    {
        $controller = app(EventoController::class);
        $june2026 = \Carbon\Carbon::create(2026, 6, 1);
        
        $method = new \ReflectionMethod($controller, 'findNthWeekdayOfMonth');
        $method->setAccessible(true);
        
        $result = $method->invoke($controller, $june2026, 5, 0);
        
        $this->assertNull($result);
    }
    
    public function test_generate_mensile_events_for_all_months(): void
    {
        $controller = app(EventoController::class);
        
        $evento = Evento::make([
            'nome_evento' => 'Test Mensile',
            'tipo_recorrenza' => 'mensile',
            'occorrenza_mese' => '1,0',
            'mesi_recorrenza' => '1,2,3,4,5,6,7,8,9,10,11,12',
        ]);
        
        $method = new \ReflectionMethod($controller, 'generateMensileEvents');
        $method->setAccessible(true);
        
        $events = $method->invoke($controller, $evento, '2026-06-01', '2026-08-31', 'http://test.local');
        
        $this->assertCount(3, $events);
        $this->assertStringContainsString('2026-06-07', $events[0]['start']);
        $this->assertStringContainsString('2026-07-05', $events[1]['start']);
        $this->assertStringContainsString('2026-08-02', $events[2]['start']);
    }
    
    public function test_generate_mensile_events_respects_selected_months(): void
    {
        $controller = app(EventoController::class);
        
        $evento = Evento::make([
            'nome_evento' => 'Test Mensile',
            'tipo_recorrenza' => 'mensile',
            'occorrenza_mese' => '1,6',
            'mesi_recorrenza' => '1,6',
        ]);
        
        $method = new \ReflectionMethod($controller, 'generateMensileEvents');
        $method->setAccessible(true);
        
        $events = $method->invoke($controller, $evento, '2026-01-01', '2026-12-31', 'http://test.local');
        
        $this->assertCount(2, $events);
        $this->assertStringContainsString('2026-01-03', $events[0]['start']);
        $this->assertStringContainsString('2026-06-06', $events[1]['start']);
    }
    
    public function test_generate_annuale_events_produces_correct_dates(): void
    {
        $controller = app(EventoController::class);
        
        $evento = Evento::make([
            'nome_evento' => 'Test Annuale',
            'tipo_recorrenza' => 'annuale',
            'mese_annuale' => 6,
            'giorno_mese' => 15,
        ]);
        
        $method = new \ReflectionMethod($controller, 'generateAnnualeEvents');
        $method->setAccessible(true);
        
        $events = $method->invoke($controller, $evento, '2026-01-01', '2027-12-31', 'http://test.local');
        
        $this->assertCount(2, $events);
        $this->assertStringContainsString('2026-06-15', $events[0]['start']);
        $this->assertStringContainsString('2027-06-15', $events[1]['start']);
    }
    
    public function test_generate_settimanale_events_produces_weekly_occurrences(): void
    {
        $controller = app(EventoController::class);
        
        $evento = Evento::make([
            'nome_evento' => 'Test Settimanale',
            'tipo_recorrenza' => 'settimanale',
            'giorno_settimana' => 1,
        ]);
        
        $method = new \ReflectionMethod($controller, 'generateSettimanaleEvents');
        $method->setAccessible(true);
        
        $events = $method->invoke($controller, $evento, '2026-06-01', '2026-06-30', 'http://test.local');
        
        $this->assertCount(5, $events);
    }
}
