<?php

namespace Tests\Feature;

use App\Models\Story;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class StoriesApiTest extends TestCase
{
    use RefreshDatabase;

    /**
     * Test Listing Active Stories API endpoint.
     */
    public function test_can_list_active_stories(): void
    {
        // 1. Create stories: 2 active, 1 inactive, 1 expired
        Story::create([
            'title' => 'Active Image Story',
            'media_path' => 'stories/img1.jpg',
            'media_type' => 'image',
            'button_text' => 'Click Me',
            'action_type' => 'url',
            'action_value' => 'https://example.com',
            'is_active' => true,
            'sort_order' => 10,
        ]);

        Story::create([
            'title' => 'Active Video Story',
            'media_path' => 'stories/vid1.mp4',
            'media_type' => 'video',
            'is_active' => true,
            'sort_order' => 5, // Should appear first due to sort_order
        ]);

        Story::create([
            'title' => 'Inactive Story',
            'media_path' => 'stories/img2.jpg',
            'media_type' => 'image',
            'is_active' => false,
            'sort_order' => 1,
        ]);

        Story::create([
            'title' => 'Expired Story',
            'media_path' => 'stories/img3.jpg',
            'media_type' => 'image',
            'is_active' => true,
            'expires_at' => now()->subMinutes(1),
            'sort_order' => 2,
        ]);

        // 2. Fetch via API
        $response = $this->getJson('/api/stories');

        // 3. Assertions
        $response->assertStatus(200);
        $response->assertJsonCount(2);

        // Verify ordering and structure
        $data = $response->json();
        $this->assertEquals('Active Video Story', $data[0]['title']);
        $this->assertEquals('video', $data[0]['media_type']);
        $this->assertEquals('Active Image Story', $data[1]['title']);
        $this->assertEquals('image', $data[1]['media_type']);
        $this->assertEquals('Click Me', $data[1]['button_text']);
        $this->assertEquals('url', $data[1]['action_type']);
        $this->assertEquals('https://example.com', $data[1]['action_value']);
    }
}

