const { DataTypes } = require("sequelize");
const sequelize = require("../config/database");

const Rental = sequelize.define("Rental", {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true,
  },
  itemId: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: "Items",
      key: "id",
    },
  },
  renterId: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: "Users",
      key: "id",
    },
  },
  ownerId: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: "Users",
      key: "id",
    },
  },
  startDateTime: {
    type: DataTypes.DATE,
    allowNull: false,
  },
  endDateTime: {
    type: DataTypes.DATE,
    allowNull: false,
  },
  totalAmount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
  },
  platformFee: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
  },
  payoutAmount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
  },
  status: {
    type: DataTypes.ENUM(
      "pending",
      "confirmed",
      "declined",
      "active",
      "completed",
      "cancelled",
      "returned_late",
      "returned_late_and_damaged",
      "damaged",
      "lost"
    ),
    allowNull: false,
  },
  paymentStatus: {
    type: DataTypes.ENUM("pending", "paid", "refunded", "not_required", "requires_action"),
    allowNull: false,
  },
  payoutStatus: {
    type: DataTypes.ENUM("pending", "completed", "failed", "on_hold"),
    allowNull: true,
  },
  payoutProcessedAt: {
    type: DataTypes.DATE,
  },
  stripeTransferId: {
    type: DataTypes.STRING,
  },
  // Bank deposit tracking fields (for tracking when Stripe deposits to owner's bank)
  bankDepositStatus: {
    type: DataTypes.ENUM("pending", "in_transit", "paid", "failed", "canceled"),
    allowNull: true,
  },
  bankDepositAt: {
    type: DataTypes.DATE,
  },
  stripePayoutId: {
    type: DataTypes.STRING,
  },
  bankDepositFailureCode: {
    type: DataTypes.STRING,
  },
  // Dispute tracking fields (for tracking Stripe payment disputes/chargebacks)
  // Stripe dispute statuses: https://docs.stripe.com/api/disputes/object#dispute_object-status
  stripeDisputeStatus: {
    type: DataTypes.ENUM(
      "needs_response",
      "under_review",
      "won",
      "lost",
      "warning_needs_response",
      "warning_under_review",
      "warning_closed"
    ),
    allowNull: true,
  },
  stripeDisputeId: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  stripeDisputeReason: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  stripeDisputeAmount: {
    type: DataTypes.INTEGER,
    allowNull: true,
  },
  stripeDisputeCreatedAt: {
    type: DataTypes.DATE,
    allowNull: true,
  },
  stripeDisputeEvidenceDueBy: {
    type: DataTypes.DATE,
    allowNull: true,
  },
  stripeDisputeClosedAt: {
    type: DataTypes.DATE,
    allowNull: true,
  },
  stripeDisputeLost: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
  },
  stripeDisputeLostAmount: {
    type: DataTypes.INTEGER,
    allowNull: true,
  },
  // Refund tracking fields
  refundAmount: {
    type: DataTypes.DECIMAL(10, 2),
  },
  refundProcessedAt: {
    type: DataTypes.DATE,
  },
  refundReason: {
    type: DataTypes.TEXT,
  },
  stripeRefundId: {
    type: DataTypes.STRING,
  },
  cancelledBy: {
    type: DataTypes.ENUM("renter", "owner"),
  },
  cancelledAt: {
    type: DataTypes.DATE,
  },
  declineReason: {
    type: DataTypes.TEXT,
  },
  stripePaymentMethodId: {
    type: DataTypes.STRING,
  },
  stripePaymentIntentId: {
    type: DataTypes.STRING,
  },
  paymentMethodBrand: {
    type: DataTypes.STRING,
  },
  paymentMethodLast4: {
    type: DataTypes.STRING,
  },
  chargedAt: {
    type: DataTypes.DATE,
  },
  // Payment failure notification tracking
  paymentFailedNotifiedAt: {
    type: DataTypes.DATE,
  },
  paymentFailedReason: {
    type: DataTypes.TEXT,
  },
  // Payment method update rate limiting
  paymentMethodUpdatedAt: {
    type: DataTypes.DATE,
  },
  paymentMethodUpdateCount: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
  },
  deliveryMethod: {
    type: DataTypes.ENUM("pickup", "delivery"),
    defaultValue: "pickup",
  },
  deliveryAddress: {
    type: DataTypes.TEXT,
  },
  intendedUse: {
    type: DataTypes.TEXT,
  },
  // Renter's review of the item (existing fields renamed for clarity)
  itemRating: {
    type: DataTypes.INTEGER,
    validate: {
      min: 1,
      max: 5,
    },
  },
  itemReview: {
    type: DataTypes.TEXT,
  },
  itemReviewSubmittedAt: {
    type: DataTypes.DATE,
  },
  itemReviewVisible: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
  },
  // Owner's review of the renter
  renterRating: {
    type: DataTypes.INTEGER,
    validate: {
      min: 1,
      max: 5,
    },
  },
  renterReview: {
    type: DataTypes.TEXT,
  },
  renterReviewSubmittedAt: {
    type: DataTypes.DATE,
  },
  renterReviewVisible: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
  },
  // Private messages (always visible to recipient)
  itemPrivateMessage: {
    type: DataTypes.TEXT,
  },
  renterPrivateMessage: {
    type: DataTypes.TEXT,
  },
  // Condition check and return handling fields
  actualReturnDateTime: {
    type: DataTypes.DATE,
  },
  lateFees: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0.0,
  },
  damageFees: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0.0,
  },
  replacementFees: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0.0,
  },
  itemLostReportedAt: {
    type: DataTypes.DATE,
  },
  damageAssessment: {
    type: DataTypes.JSONB,
    defaultValue: {},
  },
});

module.exports = Rental;
