using System.Net;

namespace Baya.Test.Api;

public class SearchApiTests(BayaApiFactory factory) : IClassFixture<BayaApiFactory>
{
    private const long TehranCityId = 101;

    [Fact]
    public async Task Search_Public_ReturnsPagedEnvelope()
    {
        var client = factory.CreateClient();

        // Public (no auth). Nothing seeded matches, so the search returns an empty, well-formed page.
        var response = await client.GetAsync($"/api/v1/search/nurses?service_category_id=1&city_id={TehranCityId}");

        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        var data = await AuthTestClient.ReadDataAsync(response);
        Assert.Equal(0, data.GetProperty("total").GetInt32());
        Assert.Equal(0, data.GetProperty("items").GetArrayLength());
    }

    [Fact]
    public async Task Search_MissingRequiredCategory_Returns400()
    {
        var client = factory.CreateClient();

        var response = await client.GetAsync($"/api/v1/search/nurses?service_category_id=0&city_id={TehranCityId}");

        Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
    }

    [Fact]
    public async Task Search_InvalidGender_Returns400()
    {
        var client = factory.CreateClient();

        var response = await client.GetAsync(
            $"/api/v1/search/nurses?service_category_id=1&city_id={TehranCityId}&nurse_gender=other");

        Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
    }

    [Fact]
    public async Task RebuildIndex_Unauthenticated_Returns401()
    {
        var client = factory.CreateClient();

        var response = await client.PostAsync("/api/v1/admin_search/rebuild_index", null);

        Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
    }
}
