using Microsoft.AspNetCore.Mvc;
using BoredCareers.Controllers.Payment;
using BoredCareers.Services.DatabaseService;

namespace BoredCareers.Controllers {
    [ApiController]
    [Route("api/payment")]
    public class PaymentController : MistoxControllerBase {

        IPayment _paymentService;

        public PaymentController(DatabaseService db) : base(db) {
            if (IPayment._PaymentType == PaymentType.StripeIntent) {
                _paymentService = new StripeIntent(_databaseService);
            } else {
                // Fallback
                _paymentService = new StripeIntent(_databaseService);
            }
            // Add new payment plugins here
        }

        [HttpGet]
        public IActionResult GetPublicKey() {
            return Ok(IPayment._PublicKey);
        }

        [HttpPost]
        public async Task<IActionResult> paymentWebhook() {
            try {
                string body = await new StreamReader(Request.Body).ReadToEndAsync();
                _paymentService.ValidatePurchase(body, Request.Headers["Stripe-Signature"].ToString());
                return Ok();
            } catch (Exception ex) {
                return NotFound(ex.ToString());
            }
        }

    }

}

