<?php

namespace Tests\Unit\DTOs;

use App\DTOs\ScrapedListingProduct;
use App\DTOs\ScrapedListingResult;
use Tests\TestCase;

class ScrapedListingResultTest extends TestCase
{
    public function test_scraped_listing_product_normalizes_payload_fields(): void
    {
        $product = ScrapedListingProduct::fromArray([
            'name' => 'Desk Lamp',
            'url' => 'https://shop.example.com/products/desk-lamp',
            'price' => '$39.00',
            'rating' => '4.8',
            'reviewCount' => '120',
            'imageUrl' => 'https://shop.example.com/lamp.jpg',
        ]);

        $this->assertNotNull($product);
        $this->assertSame('120', $product->reviewCount);
        $this->assertSame('https://shop.example.com/lamp.jpg', $product->imageUrl);
    }

    public function test_scraped_listing_result_builds_from_payload(): void
    {
        $result = ScrapedListingResult::fromPayload('https://shop.example.com/collections/lamps', [
            'url' => 'https://shop.example.com/collections/lamps',
            'products' => [
                ['name' => 'Desk Lamp', 'url' => 'https://shop.example.com/products/desk-lamp'],
                ['name' => '', 'url' => 'https://shop.example.com/products/invalid'],
            ],
            'error' => null,
        ], source: 'puppeteer');

        $this->assertTrue($result->succeeded());
        $this->assertCount(1, $result->products);
        $this->assertSame('puppeteer', $result->source);
        $this->assertNull($result->browserEngine);
    }
}
