import * as Sequelize from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { User, UserId } from './User';
export interface UserAuthAttributes {
	id: string;
	user_id: string;
	password_hash: string;
	last_login_at?: Date | null;
	password_changed_at?: Date | null;
	login_attempts?: number | null;
	locked_until?: Date | null;
	twofa_secret?: string | null;
	twofa_enabled?: boolean | null;
	twofa_method?: string | null;
	created_at?: Date | null;
	updated_at?: Date | null;
	is_oauth_only?: boolean | null;
}
export type UserAuthPk = 'id';
export type UserAuthId = UserAuth[UserAuthPk];
export type UserAuthOptionalAttributes =
	| 'id'
	| 'last_login_at'
	| 'password_changed_at'
	| 'login_attempts'
	| 'locked_until'
	| 'twofa_secret'
	| 'twofa_enabled'
	| 'twofa_method'
	| 'created_at'
	| 'updated_at'
	| 'is_oauth_only';
export type UserAuthCreationAttributes = Optional<UserAuthAttributes, UserAuthOptionalAttributes>;
export class UserAuth extends Model<UserAuthAttributes> implements UserAuthAttributes {
	declare id: string;
	declare user_id: string;
	declare password_hash: string;
	declare last_login_at?: Date | null;
	declare password_changed_at?: Date | null;
	declare login_attempts?: number | null;
	declare locked_until?: Date | null;
	declare twofa_secret?: string | null;
	declare twofa_enabled?: boolean | null;
	declare twofa_method?: string | null;
	declare created_at?: Date | null;
	declare updated_at?: Date | null;
	declare is_oauth_only?: boolean | null;
	// UserAuth belongsTo User via user_id
	declare user: User;
	declare getUser: Sequelize.BelongsToGetAssociationMixin<User>;
	declare setUser: Sequelize.BelongsToSetAssociationMixin<User, UserId>;
	declare createUser: Sequelize.BelongsToCreateAssociationMixin<User>;
	static initModel(sequelize: Sequelize.Sequelize): typeof UserAuth {
		return UserAuth.init(
			{
				id: {
					type: DataTypes.UUID,
					allowNull: false,
					defaultValue: DataTypes.UUIDV4,
					primaryKey: true,
				},
				user_id: {
					type: DataTypes.UUID,
					allowNull: false,
					references: {
						model: 'users',
						key: 'id',
					},
					unique: 'user_auth_user_id_key',
				},
				password_hash: {
					type: DataTypes.STRING(255),
					allowNull: false,
				},
				last_login_at: {
					type: DataTypes.DATE,
					allowNull: true,
				},
				password_changed_at: {
					type: DataTypes.DATE,
					allowNull: true,
					defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
				},
				login_attempts: {
					type: DataTypes.INTEGER,
					allowNull: true,
					defaultValue: 0,
				},
				locked_until: {
					type: DataTypes.DATE,
					allowNull: true,
				},
				twofa_secret: {
					type: DataTypes.STRING(255),
					allowNull: true,
				},
				twofa_enabled: {
					type: DataTypes.BOOLEAN,
					allowNull: true,
					defaultValue: false,
				},
				twofa_method: {
					type: DataTypes.STRING(20),
					allowNull: true,
					defaultValue: 'totp',
				},
				created_at: {
					type: DataTypes.DATE,
					allowNull: true,
					defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
				},
				updated_at: {
					type: DataTypes.DATE,
					allowNull: true,
					defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
				},
				is_oauth_only: {
					type: DataTypes.BOOLEAN,
					allowNull: true,
					defaultValue: false,
					comment: 'Flag to identify OAuth-only accounts that cannot login via password (e.g., Zalo Mini App users)',
				},
			},
			{
				sequelize,
				tableName: 'user_auth',
				schema: 'public',
				hasTrigger: true,
				timestamps: false,
				indexes: [
					{
						name: 'idx_user_auth_is_oauth_only',
						fields: [{ name: 'is_oauth_only' }],
					},
					{
						name: 'idx_user_auth_locked_until',
						fields: [{ name: 'locked_until' }],
					},
					{
						name: 'idx_user_auth_login_attempts',
						fields: [{ name: 'login_attempts' }],
					},
					{
						name: 'idx_user_auth_twofa_enabled',
						fields: [{ name: 'twofa_enabled' }],
					},
					{
						name: 'idx_user_auth_user_id',
						fields: [{ name: 'user_id' }],
					},
					{
						name: 'user_auth_pkey',
						unique: true,
						fields: [{ name: 'id' }],
					},
					{
						name: 'user_auth_user_id_key',
						unique: true,
						fields: [{ name: 'user_id' }],
					},
				],
			},
		);
	}
}
