<?php

use App\Models\Ecommerce\Product\Brand\Brand;
use App\Models\Ecommerce\Product\Product\Product;

test('brands list can be retrieved', function () {
    Brand::create([
        'name' => 'Test Brand',
        'is_visible' => true,
        'sort_order' => 1,
    ]);

    Brand::create([
        'name' => 'Hidden Brand',
        'is_visible' => false,
        'sort_order' => 2,
    ]);

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->getJson('/api/v1/brands');

    $response->assertStatus(200)
        ->assertJsonStructure([
            'data',
        ]);

    $this->assertCount(1, $response->json('data'));
    $this->assertEquals('Test Brand', $response->json('data.0.name'));
});

test('brands list can be filtered by type', function () {
    Brand::create([
        'name' => 'Market Brand',
        'type' => 'market',
        'is_visible' => true,
    ]);

    Brand::create([
        'name' => 'Other Brand',
        'type' => 'other',
        'is_visible' => true,
    ]);

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->getJson('/api/v1/brands?type=market');

    $response->assertStatus(200);
    $this->assertCount(1, $response->json('data'));
    $this->assertEquals('Market Brand', $response->json('data.0.name'));
});

test('specific brand can be retrieved', function () {
    $brand = Brand::create([
        'name' => 'Test Brand',
        'is_visible' => true,
    ]);

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->getJson("/api/v1/brands/{$brand->id}");

    $response->assertStatus(200)
        ->assertJsonStructure([
            'data' => [
                'id',
                'name',
                'slug',
            ],
        ]);

    $this->assertEquals($brand->id, $response->json('data.id'));
    $this->assertEquals('Test Brand', $response->json('data.name'));
});

test('brand products can be retrieved', function () {
    $brand = Brand::create([
        'name' => 'Test Brand',
        'is_visible' => true,
    ]);

    Product::create([
        'name' => 'Test Product',
        'slug' => 'test-product',
        'brand_id' => $brand->id,
        'price_amount' => 100,
        'is_visible' => true,
        'stock' => 10,
    ]);

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->getJson("/api/v1/brands/{$brand->id}/products");

    $response->assertStatus(200)
        ->assertJsonStructure([
            'data',
            'pagination',
        ]);

    $this->assertCount(1, $response->json('data'));
    $this->assertEquals('Test Product', $response->json('data.0.name'));
});

test('non-existent brand returns 404', function () {
    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->getJson('/api/v1/brands/999999');

    $response->assertStatus(404);
});
