<?php

use App\Helpers\DateHelper;
use App\Models\User;
use Illuminate\Support\Facades\App;

describe('Language Toggle Route', function () {
    test('language toggle stores preference in session for guests', function () {
        $this->get(route('language.switch', 'en'))
            ->assertRedirect();

        expect(session('locale'))->toBe('en');

        $this->get(route('language.switch', 'ar'))
            ->assertRedirect();

        expect(session('locale'))->toBe('ar');
    });

    test('language toggle stores preference in database for authenticated users', function () {
        $user = User::factory()->create(['preferred_language' => 'ar']);

        $this->actingAs($user)
            ->get(route('language.switch', 'en'))
            ->assertRedirect();

        expect(session('locale'))->toBe('en');
        expect($user->fresh()->preferred_language)->toBe('en');
    });

    test('language toggle rejects invalid locales', function () {
        $this->get(route('language.switch', 'fr'))
            ->assertStatus(400);

        $this->get(route('language.switch', 'invalid'))
            ->assertStatus(400);
    });

    test('language switch redirects back to same page', function () {
        $this->from('/login')
            ->get(route('language.switch', 'en'))
            ->assertRedirect('/login');
    });
});

describe('SetLocale Middleware', function () {
    test('middleware sets correct locale from user preference', function () {
        $user = User::factory()->create(['preferred_language' => 'en']);

        $this->actingAs($user)
            ->get(route('home'));

        expect(App::getLocale())->toBe('en');
    });

    test('middleware falls back to session for guests', function () {
        session(['locale' => 'en']);

        $this->get(route('home'));

        expect(App::getLocale())->toBe('en');
    });

    test('middleware defaults to Arabic when no preference set', function () {
        $this->get(route('home'));

        expect(App::getLocale())->toBe('ar');
    });
});

describe('Date Formatting Helper', function () {
    test('date formatting helper returns DD/MM/YYYY for Arabic locale', function () {
        $date = '2024-12-25';

        $formatted = DateHelper::formatDate($date, 'ar');

        expect($formatted)->toBe('25/12/2024');
    });

    test('date formatting helper returns MM/DD/YYYY for English locale', function () {
        $date = '2024-12-25';

        $formatted = DateHelper::formatDate($date, 'en');

        expect($formatted)->toBe('12/25/2024');
    });

    test('date formatting helper handles null values', function () {
        expect(DateHelper::formatDate(null))->toBe('');
        expect(DateHelper::formatTime(null))->toBe('');
        expect(DateHelper::formatDateTime(null))->toBe('');
    });

    test('date formatting helper uses current locale by default', function () {
        App::setLocale('ar');
        expect(DateHelper::formatDate('2024-12-25'))->toBe('25/12/2024');

        App::setLocale('en');
        expect(DateHelper::formatDate('2024-12-25'))->toBe('12/25/2024');
    });

    test('time formatting helper returns 12-hour format', function () {
        expect(DateHelper::formatTime('14:30:00'))->toBe('2:30 PM');
        expect(DateHelper::formatTime('08:15:00'))->toBe('8:15 AM');
    });

    test('datetime formatting helper combines date and time correctly', function () {
        $datetime = '2024-12-25 14:30:00';

        expect(DateHelper::formatDateTime($datetime, 'ar'))->toBe('25/12/2024 2:30 PM');
        expect(DateHelper::formatDateTime($datetime, 'en'))->toBe('12/25/2024 2:30 PM');
    });
});

describe('Translation Files', function () {
    test('Arabic translation files exist and have content', function () {
        expect(__('auth.failed', [], 'ar'))->not->toBe('auth.failed');
        expect(__('validation.required', [], 'ar'))->not->toBe('validation.required');
        expect(__('navigation.dashboard', [], 'ar'))->not->toBe('navigation.dashboard');
        expect(__('pagination.previous', [], 'ar'))->not->toBe('pagination.previous');
    });

    test('English translation files exist and have content', function () {
        expect(__('auth.failed', [], 'en'))->not->toBe('auth.failed');
        expect(__('validation.required', [], 'en'))->not->toBe('validation.required');
        expect(__('navigation.dashboard', [], 'en'))->not->toBe('navigation.dashboard');
        expect(__('pagination.previous', [], 'en'))->not->toBe('pagination.previous');
    });

    test('validation attribute translations work', function () {
        expect(__('validation.attributes.email', [], 'ar'))->toBe('البريد الإلكتروني');
        expect(__('validation.attributes.email', [], 'en'))->toBe('email');
    });
});
