const Sequelize = require("sequelize");
const sequelizeConnect = require("../connection/db");
const { API_ASSET_URL } = require('../marketplace.json');
const PaymentMethods = sequelizeConnect.define(
  "payment_methods",
  {
    id: {
      type: Sequelize.BIGINT,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true,
    },
    payment_method: {
      type: Sequelize.STRING(500),
      allowNull: false,
    },
    code: {
      type: Sequelize.STRING(500),
      allowNull: false,
    },
    method: {
      type: Sequelize.STRING(500),
      allowNull: true,
    },
    image_link: {
      type: Sequelize.STRING(500),
      allowNull: false,
    },
    status: {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: true,
    },
  },
  {
    tableName: "payment_methods",
    timestamps: true,
    hooks: {
      afterSync: async (options) => {
        const count = await PaymentMethods.count();
        if (count === 0) {
          await PaymentMethods.bulkCreate([
            {
              payment_method: "BUILDCREDIT",
              code: "Build Credit",
              image_link: `${API_ASSET_URL}buildcredit.png`,
              status: true,
            },
            {
              payment_method: "SELLER CREDIT",
              code: "Hardware Credit",
              image_link: `${API_ASSET_URL}sellercredit.png`,
              status: true,
            },
            {
              payment_method: "Yield",
              code: "Yield",
              image_link: `${API_ASSET_URL}yield.png`,
              status: true,
            },
            {
              payment_method: "BANK DEPOSIT",
              code: "Bank Transfer",
              image_link: `${API_ASSET_URL}bankdeposit.png`,
              status: true,
            },
            {
              payment_method: "MASTERCARD/VISA",
              code: "creditcard",
              method: "Card",
              image_link: `${API_ASSET_URL}card.png`,
              status: true,
            },
            {
              payment_method: "BPI",
              code: "bpi_online",
              method: "onlinebanktransfer",
              image_link: `${API_ASSET_URL}bpi.png`,
              status: true,
            },
            {
              payment_method: "BDO",
              code: "br_bdo_ph",
              method: "onlinebanktransfer",
              image_link: `${API_ASSET_URL}bdo.png`,
              status: true,
            },
            {
              payment_method: "UBP",
              code: "ubp_online",
              method: "onlinebanktransfer",
              image_link: `${API_ASSET_URL}ubp.png`,
              status: true,
            },
            {
              payment_method: "GCASH",
              code: "gc",
              method: "wallet",
              image_link: `${API_ASSET_URL}gcash.jpg`,
              status: true,
            },
            {
              payment_method: "MLHUILLIER",
              code: "mlhuillier_ph",
              method: "nonbank_otc",
              image_link: `${API_ASSET_URL}mlhuillier.png`,
              status: true,
            },
            {
              payment_method: "CEBUANA",
              code: "cebuana_ph",
              method: "nonbank_otc",
              image_link: `${API_ASSET_URL}cebuana.png`,
              status: true,
            },
            {
              payment_method: "BAYAD CENTER",
              code: "bayadcenter_ph",
              method: "nonbank_otc",
              image_link: `${API_ASSET_URL}bayadcenter.png`,
              status: true,
            },
            {
              payment_method: "PALAWAN EXPRESS",
              code: "palawan_express_ph",
              method: "nonbank_otc",
              image_link: `${API_ASSET_URL}palawan.jpg`,
              status: true,
            },
          ]);
          console.log("Default payment methods created");
        }
      },
    },
  }
);

module.exports = PaymentMethods;
