<?php

namespace Tests\Feature;

use Tests\TestCase;

class QuickOrderSearchTest extends TestCase
{
    /**
     * Test search returns error when term is too short
     */
    public function test_search_requires_minimum_2_characters()
    {
        $response = $this->getJson('/quick-order/search?term=a');

        $response->assertStatus(400)
            ->assertJson([
                'status' => 'error',
            ])
            ->assertJsonFragment(['message' => 'Search term must be at least 2 characters']);
    }

    /**
     * Test search returns error when term is empty
     */
    public function test_search_requires_term()
    {
        $response = $this->getJson('/quick-order/search');

        $response->assertStatus(400)
            ->assertJson([
                'status' => 'error',
            ]);
    }

    /**
     * Test search returns ok status with valid term
     */
    public function test_search_returns_ok_with_valid_term()
    {
        $response = $this->getJson('/quick-order/search?term=test');

        $response->assertStatus(200)
            ->assertJson([
                'status' => 'ok',
            ])
            ->assertJsonStructure([
                'status',
                'products',
            ]);
    }

    /**
     * Test search response products have required fields
     */
    public function test_search_response_has_required_fields()
    {
        $response = $this->getJson('/quick-order/search?term=test');

        $response->assertStatus(200);

        $data = $response->json();

        if (count($data['products']) > 0) {
            $requiredFields = ['id', 'name', 'image', 'from_price', 'product_type', 'in_stock', 'url', 'sizes', 'requires_assessment', 'assessment_url'];
            foreach ($requiredFields as $field) {
                $this->assertArrayHasKey($field, $data['products'][0], "Product missing required field: {$field}");
            }
        }
    }

    /**
     * Test search returns max 24 results
     */
    public function test_search_returns_max_results()
    {
        $response = $this->getJson('/quick-order/search?term=tab');

        $response->assertStatus(200);

        $data = $response->json();

        $this->assertLessThanOrEqual(24, count($data['products']));
    }

    /**
     * Test search does not return hidden products
     */
    public function test_search_excludes_hidden_products()
    {
        $response = $this->getJson('/quick-order/search?term=test');

        $response->assertStatus(200);

        $data = $response->json();

        foreach ($data['products'] as $product) {
            $this->assertNotEmpty($product['name'], 'Product name should not be empty');
            $this->assertArrayHasKey('id', $product, 'Each product should have an id');
        }
    }

    /**
     * Test sizes in response have required structure
     */
    public function test_product_sizes_have_required_fields()
    {
        $response = $this->getJson('/quick-order/search?term=test');

        $response->assertStatus(200);

        $data = $response->json();

        foreach ($data['products'] as $product) {
            $this->assertIsArray($product['sizes']);
            foreach ($product['sizes'] as $size) {
                $this->assertArrayHasKey('id', $size);
                $this->assertArrayHasKey('size', $size);
                $this->assertArrayHasKey('price', $size);
                $this->assertArrayHasKey('sale_price', $size);
                $this->assertArrayHasKey('stock', $size);
            }
        }
    }

    /**
     * Test assessment fields are present
     */
    public function test_assessment_fields_present()
    {
        $response = $this->getJson('/quick-order/search?term=test');

        $response->assertStatus(200);

        $data = $response->json();

        foreach ($data['products'] as $product) {
            $this->assertArrayHasKey('requires_assessment', $product);
            $this->assertArrayHasKey('assessment_url', $product);
            $this->assertIsBool($product['requires_assessment']);
        }
    }
}
