"use strict";

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable("Rentals", {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      itemId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: "Items",
          key: "id",
        },
        onUpdate: "CASCADE",
        onDelete: "CASCADE",
      },
      renterId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: "Users",
          key: "id",
        },
        onUpdate: "CASCADE",
        onDelete: "CASCADE",
      },
      ownerId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: "Users",
          key: "id",
        },
        onUpdate: "CASCADE",
        onDelete: "CASCADE",
      },
      startDateTime: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      endDateTime: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      totalAmount: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false,
      },
      platformFee: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false,
      },
      payoutAmount: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false,
      },
      status: {
        type: Sequelize.ENUM(
          "pending",
          "confirmed",
          "declined",
          "active",
          "completed",
          "cancelled",
          "returned_late",
          "returned_late_and_damaged",
          "damaged",
          "lost"
        ),
        allowNull: false,
      },
      paymentStatus: {
        type: Sequelize.ENUM("pending", "paid", "refunded", "not_required"),
        allowNull: false,
      },
      payoutStatus: {
        type: Sequelize.ENUM("pending", "completed", "failed"),
        allowNull: true,
      },
      payoutProcessedAt: {
        type: Sequelize.DATE,
      },
      stripeTransferId: {
        type: Sequelize.STRING,
      },
      refundAmount: {
        type: Sequelize.DECIMAL(10, 2),
      },
      refundProcessedAt: {
        type: Sequelize.DATE,
      },
      refundReason: {
        type: Sequelize.TEXT,
      },
      stripeRefundId: {
        type: Sequelize.STRING,
      },
      cancelledBy: {
        type: Sequelize.ENUM("renter", "owner"),
      },
      cancelledAt: {
        type: Sequelize.DATE,
      },
      declineReason: {
        type: Sequelize.TEXT,
      },
      stripePaymentMethodId: {
        type: Sequelize.STRING,
      },
      stripePaymentIntentId: {
        type: Sequelize.STRING,
      },
      paymentMethodBrand: {
        type: Sequelize.STRING,
      },
      paymentMethodLast4: {
        type: Sequelize.STRING,
      },
      chargedAt: {
        type: Sequelize.DATE,
      },
      deliveryMethod: {
        type: Sequelize.ENUM("pickup", "delivery"),
        defaultValue: "pickup",
      },
      deliveryAddress: {
        type: Sequelize.TEXT,
      },
      intendedUse: {
        type: Sequelize.TEXT,
      },
      itemRating: {
        type: Sequelize.INTEGER,
      },
      itemReview: {
        type: Sequelize.TEXT,
      },
      itemReviewSubmittedAt: {
        type: Sequelize.DATE,
      },
      itemReviewVisible: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
      },
      renterRating: {
        type: Sequelize.INTEGER,
      },
      renterReview: {
        type: Sequelize.TEXT,
      },
      renterReviewSubmittedAt: {
        type: Sequelize.DATE,
      },
      renterReviewVisible: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
      },
      itemPrivateMessage: {
        type: Sequelize.TEXT,
      },
      renterPrivateMessage: {
        type: Sequelize.TEXT,
      },
      actualReturnDateTime: {
        type: Sequelize.DATE,
      },
      lateFees: {
        type: Sequelize.DECIMAL(10, 2),
        defaultValue: 0.0,
      },
      damageFees: {
        type: Sequelize.DECIMAL(10, 2),
        defaultValue: 0.0,
      },
      replacementFees: {
        type: Sequelize.DECIMAL(10, 2),
        defaultValue: 0.0,
      },
      itemLostReportedAt: {
        type: Sequelize.DATE,
      },
      damageAssessment: {
        type: Sequelize.JSONB,
        defaultValue: {},
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });

    // Add indexes
    await queryInterface.addIndex("Rentals", ["itemId"]);
    await queryInterface.addIndex("Rentals", ["renterId"]);
    await queryInterface.addIndex("Rentals", ["ownerId"]);
    await queryInterface.addIndex("Rentals", ["status"]);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable("Rentals");
  },
};
