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

namespace TaxBaik.Web.Controllers;

[ApiController]
[Route("api/[controller]")]
[Authorize]
public class TaxProfileController(TaxProfileService taxProfileService) : ControllerBase
{
    [HttpPost]
    public async Task<IActionResult> Create([FromBody] CreateTaxProfileRequest request)
    {
        try
        {
            var id = await taxProfileService.CreateAsync(request.ClientId, request.BusinessType,
                request.BusinessRegistration, request.AccountingMethod, request.EstablishmentDate);
            return CreatedAtAction(nameof(GetByClientId), new { clientId = request.ClientId }, new { id });
        }
        catch (ValidationException ex)
        {
            return BadRequest(new { error = ex.Message });
        }
    }

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

    [HttpGet("client/{clientId:int}")]
    public async Task<IActionResult> GetByClientId(int clientId)
    {
        try
        {
            var profile = await taxProfileService.GetByClientIdAsync(clientId);
            if (profile == null)
                return NotFound(new { error = "세무 프로필을 찾을 수 없습니다." });
            return Ok(profile);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "조회 실패", message = ex.Message });
        }
    }

    [HttpGet("high-risk")]
    public async Task<IActionResult> GetHighRiskProfiles()
    {
        try
        {
            var profiles = await taxProfileService.GetHighRiskProfilesAsync();
            return Ok(new { data = profiles });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "조회 실패", message = ex.Message });
        }
    }

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

    [HttpPut("{id:int}")]
    public async Task<IActionResult> Update(int id, [FromBody] UpdateTaxProfileRequest request)
    {
        try
        {
            await taxProfileService.UpdateAsync(id, request.BusinessType, request.AccountingMethod,
                request.NextFilingDueDate, request.TaxRiskLevel);
            return Ok(new { message = "세무 프로필이 수정되었습니다." });
        }
        catch (ValidationException ex)
        {
            return BadRequest(new { error = ex.Message });
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "수정 실패", message = ex.Message });
        }
    }

    public record CreateTaxProfileRequest(
        int ClientId, string BusinessType, string? BusinessRegistration = null,
        string? AccountingMethod = null, DateTime? EstablishmentDate = null);

    public record UpdateTaxProfileRequest(
        string? BusinessType = null, string? AccountingMethod = null,
        DateTime? NextFilingDueDate = null, string TaxRiskLevel = "normal");
}
