import * as Sequelize from 'sequelize';
import { Op } from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { File, FileId } from './File';
import type { Resident, ResidentId } from './Resident';
import type { User, UserId } from './User';
export interface ResidentDocumentAttributes {
	id: string;
	resident_id: string;
	file_id: string;
	document_type:
		| 'id_card'
		| 'passport'
		| 'household_registration'
		| 'temporary_residence'
		| 'contract'
		| 'other'
		| 'cccd_front'
		| 'cccd_back'
		| 'portrait';
	document_name?: string | null;
	description?: string | null;
	is_primary?: boolean | null;
	issued_date?: string | null;
	expiry_date?: string | null;
	verified_at?: Date | null;
	verified_by?: string | null;
	created_by?: string | null;
	updated_by?: string | null;
	created_at: Date;
	updated_at: Date;
	deleted_at?: Date | null;
	deleted_by?: string | null;
	status: string;
}
export type ResidentDocumentPk = 'id';
export type ResidentDocumentId = ResidentDocument[ResidentDocumentPk];
export type ResidentDocumentOptionalAttributes =
	| 'id'
	| 'document_name'
	| 'description'
	| 'is_primary'
	| 'issued_date'
	| 'expiry_date'
	| 'verified_at'
	| 'verified_by'
	| 'created_by'
	| 'updated_by'
	| 'created_at'
	| 'updated_at'
	| 'deleted_at'
	| 'deleted_by'
	| 'status';
export type ResidentDocumentCreationAttributes = Optional<
	ResidentDocumentAttributes,
	ResidentDocumentOptionalAttributes
