<?php

namespace Tests\Feature\Front;

use App\Models\Lubricante;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class LubricantesEndpointTest extends TestCase
{
    use RefreshDatabase;

    private const ENDPOINT = '/endpoint/lubricantes';

    /**
     * El catálogo público expone sólo lubricantes activos y campos mínimos para
     * no filtrar metadatos administrativos ni forzar carga de datos innecesarios.
     */
    public function test_public_endpoint_returns_only_active_lubricantes(): void
    {
        $active = Lubricante::query()->create(['titulo' => 'Petronas Syntium', 'estado' => 1]);
        Lubricante::query()->create(['titulo' => 'Petronas Inactivo', 'estado' => 0]);

        DB::query()->from('images')->insert([
            'imageable_type' => Lubricante::class,
            'imageable_id' => $active->id,
            'name' => 'syntium',
            'ext' => 'webp',
            'active' => true,
            'key' => null,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $response = $this->getJson(self::ENDPOINT);

        $response->assertOk();
        $response->assertJsonCount(1);
        $response->assertJsonFragment([
            'titulo' => 'Petronas Syntium',
            'imagen' => asset('storage/uploads/lubricantes/syntium@large.webp'),
        ]);
        $response->assertJsonMissing(['titulo' => 'Petronas Inactivo']);
    }
}
