using Microsoft.AspNetCore.Authorization;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MTWorkHR.Application.Filters;
using MTWorkHR.Application.Models;
using MTWorkHR.Application.Services;
using MTWorkHR.Application.Services.Interfaces;
using System.IO;

namespace MTWorkHR.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [AppAuthorize]
    public class ContractController : ControllerBase
    {
        private readonly IContractService _ContractService;
        private readonly IFileService _fileService;
      //  private readonly IContractPdfService _pdfService;

        public ContractController(IContractService UserContractService, IFileService fileService/*, IContractPdfService pdfService*/)
        {
            _ContractService = UserContractService;
            _fileService = fileService;
      //      _pdfService = pdfService;
        }
        [HttpGet("GetAll")]
        [ProducesResponseType(StatusCodes.Status200OK)]

        public async Task<ActionResult<List<ContractDto>>> GetAll([FromQuery] ContractPagingInputDto pagingInput)
        {
            return Ok(await _ContractService.GetAll(pagingInput));
        }
        [HttpGet("Get")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "Contract")]
        public async Task<ActionResult<ContractDto>> Get(long ContractId)
        {
            return Ok(await _ContractService.GetById(ContractId));
        }


        [HttpPost("Create")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "Contract.Create")]
        public async Task<ActionResult<ContractDto>> Create([FromBody] ContractDto input)
        {
            return await _ContractService.Create(input);
        }

        [HttpPost("Update")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "Contract.Update")]
        public async Task Update([FromBody] ContractDto input)
        {
            await _ContractService.Update(input);
        }

        [HttpDelete("Delete")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "Contract.Delete")]

        public async Task Delete([FromQuery] long id)
        {
            await _ContractService.Delete(id);
        }

        [HttpPost("ChangeStatus")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [AppAuthorize(Permissions = "Contract.Update")]
        public async Task<bool> ChangeStatus([FromBody] ContractStatusDto input)
        {
            return await _ContractService.ChangeStatus(input.ContractId, input.StatusId);
        }


        [HttpPost("generate")]
        public async Task <AttachmentResponseDto> GenerateContract([FromQuery] long contractId)
        {
            string outputPath = Path.Combine(Directory.GetCurrentDirectory(), "contract.pdf");
           var response = await _ContractService.GenerateContractPdf(outputPath, contractId);
         //   return File(stream, "application/pdf", "example.pdf");

            //string blobUrl = _pdfService.GenerateContractPdf(contractData);

            //// Return the URL to the client
            return (response);

            //return  PhysicalFile(outputPath, "application/pdf", "contract.pdf");
        }

        [HttpGet("generate-pdf")]
        public IActionResult GeneratePdf()
        {
            try
            {
                var stream = _ContractService.generatePdfTest();
                return File(stream, "application/pdf", "example.pdf");
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        //[HttpPost("CreateTaskAttachment")]
        //[ProducesResponseType(StatusCodes.Status200OK)]
        //[Consumes("multipart/form-data")]
        //public async Task<ActionResult<AttachmentDto>> CreateTaskAttachment([FromForm] AttachmentDto input)
        //{
        //    return await _ContractTaskAttachmentService.Create(input);
        //}

        //[HttpPost("CreateProjectAttachment")]
        //[ProducesResponseType(StatusCodes.Status200OK)]
        //[Consumes("multipart/form-data")]
        //public async Task<ActionResult<AttachmentDto>> CreateProjectAttachment([FromForm] AttachmentDto input)
        //{
        //    return await _ProjectStageAttachmentService.Create(input);
        //}

    }
}
