"use strict";

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // ─── 1. SubscriptionPlans ───
    await queryInterface.createTable("SubscriptionPlans", {
      id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
      },
      name: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      description: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      price: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: false,
      },
      currency: {
        type: Sequelize.STRING(10),
        allowNull: false,
        defaultValue: "NGN",
      },
      billingInterval: {
        type: Sequelize.ENUM("monthly", "yearly"),
        allowNull: false,
        defaultValue: "monthly",
      },
      feeDiscountPercent: {
        type: Sequelize.DECIMAL(5, 2),
        defaultValue: 0.00,
      },
      transactionLimit: {
        type: Sequelize.INTEGER,
        allowNull: true,
      },
      benefits: {
        type: Sequelize.JSON,
        allowNull: true,
      },
      includedServices: {
        type: Sequelize.JSON,
        allowNull: true,
      },
      sortOrder: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
      },
      status: {
        type: Sequelize.ENUM("active", "inactive"),
        defaultValue: "active",
        allowNull: false,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
      },
    });

    // ─── 2. MerchantSubscriptions ───
    await queryInterface.createTable("MerchantSubscriptions", {
      id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
      },
      merchantId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: { model: "Merchants", key: "id" },
        onUpdate: "CASCADE",
        onDelete: "CASCADE",
      },
      planId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: { model: "SubscriptionPlans", key: "id" },
        onUpdate: "CASCADE",
        onDelete: "RESTRICT",
      },
      status: {
        type: Sequelize.ENUM("active", "cancelled", "expired", "pending"),
        defaultValue: "active",
        allowNull: false,
      },
      startDate: {
        type: Sequelize.DATEONLY,
        allowNull: false,
      },
      endDate: {
        type: Sequelize.DATEONLY,
        allowNull: false,
      },
      autoRenew: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
      },
      cancelledAt: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      previousPlanId: {
        type: Sequelize.UUID,
        allowNull: true,
        references: { model: "SubscriptionPlans", key: "id" },
        onUpdate: "CASCADE",
        onDelete: "SET NULL",
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
      },
    });

    // ─── 3. SubscriptionInvoices ───
    await queryInterface.createTable("SubscriptionInvoices", {
      id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
      },
      subscriptionId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: { model: "MerchantSubscriptions", key: "id" },
        onUpdate: "CASCADE",
        onDelete: "CASCADE",
      },
      merchantId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: { model: "Merchants", key: "id" },
        onUpdate: "CASCADE",
        onDelete: "CASCADE",
      },
      planId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: { model: "SubscriptionPlans", key: "id" },
        onUpdate: "CASCADE",
        onDelete: "RESTRICT",
      },
      amount: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: false,
      },
      currency: {
        type: Sequelize.STRING(10),
        allowNull: false,
        defaultValue: "NGN",
      },
      paymentMethod: {
        type: Sequelize.ENUM("wallet", "external"),
        defaultValue: "wallet",
      },
      paymentStatus: {
        type: Sequelize.ENUM("paid", "failed", "refunded"),
        defaultValue: "paid",
      },
      billingPeriodStart: {
        type: Sequelize.DATEONLY,
        allowNull: false,
      },
      billingPeriodEnd: {
        type: Sequelize.DATEONLY,
        allowNull: false,
      },
      paidAt: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
      },
    });

    // ─── Indexes ───
    await queryInterface.addIndex("MerchantSubscriptions", ["merchantId", "status"]);
    await queryInterface.addIndex("SubscriptionInvoices", ["merchantId"]);
    await queryInterface.addIndex("SubscriptionInvoices", ["subscriptionId"]);
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable("SubscriptionInvoices");
    await queryInterface.dropTable("MerchantSubscriptions");
    await queryInterface.dropTable("SubscriptionPlans");
  },
};
