<?php

use App\Models\Page;
use App\Models\User;

beforeEach(function () {
    // Seed the legal pages required for terms/privacy tests
    Page::factory()->terms()->create();
    Page::factory()->privacy()->create();
});

describe('Public Pages', function () {
    test('home page is accessible', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('Libra');
    });

    test('booking page is accessible', function () {
        $this->get(route('booking'))
            ->assertOk();
    });

    test('posts page is accessible', function () {
        $this->get(route('posts.index'))
            ->assertOk();
    });

    test('terms page is accessible', function () {
        // Legacy route redirects to /page/terms
        $this->get(route('terms'))
            ->assertRedirect('/page/terms');

        // Actual page responds correctly
        $this->get('/page/terms')
            ->assertOk();
    });

    test('privacy page is accessible', function () {
        // Legacy route redirects to /page/privacy
        $this->get(route('privacy'))
            ->assertRedirect('/page/privacy');

        // Actual page responds correctly
        $this->get('/page/privacy')
            ->assertOk();
    });
});

describe('Navigation Component', function () {
    test('navigation displays on public pages', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="main-navigation"', false);
    });

    test('navigation shows home link', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="nav-home"', false);
    });

    test('navigation shows booking link', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="nav-booking"', false);
    });

    test('navigation shows posts link', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="nav-posts"', false);
    });

    test('navigation shows login link for guests', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="nav-login"', false);
    });

    test('navigation shows dashboard link for authenticated users', function () {
        $user = User::factory()->create();

        $this->actingAs($user)
            ->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="nav-dashboard"', false);
    });

    test('navigation shows logout for authenticated users', function () {
        $user = User::factory()->create();

        $this->actingAs($user)
            ->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="nav-logout"', false);
    });

    test('navigation hides login for authenticated users', function () {
        $user = User::factory()->create();

        $this->actingAs($user)
            ->get(route('home'))
            ->assertOk()
            ->assertDontSee('data-test="nav-login"', false);
    });
});

describe('Mobile Menu', function () {
    test('mobile menu button is present', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="mobile-menu-button"', false);
    });

    test('mobile menu container is present', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="mobile-menu"', false);
    });
});

describe('Footer Component', function () {
    test('footer displays on public pages', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="main-footer"', false);
    });

    test('footer contains terms link', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="footer-terms"', false);
    });

    test('footer contains privacy link', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="footer-privacy"', false);
    });

    test('footer displays current year in copyright', function () {
        $currentYear = date('Y');

        $this->get(route('home'))
            ->assertOk()
            ->assertSee($currentYear);
    });

    test('footer contains clickable phone number', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="footer-phone"', false)
            ->assertSee('href="tel:+970599353502"', false)
            ->assertSee('+970 599 353 502');
    });

    test('footer contains address', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="footer-address"', false);
    });

    test('footer contains clickable email', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="footer-email"', false)
            ->assertSee('href="mailto:info@libra.adv.ps"', false);
    });
});

describe('Language Toggle in Navigation', function () {
    test('language toggle is visible', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="language-switch-ar"', false)
            ->assertSee('data-test="language-switch-en"', false);
    });

    test('switching to Arabic applies RTL layout', function () {
        $this->get(route('language.switch', 'ar'))
            ->assertRedirect();

        $this->get(route('home'))
            ->assertOk()
            ->assertSee('dir="rtl"', false);
    });

    test('switching to English applies LTR layout', function () {
        $this->get(route('language.switch', 'en'))
            ->assertRedirect();

        $this->get(route('home'))
            ->assertOk()
            ->assertSee('dir="ltr"', false);
    });
});

describe('Navigation Translations', function () {
    test('English navigation translations are loaded', function () {
        expect(__('navigation.home', [], 'en'))->toBe('Home');
        expect(__('navigation.booking', [], 'en'))->toBe('Book Consultation');
        expect(__('navigation.posts', [], 'en'))->toBe('Legal Insights');
        expect(__('navigation.login', [], 'en'))->toBe('Login');
        expect(__('navigation.logout', [], 'en'))->toBe('Log Out');
        expect(__('navigation.dashboard', [], 'en'))->toBe('Dashboard');
    });

    test('Arabic navigation translations are loaded', function () {
        expect(__('navigation.home', [], 'ar'))->toBe('الرئيسية');
        expect(__('navigation.booking', [], 'ar'))->toBe('حجز استشارة');
        expect(__('navigation.posts', [], 'ar'))->toBe('مقالات قانونية');
        expect(__('navigation.login', [], 'ar'))->toBe('تسجيل الدخول');
        expect(__('navigation.logout', [], 'ar'))->toBe('تسجيل الخروج');
        expect(__('navigation.dashboard', [], 'ar'))->toBe('لوحة التحكم');
    });
});

describe('Footer Translations', function () {
    test('English footer translations are loaded', function () {
        expect(__('footer.terms', [], 'en'))->toBe('Terms of Service');
        expect(__('footer.privacy', [], 'en'))->toBe('Privacy Policy');
        expect(__('footer.copyright', [], 'en'))->toBe('Libra Law Firm. All rights reserved.');
        expect(__('footer.address', [], 'en'))->toBe('American Building - Next to Al-Shini - 4th Floor');
        expect(__('footer.phone', [], 'en'))->toBe('+970 599 353 502');
    });

    test('Arabic footer translations are loaded', function () {
        expect(__('footer.terms', [], 'ar'))->toBe('شروط الخدمة');
        expect(__('footer.privacy', [], 'ar'))->toBe('سياسة الخصوصية');
        expect(__('footer.copyright', [], 'ar'))->toBe('مكتب ليبرا للمحاماة. جميع الحقوق محفوظة.');
        expect(__('footer.address', [], 'ar'))->toBe('العمارة الأمريكية - بجانب الشني - الطابق الرابع');
        expect(__('footer.phone', [], 'ar'))->toBe('+970 599 353 502');
    });
});

describe('Tailwind Colors', function () {
    test('app.css contains brand colors', function () {
        $css = file_get_contents(resource_path('css/app.css'));

        // New brand palette from Story 12.2 - Dark Charcoal & Warm Gold
        expect($css)->toContain('--color-primary: #2D3624');       // Dark Forest Green
        expect($css)->toContain('--color-primary-light: #3D4634'); // Lighter Forest Green
        expect($css)->toContain('--color-cta: #A68966');           // Warm Gold
        expect($css)->toContain('--color-cta-hover: #8A7555');     // Darker Gold
        expect($css)->toContain('--color-cta-light: #C4A882');     // Light Gold
        expect($css)->toContain('--color-active: #4A5D23');        // Olive Green
        expect($css)->toContain('--color-background: #F4F1EA');    // Warm Cream
        expect($css)->toContain('--color-text: #2D322A');          // Forest Green

        // Backward compatibility aliases (mapped to new colors)
        expect($css)->toContain('--color-navy: var(--color-primary)');
        expect($css)->toContain('--color-gold: var(--color-cta)');
    });
});

describe('Accessibility Features', function () {
    test('skip to content link is present', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('data-test="skip-to-content"', false)
            ->assertSee('href="#main-content"', false);
    });

    test('main content has proper id for skip link', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('id="main-content"', false);
    });

    test('mobile menu has proper ARIA attributes', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee('role="dialog"', false)
            ->assertSee('aria-modal="true"', false);
    });

    test('mobile menu button has aria-expanded attribute', function () {
        $this->get(route('home'))
            ->assertOk()
            ->assertSee(':aria-expanded="mobileMenuOpen"', false);
    });
});
