using System.ComponentModel.DataAnnotations;
using Asp.Versioning;
using Baya.Application.Features.Geography.Queries.GetGeoTree;
using Baya.Application.Features.Geography.Queries.ListCities;
using Baya.Application.Features.Geography.Queries.ListDistricts;
using Baya.Application.Features.Geography.Queries.ListProvinces;
using Baya.Application.Models.Geography;
using Baya.WebFramework.Attributes;
using Baya.WebFramework.BaseController;
using Mediator;
using Microsoft.AspNetCore.Mvc;

namespace Baya.Web.Api.Controllers.V1;

[ApiVersion("1")]
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
[Display(Description = "Public geo lookups: the province → city → district cascading dropdowns")]
public sealed class GeoController(ISender sender) : BaseController
{
    [HttpGet("[action]")]
    [ProducesOkApiResponseType<IReadOnlyList<ProvinceDto>>]
    public async Task<IActionResult> Provinces(CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new ListProvincesQuery(), cancellationToken));

    [HttpGet("[action]")]
    [ProducesOkApiResponseType<IReadOnlyList<CityDto>>]
    public async Task<IActionResult> Cities([FromQuery(Name = "province_id")] long provinceId, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new ListCitiesQuery(provinceId), cancellationToken));

    [HttpGet("[action]")]
    [ProducesOkApiResponseType<IReadOnlyList<DistrictDto>>]
    public async Task<IActionResult> Districts([FromQuery(Name = "city_id")] long cityId, CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new ListDistrictsQuery(cityId), cancellationToken));

    [HttpGet("[action]")]
    [ProducesOkApiResponseType<IReadOnlyList<ProvinceTreeDto>>]
    public async Task<IActionResult> Tree(CancellationToken cancellationToken)
        => OperationResult(await sender.Send(new GetGeoTreeQuery(), cancellationToken));
}
