<?php

use App\Models\Ecommerce\Channel\Channel;
use App\Models\Ecommerce\Product\Product\Product;

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

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

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

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

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

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

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

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

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

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

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

    // Attach product to channel
    $channel->products()->attach($product);

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->getJson("/api/v1/channels/{$channel->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 channel returns 404', function () {
    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->getJson('/api/v1/channels/999999');

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