import {
  Sequelize,
  DataTypes,
  Model,
  InferAttributes,
  InferCreationAttributes,
  CreationOptional,
} from "sequelize";

export class ElectionObserver extends Model<
  InferAttributes<ElectionObserver>,
  InferCreationAttributes<ElectionObserver>
> {
  declare id: CreationOptional<string>;
  declare electionId: string;
  declare userId: string;
  declare invitedAt: Date;
  declare revokedAt: Date | null;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
}

export function initElectionObserverModel(sequelize: Sequelize) {
  ElectionObserver.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      electionId: { type: DataTypes.UUID, allowNull: false },
      userId: { type: DataTypes.UUID, allowNull: false },
      invitedAt: { type: DataTypes.DATE, allowNull: false },
      revokedAt: { type: DataTypes.DATE, allowNull: true },
      createdAt: DataTypes.DATE,
      updatedAt: DataTypes.DATE,
    },
    {
      sequelize,
      modelName: "ElectionObserver",
      tableName: "ElectionObservers",
    }
  );
  return ElectionObserver;
}
