<?php

use App\Models\Post;

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

test('home page displays English tagline when locale is English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Committed to Justice – Grounded in Dignity – Driven to Advocate');
});

test('home page displays Arabic tagline when locale is Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('ملتزمون بالعدالة – متجذرون بالكرامة – مدفوعون للدفاع');
});

test('home page displays English intro text when locale is English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Libra for Rights is a legal institution woven from the fabric of society');
});

test('home page displays Arabic intro text when locale is Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('ليبرا للحقوق مؤسسة قانونية منسوجة من نسيج المجتمع');
});

test('home page displays Book a Consultation button in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Book a Consultation');
});

test('home page displays Book a Consultation button in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('احجز استشارة');
});

test('home page displays Our Services button in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Our Services');
});

test('home page displays Our Services button in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('خدماتنا');
});

test('home page contains link to booking page', function () {
    $this->get('/')
        ->assertOk()
        ->assertSee('href="'.route('booking').'"', false);
});

test('home page contains link to services section', function () {
    $this->get('/')
        ->assertOk()
        ->assertSee('href="#services"', false);
});

test('home page contains services section with id', function () {
    $this->get('/')
        ->assertOk()
        ->assertSee('id="services"', false);
});

// About Section Tests

test('home page contains about section with id', function () {
    $this->get('/')
        ->assertOk()
        ->assertSee('id="about"', false);
});

test('home page displays about section title in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Meet the Founder');
});

test('home page displays about section title in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('تعرف على المؤسِّسة');
});

test('home page displays lawyer name in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Huda Armouche');
});

test('home page displays lawyer name in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('هدى عرموش');
});

test('home page displays lawyer title in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Attorney at Law');
});

test('home page displays lawyer title in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('محامية');
});

test('home page displays lawyer bio in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Huda Armouche founded Libra for Rights');
});

test('home page displays lawyer bio in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('أسست هدى عرموش ليبرا للحقوق');
});

test('home page displays lawyer photo', function () {
    $this->get('/')
        ->assertOk()
        ->assertSee('images/huda-armouche.jpg', false);
});

test('home page lawyer photo has alt text for accessibility', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('alt="Huda Armouche"', false);
});

// Services Section Tests

test('home page displays services section title in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Our Services');
});

test('home page displays services section title in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('خدماتنا');
});

test('home page displays services section subtitle in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Comprehensive legal solutions for individuals and businesses');
});

test('home page displays services section subtitle in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('حلول قانونية شاملة للأفراد والشركات');
});

test('home page displays legal consultations service in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Legal Consultations')
        ->assertSee('Expert legal advice tailored to your specific situation and needs.');
});

test('home page displays legal consultations service in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('الاستشارات القانونية')
        ->assertSee('استشارات قانونية متخصصة مصممة وفقاً لوضعك واحتياجاتك.');
});

test('home page displays court representation service in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Court Representation')
        ->assertSee('Professional representation before courts and judicial bodies at all stages.');
});

test('home page displays court representation service in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('التمثيل أمام المحاكم')
        ->assertSee('تمثيل احترافي أمام المحاكم والهيئات القضائية في جميع المراحل.');
});

test('home page displays litigation management service in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Litigation Management')
        ->assertSee('Complete case management from pleadings and memoranda to appeals.');
});

test('home page displays litigation management service in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('إدارة الدعاوى')
        ->assertSee('إدارة كاملة للقضايا من اللوائح والمذكرات إلى الاستئنافات.');
});

test('home page displays contract services in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Contract Services')
        ->assertSee('Drafting, review, and legal compliance for all your contractual needs.');
});

test('home page displays contract services in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('خدمات العقود')
        ->assertSee('صياغة ومراجعة العقود والامتثال القانوني لجميع احتياجاتكم التعاقدية.');
});

// Values Section Tests

test('home page contains values section with id', function () {
    $this->get('/')
        ->assertOk()
        ->assertSee('id="values"', false);
});

test('home page displays values section title in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Our Values');
});

test('home page displays values section title in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('قيمنا');
});

test('home page displays values section subtitle in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('These start in the field and return to the people');
});

test('home page displays values section subtitle in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('تبدأ من الميدان وتعود إلى الناس');
});

test('home page displays integrity value in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Integrity');
});

test('home page displays integrity value in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('النزاهة');
});

test('home page displays justice value in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Justice');
});

test('home page displays justice value in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('العدالة');
});

test('home page displays knowledge value in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Knowledge');
});

test('home page displays knowledge value in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('المعرفة');
});

test('home page displays womens empowerment value in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Women&#039;s Empowerment', false);
});

test('home page displays womens empowerment value in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('تمكين المرأة');
});

