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

export class BallotSelection extends Model<
  InferAttributes<BallotSelection>,
  InferCreationAttributes<BallotSelection>
> {
  declare id: CreationOptional<string>;
  declare ballotId: string;
  declare electionPositionId: string;
  declare candidateId: string | null;
  declare rank: number | null;
  declare writeInText: string | null;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
}

export function initBallotSelectionModel(sequelize: Sequelize) {
  BallotSelection.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      ballotId: { type: DataTypes.UUID, allowNull: false },
      electionPositionId: { type: DataTypes.UUID, allowNull: false },
      candidateId: { type: DataTypes.UUID, allowNull: true },
      rank: { type: DataTypes.INTEGER, allowNull: true },
      writeInText: { type: DataTypes.STRING(255), allowNull: true },
      createdAt: DataTypes.DATE,
      updatedAt: DataTypes.DATE,
    },
    { sequelize, modelName: "BallotSelection", tableName: "BallotSelections" }
  );
  return BallotSelection;
}
