using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TaxBaik.Application.Services;

namespace TaxBaik.Web.Controllers;

[ApiController]
[Route("api/[controller]")]
[Authorize]
public class ConsultingActivityController(ConsultingActivityService service) : ControllerBase
{
    [HttpPost]
    public async Task<IActionResult> Create([FromBody] CreateConsultingActivityRequest request)
    {
        try
        {
            var id = await service.CreateAsync(request.ClientId, request.ActivityType, request.ActivityDate,
                request.Description, request.ConsultantId, request.NextFollowupDate);
            return CreatedAtAction(nameof(GetById), new { id }, new { id });
        }
        catch (ValidationException ex)
        {
            return BadRequest(new { error = ex.Message });
        }
    }

    [HttpGet]
    public async Task<IActionResult> GetAll()
    {
        try
        {
            var activities = await service.GetAllAsync();
            return Ok(activities);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "조회 실패", message = ex.Message });
        }
    }

    [HttpGet("{id:int}")]
    public async Task<IActionResult> GetById(int id)
    {
        try
        {
            var activity = await service.GetByClientIdAsync(id);
            if (activity == null)
                return NotFound(new { error = "상담 활동을 찾을 수 없습니다." });
            return Ok(activity);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "조회 실패", message = ex.Message });
        }
    }

    [HttpGet("client/{clientId:int}")]
    public async Task<IActionResult> GetByClientId(int clientId)
    {
        try
        {
            var activities = await service.GetByClientIdAsync(clientId);
            return Ok(new { data = activities });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "조회 실패", message = ex.Message });
        }
    }

    [HttpGet("pending-followups")]
    public async Task<IActionResult> GetPendingFollowups()
    {
        try
        {
            var activities = await service.GetPendingFollowupsAsync();
            return Ok(new { data = activities });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "조회 실패", message = ex.Message });
        }
    }

    [HttpGet("consultant/{consultantId:int}")]
    public async Task<IActionResult> GetByConsultant(int consultantId, [FromQuery] int daysBack = 30)
    {
        try
        {
            var fromDate = DateTime.Today.AddDays(-daysBack);
            var activities = await service.GetConsultantActivityAsync(consultantId, fromDate);
            return Ok(new { data = activities, daysBack });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "조회 실패", message = ex.Message });
        }
    }

    [HttpPut("{id:int}")]
    public async Task<IActionResult> Update(int id, [FromBody] UpdateConsultingActivityRequest request)
    {
        try
        {
            await service.UpdateAsync(id, request.Outcome, request.NextFollowupDate);
            return Ok(new { message = "상담 활동이 수정되었습니다." });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "수정 실패", message = ex.Message });
        }
    }

    public record CreateConsultingActivityRequest(
        int ClientId, string ActivityType, DateTime ActivityDate, string Description,
        int? ConsultantId = null, DateTime? NextFollowupDate = null);

    public record UpdateConsultingActivityRequest(
        string? Outcome = null, DateTime? NextFollowupDate = null);
}
