import * as Sequelize from 'sequelize';
import { Op } from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { Apartment, ApartmentId } from './Apartment';
import type { User, UserId } from './User';
export interface BuildingAttributes {
	id: string;
	name: string;
	code: string;
	address?: string | null;
	description?: string | null;
	status: 'active' | 'inactive';
	deleted_at?: Date | null;
	deleted_by?: string | null;
	created_by?: string | null;
	updated_by?: string | null;
	created_at: Date;
	updated_at: Date;
}
export type BuildingPk = 'id';
export type BuildingId = Building[BuildingPk];
export type BuildingOptionalAttributes =
	| 'id'
	| 'address'
	| 'description'
	| 'status'
	| 'deleted_at'
	| 'deleted_by'
	| 'created_by'
	| 'updated_by'
	| 'created_at'
	| 'updated_at';
export type BuildingCreationAttributes = Optional<BuildingAttributes, BuildingOptionalAttributes>;
export class Building extends Model<BuildingAttributes> implements BuildingAttributes {
	declare id: string;
	declare name: string;
	declare code: string;
	declare address?: string | null;
	declare description?: string | null;
	declare status: 'active' | 'inactive';
	declare deleted_at?: Date | null;
	declare deleted_by?: string | null;
	declare created_by?: string | null;
	declare updated_by?: string | null;
	declare created_at: Date;
	declare updated_at: Date;
	// Building hasMany Apartment via building_id
	declare apartments: Apartment[];
	declare getApartments: Sequelize.HasManyGetAssociationsMixin<Apartment>;
	declare setApartments: Sequelize.HasManySetAssociationsMixin<Apartment, ApartmentId>;
	declare addApartment: Sequelize.HasManyAddAssociationMixin<Apartment, ApartmentId>;
	declare addApartments: Sequelize.HasManyAddAssociationsMixin<Apartment, ApartmentId>;
	declare createApartment: Sequelize.HasManyCreateAssociationMixin<Apartment>;
	declare removeApartment: Sequelize.HasManyRemoveAssociationMixin<Apartment, ApartmentId>;
	declare removeApartments: Sequelize.HasManyRemoveAssociationsMixin<Apartment, ApartmentId>;
	declare hasApartment: Sequelize.HasManyHasAssociationMixin<Apartment, ApartmentId>;
	declare hasApartments: Sequelize.HasManyHasAssociationsMixin<Apartment, ApartmentId>;
	declare countApartments: Sequelize.HasManyCountAssociationsMixin;
	// Building 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>;
	// Building 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>;
	// Building 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>;
	static initModel(sequelize: Sequelize.Sequelize): typeof Building {
		return Building.init(
			{
				id: {
					type: DataTypes.UUID,
					allowNull: false,
					defaultValue: DataTypes.UUIDV4,
					primaryKey: true,
				},
				name: {
					type: DataTypes.STRING(200),
					allowNull: false,
				},
				code: {
					type: DataTypes.STRING(50),
					allowNull: false,
				},
				address: {
					type: DataTypes.TEXT,
					allowNull: true,
				},
				description: {
					type: DataTypes.TEXT,
					allowNull: true,
				},
				status: {
					type: DataTypes.ENUM('active', 'inactive'),
					allowNull: false,
					defaultValue: 'active',
				},
				deleted_at: {
					type: DataTypes.DATE,
					allowNull: true,
				},
				deleted_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'),
				},
			},
			{
				sequelize,
				tableName: 'buildings',
				schema: 'public',
				hasTrigger: true,
				timestamps: false,

				scopes: {
					default: {
						where: {
							deleted_at: null,
						},
					},
					withDeleted: {},
					onlyDeleted: {
						where: {
							deleted_at: { [Op.ne]: null },
						},
					},
				},
				indexes: [
					{
						name: 'buildings_pkey',
						unique: true,
						fields: [{ name: 'id' }],
					},
					{
						name: 'idx_buildings_code',
						fields: [{ name: 'code' }],
					},
					{
						name: 'idx_buildings_deleted_at',
						fields: [{ name: 'deleted_at' }],
					},
					{
						name: 'idx_buildings_status',
						fields: [{ name: 'status' }],
					},
					{
						name: 'uq_buildings_code',
						unique: true,
						fields: [{ name: 'code' }],
					},
				],
			},
		);
	}
}
