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

namespace TaxBaik.Web.Controllers;

[ApiController]
[Route("api/[controller]")]
[Authorize]
public class TaxFilingScheduleController(TaxFilingScheduleService service) : ControllerBase
{
    [HttpPost]
    public async Task<IActionResult> Create([FromBody] CreateTaxFilingScheduleRequest request)
    {
        try
        {
            var id = await service.CreateAsync(request.ClientId, request.FilingType, request.DueDate,
                request.FilingYear, request.AssignedTo);
            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 schedules = await service.GetAllAsync();
            return Ok(schedules);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "조회 실패", message = ex.Message });
        }
    }

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

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

    [HttpGet("upcoming")]
    public async Task<IActionResult> GetUpcomingDues([FromQuery] int daysAhead = 30)
    {
        try
        {
            var schedules = await service.GetUpcomingDuesAsync(daysAhead);
            return Ok(new { data = schedules, daysAhead });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "조회 실패", message = ex.Message });
        }
    }

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

    [HttpPut("{id:int}/complete")]
    public async Task<IActionResult> MarkCompleted(int id)
    {
        try
        {
            await service.MarkCompletedAsync(id);
            return Ok(new { message = "신고 일정이 완료되었습니다." });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "수정 실패", message = ex.Message });
        }
    }

    public record CreateTaxFilingScheduleRequest(
        int ClientId, string FilingType, DateTime DueDate, int FilingYear,
        int? AssignedTo = null);
}
