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

export class Ballot extends Model<
  InferAttributes<Ballot>,
  InferCreationAttributes<Ballot>
> {
  declare id: CreationOptional<string>;
  declare electionId: string;
  declare castToken: string;
  declare receiptCode: string;
  declare submittedAt: Date;
  /** Populated only for open ballots. No Sequelize association by design. */
  declare voterRollId: string | null;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
}

export function initBallotModel(sequelize: Sequelize) {
  Ballot.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      electionId: { type: DataTypes.UUID, allowNull: false },
      castToken: { type: DataTypes.UUID, allowNull: false },
      receiptCode: { type: DataTypes.STRING(16), allowNull: false, unique: true },
      submittedAt: { type: DataTypes.DATE, allowNull: false },
      voterRollId: { type: DataTypes.UUID, allowNull: true },
      createdAt: DataTypes.DATE,
      updatedAt: DataTypes.DATE,
    },
    { sequelize, modelName: "Ballot", tableName: "Ballots" }
  );
  return Ballot;
}