test('home page displays professionalism value in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Professionalism');
});

test('home page displays professionalism value in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('الاحترافية');
});

test('home page displays social innovation value in English', function () {
    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Social Innovation');
});

test('home page displays social innovation value in Arabic', function () {
    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('الابتكار الاجتماعي');
});

// Latest Posts Section Tests

test('home page does not display posts section when no published posts exist', function () {
    // No posts created
    $this->get('/')
        ->assertOk()
        ->assertDontSee('id="posts"', false);
});

test('home page displays posts section when published posts exist', function () {
    Post::factory()->published()->count(3)->create();

    $this->get('/')
        ->assertOk()
        ->assertSee('id="posts"', false);
});

test('home page displays posts section title in English', function () {
    Post::factory()->published()->create();

    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Latest Articles');
});

test('home page displays posts section title in Arabic', function () {
    Post::factory()->published()->create();

    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('أحدث المقالات');
});

test('home page displays maximum 3 latest posts', function () {
    // Create 5 published posts
    Post::factory()->published()->count(5)->create();

    $response = $this->get('/');
    $response->assertOk();

    // The section should exist
    $response->assertSee('id="posts"', false);

    // Count the number of post cards (articles with specific class)
    $content = $response->getContent();
    $postCardCount = substr_count($content, 'class="bg-card p-6 rounded-lg shadow-card hover:shadow-card-hover transition-shadow"');
    expect($postCardCount)->toBe(3);
});

test('home page displays post titles', function () {
    $post = Post::factory()->published()->create([
        'title' => ['en' => 'Test English Title', 'ar' => 'عنوان اختبار'],
    ]);

    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Test English Title');
});

test('home page displays post titles in Arabic', function () {
    $post = Post::factory()->published()->create([
        'title' => ['en' => 'Test English Title', 'ar' => 'عنوان اختبار عربي'],
    ]);

    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('عنوان اختبار عربي');
});

test('home page displays post excerpt', function () {
    $post = Post::factory()->published()->create([
        'body' => ['en' => 'This is a test body content for the post that should be truncated.', 'ar' => 'هذا محتوى اختبار'],
    ]);

    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('This is a test body content');
});

test('home page displays read more link in English', function () {
    Post::factory()->published()->create();

    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Read More');
});

test('home page displays read more link in Arabic', function () {
    Post::factory()->published()->create();

    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('اقرأ المزيد');
});

test('home page displays view all posts link in English', function () {
    Post::factory()->published()->create();

    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('View All Articles');
});

test('home page displays view all posts link in Arabic', function () {
    Post::factory()->published()->create();

    $this->withSession(['locale' => 'ar'])
        ->get('/')
        ->assertOk()
        ->assertSee('عرض جميع المقالات');
});

test('home page view all posts links to posts index', function () {
    Post::factory()->published()->create();

    $this->get('/')
        ->assertOk()
        ->assertSee('href="'.route('posts.index').'"', false);
});

test('home page post title links to post show page', function () {
    $post = Post::factory()->published()->create();

    $this->get('/')
        ->assertOk()
        ->assertSee('href="'.route('posts.show', $post).'"', false);
});

test('home page only shows published posts not drafts', function () {
    $publishedPost = Post::factory()->published()->create([
        'title' => ['en' => 'Published Post Title', 'ar' => 'منشور'],
    ]);

    $draftPost = Post::factory()->draft()->create([
        'title' => ['en' => 'Draft Post Title', 'ar' => 'مسودة'],
    ]);

    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('Published Post Title')
        ->assertDontSee('Draft Post Title');
});

test('home page displays posts in order of latest published first', function () {
    $oldPost = Post::factory()->published()->create([
        'title' => ['en' => 'Old Post', 'ar' => 'قديم'],
        'published_at' => now()->subDays(10),
    ]);

    $newPost = Post::factory()->published()->create([
        'title' => ['en' => 'New Post', 'ar' => 'جديد'],
        'published_at' => now()->subDay(),
    ]);

    $response = $this->withSession(['locale' => 'en'])->get('/');
    $response->assertOk();

    $content = $response->getContent();
    $newPostPosition = strpos($content, 'New Post');
    $oldPostPosition = strpos($content, 'Old Post');

    // New post should appear before old post
    expect($newPostPosition)->toBeLessThan($oldPostPosition);
});

test('home page displays post publication date', function () {
    $post = Post::factory()->published()->create([
        'published_at' => now()->setDate(2026, 1, 15),
    ]);

    $this->withSession(['locale' => 'en'])
        ->get('/')
        ->assertOk()
        ->assertSee('15 January 2026');
});
