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

export class BallotCastEvent extends Model<
  InferAttributes<BallotCastEvent>,
  InferCreationAttributes<BallotCastEvent>
> {
  declare id: CreationOptional<string>;
  declare electionId: string;
  declare voterRollId: string;
  declare castAt: Date;
  declare ipHash: string | null;
  declare deviceFingerprintHash: string | null;
  declare userAgent: string | null;
  declare verificationMethod: BallotVerificationMethod;
  declare digitalSignatureId: string | null;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
}

export function initBallotCastEventModel(sequelize: Sequelize) {
  BallotCastEvent.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      electionId: { type: DataTypes.UUID, allowNull: false },
      voterRollId: { type: DataTypes.UUID, allowNull: false },
      castAt: { type: DataTypes.DATE, allowNull: false },
      ipHash: { type: DataTypes.STRING(64), allowNull: true },
      deviceFingerprintHash: { type: DataTypes.STRING(64), allowNull: true },
      userAgent: { type: DataTypes.STRING(512), allowNull: true },
      verificationMethod: {
        type: DataTypes.ENUM("email", "sms_2fa", "email_sms"),
        allowNull: false,
        defaultValue: "email",
      },
      digitalSignatureId: { type: DataTypes.UUID, allowNull: true },
      createdAt: DataTypes.DATE,
      updatedAt: DataTypes.DATE,
    },
    { sequelize, modelName: "BallotCastEvent", tableName: "BallotCastEvents" }
  );
  return BallotCastEvent;
}