>;
export class ResidentDocument extends Model<ResidentDocumentAttributes> implements ResidentDocumentAttributes {
	declare id: string;
	declare resident_id: string;
	declare file_id: string;
	declare document_type:
		| 'id_card'
		| 'passport'
		| 'household_registration'
		| 'temporary_residence'
		| 'contract'
		| 'other'
		| 'cccd_front'
		| 'cccd_back'
		| 'portrait';
	declare document_name?: string | null;
	declare description?: string | null;
	declare is_primary?: boolean | null;
	declare issued_date?: string | null;
	declare expiry_date?: string | null;
	declare verified_at?: Date | null;
	declare verified_by?: string | null;
	declare created_by?: string | null;
	declare updated_by?: string | null;
	declare created_at: Date;
	declare updated_at: Date;
	declare deleted_at?: Date | null;
	declare deleted_by?: string | null;
	declare status: string;
	// ResidentDocument belongsTo File via file_id
	declare file: File;
	declare getFile: Sequelize.BelongsToGetAssociationMixin<File>;
	declare setFile: Sequelize.BelongsToSetAssociationMixin<File, FileId>;
	declare createFile: Sequelize.BelongsToCreateAssociationMixin<File>;
	// ResidentDocument belongsTo Resident via resident_id
	declare resident: Resident;
	declare getResident: Sequelize.BelongsToGetAssociationMixin<Resident>;
	declare setResident: Sequelize.BelongsToSetAssociationMixin<Resident, ResidentId>;
	declare createResident: Sequelize.BelongsToCreateAssociationMixin<Resident>;
	// ResidentDocument 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>;
	// ResidentDocument belongsTo User via deleted_by
	declare deleted_by_user: User;
	declare getDeleted_by_user: Sequelize.BelongsToGetAssociationMixin<User>;
	declare setDeleted_by_user: Sequelize.BelongsToSetAssociationMixin<User, UserId>;
	declare createDeleted_by_user: Sequelize.BelongsToCreateAssociationMixin<User>;
	// ResidentDocument belongsTo User via updated_by
	declare updated_by_user: User;
	declare getUpdated_by_user: Sequelize.BelongsToGetAssociationMixin<User>;
	declare setUpdated_by_user: Sequelize.BelongsToSetAssociationMixin<User, UserId>;
	declare createUpdated_by_user: Sequelize.BelongsToCreateAssociationMixin<User>;
	// ResidentDocument belongsTo User via verified_by
	declare verified_by_user: User;
	declare getVerified_by_user: Sequelize.BelongsToGetAssociationMixin<User>;
	declare setVerified_by_user: Sequelize.BelongsToSetAssociationMixin<User, UserId>;
	declare createVerified_by_user: Sequelize.BelongsToCreateAssociationMixin<User>;
	static initModel(sequelize: Sequelize.Sequelize): typeof ResidentDocument {
		return ResidentDocument.init(
			{
				id: {
					type: DataTypes.UUID,
					allowNull: false,
					defaultValue: DataTypes.UUIDV4,
					primaryKey: true,
				},
				resident_id: {
					type: DataTypes.UUID,
					allowNull: false,
					references: {
						model: 'residents',
						key: 'id',
					},
				},
				file_id: {
					type: DataTypes.UUID,
					allowNull: false,
					references: {
						model: 'files',
						key: 'id',
					},
				},
				document_type: {
					type: DataTypes.ENUM(
						'id_card',
						'passport',
						'household_registration',
						'temporary_residence',
						'contract',
						'other',
						'cccd_front',
						'cccd_back',
						'portrait',
					),
					allowNull: false,
				},
				document_name: {
					type: DataTypes.STRING(255),
					allowNull: true,
				},
				description: {
					type: DataTypes.TEXT,
					allowNull: true,
				},
				is_primary: {
					type: DataTypes.BOOLEAN,
					allowNull: true,
					defaultValue: false,
				},
				issued_date: {
					type: DataTypes.DATEONLY,
					allowNull: true,
				},
				expiry_date: {
					type: DataTypes.DATEONLY,
					allowNull: true,
				},
				verified_at: {
					type: DataTypes.DATE,
					allowNull: true,
				},
				verified_by: {
					type: DataTypes.UUID,
					allowNull: true,
					references: {
						model: 'users',
						key: 'id',
					},
				},
				created_by: {
					type: DataTypes.UUID,
					allowNull: true,
					references: {
						model: 'users',
						key: 'id',
					},
				},
				updated_by: {
					type: DataTypes.UUID,
					allowNull: true,
					references: {
						model: 'users',
						key: 'id',
					},
				},
				created_at: {
					type: DataTypes.DATE,
					allowNull: false,
					defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
				},
				updated_at: {
					type: DataTypes.DATE,
					allowNull: false,
					defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
				},
				deleted_at: {
					type: DataTypes.DATE,
					allowNull: true,
				},
				deleted_by: {
					type: DataTypes.UUID,
					allowNull: true,
					references: {
						model: 'users',
						key: 'id',
					},
				},
				status: {
					type: DataTypes.STRING(20),
					allowNull: false,
					defaultValue: 'pending',
				},
			},
			{
				sequelize,
				tableName: 'resident_documents',
				schema: 'public',
				hasTrigger: true,
				timestamps: false,

				scopes: {
					default: {
						where: {
							deleted_at: null,
						},
					},
					withDeleted: {},
					onlyDeleted: {
						where: {
							deleted_at: { [Op.ne]: null },
						},
					},
				},
				indexes: [
					{
						name: 'idx_resident_documents_deleted_at',
						fields: [{ name: 'deleted_at' }],
					},
					{
						name: 'idx_resident_documents_file_id',
						fields: [{ name: 'file_id' }],
					},
					{
						name: 'idx_resident_documents_resident_id',
						fields: [{ name: 'resident_id' }],
					},
					{
						name: 'idx_resident_documents_type',
						fields: [{ name: 'document_type' }],
					},
					{
						name: 'resident_documents_pkey',
						unique: true,
						fields: [{ name: 'id' }],
					},
					{
						name: 'uq_primary_document',
						unique: true,
						fields: [{ name: 'resident_id' }, { name: 'document_type' }],
					},
					{
						name: 'uq_resident_document_active',
						unique: true,
						fields: [{ name: 'resident_id' }, { name: 'file_id' }],
					},
				],
			},
		);
	}
}
