import {
  Sequelize,
  DataTypes,
  Model,
  InferAttributes,
  InferCreationAttributes,
  CreationOptional,
} from "sequelize";
import type { InvoiceStatus } from "@/types";

export class Invoice extends Model<
  InferAttributes<Invoice>,
  InferCreationAttributes<Invoice>
> {
  declare id: CreationOptional<string>;
  declare workspaceId: string;
  declare amount: number;
  declare currency: string;
  declare status: InvoiceStatus;
  declare stripeInvoiceId: string | null;
  declare issuedAt: Date;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
}

export function initInvoiceModel(sequelize: Sequelize) {
  Invoice.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      workspaceId: { type: DataTypes.UUID, allowNull: false },
      amount: { type: DataTypes.INTEGER, allowNull: false },
      currency: {
        type: DataTypes.STRING(3),
        allowNull: false,
        defaultValue: "USD",
      },
      status: {
        type: DataTypes.ENUM("paid", "open", "failed"),
        allowNull: false,
        defaultValue: "open",
      },
      stripeInvoiceId: { type: DataTypes.STRING(255), allowNull: true },
      issuedAt: { type: DataTypes.DATE, allowNull: false },
      createdAt: DataTypes.DATE,
      updatedAt: DataTypes.DATE,
    },
    { sequelize, modelName: "Invoice", tableName: "Invoices" }
  );
  return Invoice;
}
