using Microsoft.AspNetCore.Mvc;
using Documents.Interfaces;
using Documents.Models;
using Domain.Dtos;

namespace phronCare.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class DocumentTestController : ControllerBase
    {
        private readonly IDocumentTemplateService _documentTemplateService;

        public DocumentTestController(IDocumentTemplateService documentTemplateService)
        {
            _documentTemplateService = documentTemplateService;
        }

        [HttpGet("test-quote")]
        public async Task<IActionResult> TestQuote()
        {
            // 👉 Creamos un QuoteDto de prueba
            var quote = new QuoteDto
            {
                Quotenumber = "Q-00000999",
                IssueDate = DateTime.Now,
                CustomerName = "LEANDRO HERNAN ROJAS",
                PatientName = "Paciente Test",
                ProfessionalName = "Dr. Demo",
                InstitutionName = "Clínica Test",
                Currency = "ARS",
                Total = 15000m,
                Items = new List<QuoteItemDto>
                {
                    new QuoteItemDto
                    {
                        Description = "Producto A",
                        Quantity = 2,
                        UnitPrice = 5000m,
                        Subtotal = 10000m,
                        TaxAmount = 0m,
                        Total = 10000m
                    },
                    new QuoteItemDto
                    {
                        Description = "Producto B",
                        Quantity = 1,
                        UnitPrice = 5000m,
                        Subtotal = 5000m,
                        TaxAmount = 0m,
                        Total = 5000m
                    }
                },
                Taxes = new List<QuoteTaxDto>(),
                Adjustments = new List<QuoteAdjustmentDto>(),
                Observations = "Esto es una prueba de emisión de presupuesto."
            };

            // 👉 Llamamos al motor de impresión
            var request = new DocumentGenerationRequest
            {
                DocumentType = DocumentType.Quote,
                Model = quote
            };

            var pdfBytes = await _documentTemplateService.GenerateDocumentAsync(request);

            // 👉 Devolvemos el PDF como file
            return File(pdfBytes, "application/pdf", $"Presupuesto_{quote.Quotenumber}.pdf");
        }
    }
}
