<?php

namespace Tests\Feature;

use App\Element\CreateElementDto;
use App\Element\ElementRepository;
use App\Set\CreateSetDto;
use App\Set\SetRepository;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SetsEndpointTest extends TestCase
{
    use RefreshDatabase;

    public function testReturnsAllSets(): void
    {
        $setRepository = app(SetRepository::class);
        $elementRepository = app(ElementRepository::class);
        $baderechSet = $setRepository->create(new CreateSetDto(
            name: 'Baderech HaAvodah',
            description: 'Baderech HaAvodah is a way of living',
            iconImageUrl: '/assets/baderech-haavodah-icon.png',
        ));
        $dailyLearningSet = $setRepository->create(new CreateSetDto(
            name: 'Daily Learning',
            description: 'Daily learning for steady growth',
            iconImageUrl: '/assets/daily-learning-icon.svg',
        ));
        $baderechRootElement = $elementRepository->create(
            new CreateElementDto(
                set: $baderechSet,
                title: $baderechSet->getName(),
                parentElement: null,
            )
        );

        $response = $this->getJson('/api/sets');

        $response->assertOk();
        $response->assertExactJson([
            'sets' => [
                [
                    'id' => $baderechSet->getId(),
                    'name' => $baderechSet->getName(),
                    'description' => $baderechSet->getDescription(),
                    'iconImageUrl' => $baderechSet->getIconImageUrl(),
                    'rootElementId' => $baderechRootElement->getId(),
                ],
                [
                    'id' => $dailyLearningSet->getId(),
                    'name' => $dailyLearningSet->getName(),
                    'description' => $dailyLearningSet->getDescription(),
                    'iconImageUrl' => $dailyLearningSet->getIconImageUrl(),
                    'rootElementId' => null,
                ],
            ],
        ]);
    }
}
