using System.Security.Claims; using HealthManager.Application.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace HealthManager.WebApi.Controllers; [ApiController] [Route("api/consultations")] [Authorize] public class ConsultationController(ConsultationService consultationService) : ControllerBase { private Guid UserId => Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!); private string Role => User.FindFirstValue(ClaimTypes.Role)!; [HttpGet("doctors")] public async Task GetDoctors([FromQuery] string? department) { var doctors = await consultationService.GetDoctorsAsync(department); return Ok(doctors.Select(d => new { d.Id, d.Name, d.Department, d.Title, d.Specialty, d.Introduction, d.AvatarUrl, })); } [HttpGet] public async Task GetConsultations() { var consultations = Role == "doctor" ? await consultationService.GetDoctorConsultationsAsync(UserId) : await consultationService.GetPatientConsultationsAsync(UserId); return Ok(consultations.Select(c => new { c.Id, c.PatientId, c.DoctorId, PatientName = c.Patient?.Name, DoctorName = c.Doctor?.Name, c.Subject, c.Status, c.StartedAt, c.ClosedAt, c.Summary, })); } [HttpPost] public async Task StartConsultation([FromBody] StartConsultationRequest request) { var consultation = await consultationService.StartAsync(UserId, request.DoctorId, request.Subject); return Ok(new { consultation.Id, consultation.PatientId, consultation.DoctorId, consultation.Status }); } [HttpGet("{id:guid}/messages")] public async Task GetMessages(Guid id) { var messages = await consultationService.GetMessagesAsync(id); return Ok(messages.Select(m => new { m.Id, m.SenderId, m.SenderRole, m.ContentType, m.Content, m.ImageUrl, m.IsRead, m.CreatedAt, SenderName = m.Sender?.Name, })); } [HttpPost("{id:guid}/messages")] public async Task SendMessage(Guid id, [FromBody] SendMessageRequest request) { var message = await consultationService.SendMessageAsync(id, UserId, Role, request.Content, request.ContentType, request.ImageUrl); return Ok(new { message.Id, message.SenderId, message.SenderRole, message.Content, message.CreatedAt }); } } public record StartConsultationRequest(Guid DoctorId, string Subject); public record SendMessageRequest(string Content, string ContentType = "text", string? ImageUrl = null);