const Payments = require('../models/payments.model');
const { requestSuccessful, requestFailed, sendMessageToAdmin } = require('../lib/code.bits');
const User = require('../models/user.model');
const Subscription = require('../models/subscription.model');
const Plan = require('../models/plan.model');
const { createSubscription } = require('../controllers/subscription.controller');

const recordPayments = async (subscriber,plan, amount, payment) => {
  try { 
    const payments = new Payments({
      ...plan,
      plan, 
      amount,
      planName: plan.planName,
      ...payment, 
      data: payment,
      subscriber,
    });
    payments.save();
    const durationUsedUp = plan.durationUsedUp + 1;
    const payed = await Plan.findOne({ _id: plan._id });
    payed.durationUsedUp = durationUsedUp
    await payed.save()
    
    return payments;
  } catch (error) {
    console.error(error);
    return null;
  }
};
exports.recordPayments = recordPayments;

exports.recordPayment = async (req, res) => {
  const { user: _id , payment, durationPaidFor  } =  req.body
  try { 

    const plan = await Plan.findOne({ subscriber: _id });
    const user = await User.findOne({ _id });
    
    await recordPayments(user, plan, plan.subscriptionAmount * durationPaidFor, payment)
    const durationCovered = plan.durationCovered + durationPaidFor;
    
    plan.durationCovered  = durationCovered;
    await plan.save();

    let subscription = await Subscription.findOne({ plan: plan._id, active: true });

    if(!subscription) {
        sendMessageToAdmin(`A new subscription has started under ${plan._id} plan`)

        plan.durationUsedUp = plan.durationUsedUp + 1;
        await plan.save();

        subscription =  await createSubscription(user,plan);
    }
    
    requestSuccessful(res, { success: true, subscription });
  } catch (error) {
    console.error(error)
    requestFailed(res, error);
  }
};
exports.paymentRecords = async (req, res) => {
  const { _id  } = req.user;
  const { limit, page } = req.query;
  const skip = parseInt(limit) * parseInt(page);
 
  try { 
    const payments = await Payments.find({ subscriber: _id })
    .populate('subscriber')
    .sort({ createdAt: -1 })
    .skip(parseInt(skip) || 0)
    .limit(parseInt(limit) || 30);

    requestSuccessful(res, {payment_history: payments});
  } catch (error) {
    requestFailed(res, error);
  }
};

exports.paystackWebhook = async (req, res) => {
  const event = req.body.event;
  const body = req.body.data;
  return requestSuccessful(
    res,
    { success: true },
    `Paystack confirm payment!`
  );
  // if (event == 'charge.success') {
  //   const planName = body['plan']['name'];
  //   const transactionId = body.reference;
  //   const user = await User.findOne({ email: body.customer.email });
  //   if (user) {
  //     try {
  //       const payments = new Payments({
  //         ...body['plan'],
  //         planName,
  //         transactionId,
  //         subscriber: user._id,
  //         data: req.body,
  //       });
  //       payments.save();

  //       return requestSuccessful(
  //         res,
  //         payments,
  //         `payments record saved successful ${body.customer.email} ----  Event ` +
  //           event
  //       );
  //     } catch (error) {
  //       return requestFailed(
  //         res,
  //         'paystackWebhook failure: ' + event + ' --- ' + error,
  //         500
  //       );
  //     }
  //   } else {
  //     return requestSuccessful(
  //       res,
  //       { success: true },
  //       `paystackWebhook Success ->> failure: user not found ${body.customer.email}`
  //     );
  //   }
  // } else {
  //   return requestSuccessful(
  //     res,
  //     { success: true },
  //     `paystack sent some other event ${event}`
  //   );
  // }
};



