<?php

namespace Tests\Feature;

use App\Models\Page;
use App\Models\User;
use App\Models\Setting;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PageBuilderTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        \App\Models\Setting::clearCache();
        Setting::setValue('site_name', 'Sawdabazar', 'general');
    }

    /**
     * Test storefront blocks render correctly on active pages.
     */
    public function test_storefront_blocks_render_correctly()
    {
        $page = Page::create([
            'title' => 'About Us',
            'slug' => 'about-us',
            'is_active' => true,
            'meta_title' => 'About Sawdabazar',
            'meta_description' => 'Learn more about Sawdabazar platform.',
        ]);

        // 1. Add testimonials block
        $page->sections()->create([
            'block_type' => 'testimonials',
            'content' => [
                'title' => 'Customer Reviews',
                'testimonials' => [
                    [
                        'name' => 'Alice Smith',
                        'role' => 'VP of Tech',
                        'feedback' => 'Amazing products!',
                        'rating' => 5,
                        'avatar' => null,
                    ]
                ]
            ],
            'sort_order' => 0,
        ]);

        // 2. Add newsletter signup block
        $page->sections()->create([
            'block_type' => 'newsletter_signup',
            'content' => [
                'title' => 'Join the Club',
                'subtitle' => 'Get coupons weekly.',
                'placeholder_text' => 'Your email here...',
                'button_text' => 'Register Now',
            ],
            'sort_order' => 1,
        ]);

        // 3. Add video embed block
        $page->sections()->create([
            'block_type' => 'video_embed',
            'content' => [
                'title' => 'Our Journey',
                'video_url' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
                'aspect_ratio' => '16:9',
            ],
            'sort_order' => 2,
        ]);

        $response = $this->get('/pages/about-us');

        $response->assertStatus(200);

        // Assert SEO tags are set properly
        $response->assertSee('<title>About Sawdabazar | Sawdabazar</title>', false);
        $response->assertSee('<meta name="description" content="Learn more about Sawdabazar platform.">', false);

        // Assert Testimonials block is rendered
        $response->assertSee('Customer Reviews');
        $response->assertSee('Alice Smith');
        $response->assertSee('VP of Tech');
        $response->assertSee('Amazing products!');

        // Assert Newsletter Signup block is rendered
        $response->assertSee('Join the Club');
        $response->assertSee('Get coupons weekly.');
        $response->assertSee('Your email here...');
        $response->assertSee('Register Now');

        // Assert Video Embed block is rendered
        $response->assertSee('Our Journey');
        $response->assertSee('https://www.youtube.com/embed/dQw4w9WgXcQ');
    }

    /**
     * Test that inactive page returns 404 for guests and regular users.
     */
    public function test_inactive_page_returns_404_for_public()
    {
        $page = Page::create([
            'title' => 'Draft Page',
            'slug' => 'draft-page',
            'is_active' => false,
        ]);

        // Guest check
        $this->get('/pages/draft-page')->assertStatus(404);
        $this->get('/pages/draft-page?preview=1')->assertStatus(404);

        // Customer check
        $customer = User::create([
            'name' => 'Regular Customer',
            'email' => 'customer@example.com',
            'password' => bcrypt('password'),
            'role' => 'customer',
        ]);

        $this->actingAs($customer);
        $this->get('/pages/draft-page')->assertStatus(404);
        $this->get('/pages/draft-page?preview=1')->assertStatus(404);
    }

    /**
     * Test that admin and staff can preview inactive page.
     */
    public function test_admin_and_staff_can_preview_inactive_page()
    {
        $page = Page::create([
            'title' => 'Under Construction',
            'slug' => 'under-construction',
            'is_active' => false,
        ]);

        $admin = User::create([
            'name' => 'Admin User',
            'email' => 'admin@example.com',
            'password' => bcrypt('password'),
            'role' => 'admin',
        ]);

        $staff = User::create([
            'name' => 'Staff User',
            'email' => 'staff@example.com',
            'password' => bcrypt('password'),
            'role' => 'staff',
        ]);

        // Non-preview request as admin -> still 404 (needs preview parameter)
        $this->actingAs($admin);
        $this->get('/pages/under-construction')->assertStatus(404);

        // Preview request as admin -> 200 and has preview banner
        $response = $this->get('/pages/under-construction?preview=1');
        $response->assertStatus(200);
        $response->assertSee('You are currently previewing this page. Only administrators and staff can access this preview.');

        // Preview request as staff -> 200 and has preview banner
        $this->actingAs($staff);
        $response = $this->get('/pages/under-construction?preview=1');
        $response->assertStatus(200);
        $response->assertSee('You are currently previewing this page. Only administrators and staff can access this preview.');
    }
}
