import * as Sequelize from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { Resident, ResidentId } from './Resident';
import type { User, UserId } from './User';
export interface ResidentEventAttributes {
	id: string;
	resident_id: string;
	event_type: 'created' | 'updated' | 'moved_in' | 'moved_out' | 'room_transfer' | 'status_changed' | 'deleted';
	payload?: object | null;
	correlation_id?: string | null;
	created_by?: string | null;
	created_at: Date;
}
export type ResidentEventPk = 'id';
export type ResidentEventId = ResidentEvent[ResidentEventPk];
export type ResidentEventOptionalAttributes = 'id' | 'payload' | 'correlation_id' | 'created_by' | 'created_at';
export type ResidentEventCreationAttributes = Optional<ResidentEventAttributes, ResidentEventOptionalAttributes>;
export class ResidentEvent extends Model<ResidentEventAttributes> implements ResidentEventAttributes {
	declare id: string;
	declare resident_id: string;
	declare event_type: 'created' | 'updated' | 'moved_in' | 'moved_out' | 'room_transfer' | 'status_changed' | 'deleted';
	declare payload?: object | null;
	declare correlation_id?: string | null;
	declare created_by?: string | null;
	declare created_at: Date;
	// ResidentEvent belongsTo Resident via resident_id
	declare resident: Resident;
	declare getResident: Sequelize.BelongsToGetAssociationMixin<Resident>;
	declare setResident: Sequelize.BelongsToSetAssociationMixin<Resident, ResidentId>;
	declare createResident: Sequelize.BelongsToCreateAssociationMixin<Resident>;
	// ResidentEvent belongsTo User via created_by
	declare created_by_user: User;
	declare getCreated_by_user: Sequelize.BelongsToGetAssociationMixin<User>;
	declare setCreated_by_user: Sequelize.BelongsToSetAssociationMixin<User, UserId>;
	declare createCreated_by_user: Sequelize.BelongsToCreateAssociationMixin<User>;
	static initModel(sequelize: Sequelize.Sequelize): typeof ResidentEvent {
		return ResidentEvent.init(
			{
				id: {
					type: DataTypes.UUID,
					allowNull: false,
					defaultValue: DataTypes.UUIDV4,
					primaryKey: true,
				},
				resident_id: {
					type: DataTypes.UUID,
					allowNull: false,
					references: {
						model: 'residents',
						key: 'id',
					},
				},
				event_type: {
					type: DataTypes.ENUM(
						'created',
						'updated',
						'moved_in',
						'moved_out',
						'room_transfer',
						'status_changed',
						'deleted',
					),
					allowNull: false,
				},
				payload: {
					type: DataTypes.JSONB,
					allowNull: true,
					comment: 'Standardized JSONB structure: {before, after, metadata, reason}. Enforce at application layer.',
				},
				correlation_id: {
					type: DataTypes.UUID,
					allowNull: true,
					comment: 'UUID to group related events (e.g., room_transfer sequence).',
				},
				created_by: {
					type: DataTypes.UUID,
					allowNull: true,
					references: {
						model: 'users',
						key: 'id',
					},
				},
				created_at: {
					type: DataTypes.DATE,
					allowNull: false,
					defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
				},
			},
			{
				sequelize,
				tableName: 'resident_events',
				schema: 'public',
				timestamps: false,
				indexes: [
					{
						name: 'idx_resident_events_correlation_id',
						fields: [{ name: 'correlation_id' }],
					},
					{
						name: 'idx_resident_events_created_at',
						fields: [{ name: 'created_at' }],
					},
					{
						name: 'idx_resident_events_resident_id',
						fields: [{ name: 'resident_id' }],
					},
					{
						name: 'idx_resident_events_type',
						fields: [{ name: 'event_type' }],
					},
					{
						name: 'resident_events_pkey',
						unique: true,
						fields: [{ name: 'id' }],
					},
				],
			},
		);
	}
}
