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

namespace TaxBaik.Web.Controllers;

[ApiController]
[Route("api/[controller]")]
[Authorize]
public class ContractController(ContractService service) : ControllerBase
{
    [HttpPost]
    public async Task<IActionResult> Create([FromBody] CreateContractRequest request)
    {
        try
        {
            var id = await service.CreateAsync(request.ClientId, request.ContractNumber, request.ServiceType,
                request.StartDate, request.MonthlyFee, request.TotalAmount);
            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 contracts = await service.GetAllAsync();
            return Ok(contracts);
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { error = "조회 실패", message = ex.Message });
        }
    }

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

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

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

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

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

    public record CreateContractRequest(
        int ClientId, string ContractNumber, string ServiceType, DateTime StartDate,
        decimal? MonthlyFee = null, decimal? TotalAmount = null);
}
