using Asp.Versioning;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetails;
using Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetailsBySearch;
using Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorList;
using Mirea.Api.Dto.Responses;
using Mirea.Api.Endpoint.Common.Attributes;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Mirea.Api.Endpoint.Controllers.V1;
[ApiVersion("1.0")]
[CacheMaxAge(true)]
public class ProfessorController(IMediator mediator) : BaseController
{
    /// 
    /// Retrieves a list of professors.
    /// 
    /// The page number for pagination (optional).
    /// The page size for pagination (optional).
    /// A list of professors.
    [HttpGet]
    [BadRequestResponse]
    public async Task>> Get([FromQuery] int? page, [FromQuery] int? pageSize)
    {
        var result = await mediator.Send(new GetProfessorListQuery()
        {
            Page = page,
            PageSize = pageSize
        });
        return Ok(result.Professors
            .Select(p => new ProfessorResponse()
            {
                Id = p.Id,
                Name = p.Name,
                AltName = p.AltName
            })
        );
    }
    /// 
    /// Retrieves detailed information about a specific professor.
    /// 
    /// The ID of the professor to retrieve.
    /// Detailed information about the professor.
    [HttpGet("{id:int}")]
    [BadRequestResponse]
    [NotFoundResponse]
    public async Task> GetDetails(int id)
    {
        var result = await mediator.Send(new GetProfessorInfoQuery()
        {
            Id = id
        });
        return Ok(new ProfessorResponse()
        {
            Id = result.Id,
            Name = result.Name,
            AltName = result.AltName
        });
    }
    /// 
    /// Retrieves detailed information about professors based on their name.
    /// 
    /// 
    /// This method searches for professors whose name matches the provided search term.
    /// 
    /// The name of the professor to search for. Must be at least 4 characters long.
    /// 
    /// A list of  objects containing the details of the matching professors.
    /// 
    [HttpGet("{name:required}")]
    [BadRequestResponse]
    [NotFoundResponse]
    public async Task>> GetDetails(string name)
    {
        if (string.IsNullOrEmpty(name) || name.Length < 4)
            return BadRequest($"The minimum number of characters is 4 (current: {name.Length}).");
        var result = await mediator.Send(new GetProfessorInfoSearchQuery()
        {
            Name = name
        });
        return Ok(result.Details.Select(x => new ProfessorResponse()
        {
            Id = x.Id,
            Name = x.Name,
            AltName = x.AltName
        }));
    }
}