import * as Sequelize from 'sequelize';
import { Op } from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { Building, BuildingId } from './Building';
import type { Room, RoomId } from './Room';
import type { User, UserId } from './User';
export interface ApartmentAttributes {
	id: string;
	building_id: string;
	name: string;
	code: string;
	floor?: number | 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 ApartmentPk = 'id';
export type ApartmentId = Apartment[ApartmentPk];
export type ApartmentOptionalAttributes =
	| 'id'
	| 'floor'
	| 'status'
	| 'deleted_at'
	| 'deleted_by'
	| 'created_by'
	| 'updated_by'
	| 'created_at'
	| 'updated_at';
export type ApartmentCreationAttributes = Optional<ApartmentAttributes, ApartmentOptionalAttributes>;
export class Apartment extends Model<ApartmentAttributes> implements ApartmentAttributes {
	declare id: string;
	declare building_id: string;
	declare name: string;
	declare code: string;
	declare floor?: number | 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;
	// Apartment hasMany Room via apartment_id
	declare rooms: Room[];
	declare getRooms: Sequelize.HasManyGetAssociationsMixin<Room>;
	declare setRooms: Sequelize.HasManySetAssociationsMixin<Room, RoomId>;
	declare addRoom: Sequelize.HasManyAddAssociationMixin<Room, RoomId>;
	declare addRooms: Sequelize.HasManyAddAssociationsMixin<Room, RoomId>;
	declare createRoom: Sequelize.HasManyCreateAssociationMixin<Room>;
	declare removeRoom: Sequelize.HasManyRemoveAssociationMixin<Room, RoomId>;
	declare removeRooms: Sequelize.HasManyRemoveAssociationsMixin<Room, RoomId>;
	declare hasRoom: Sequelize.HasManyHasAssociationMixin<Room, RoomId>;
	declare hasRooms: Sequelize.HasManyHasAssociationsMixin<Room, RoomId>;
	declare countRooms: Sequelize.HasManyCountAssociationsMixin;
	// Apartment belongsTo Building via building_id
	declare building: Building;
	declare getBuilding: Sequelize.BelongsToGetAssociationMixin<Building>;
	declare setBuilding: Sequelize.BelongsToSetAssociationMixin<Building, BuildingId>;
	declare createBuilding: Sequelize.BelongsToCreateAssociationMixin<Building>;
	// Apartment 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>;
	// Apartment 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>;
	// Apartment 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 Apartment {
		return Apartment.init(
			{
				id: {
					type: DataTypes.UUID,
					allowNull: false,
					defaultValue: DataTypes.UUIDV4,
					primaryKey: true,
				},
				building_id: {
					type: DataTypes.UUID,
					allowNull: false,
					references: {
						model: 'buildings',
						key: 'id',
					},
				},
				name: {
					type: DataTypes.STRING(200),
					allowNull: false,
				},
				code: {
					type: DataTypes.STRING(50),
					allowNull: false,
				},
				floor: {
					type: DataTypes.INTEGER,
					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: 'apartments',
				schema: 'public',
				hasTrigger: true,
				timestamps: false,

				scopes: {
					default: {
						where: {
							deleted_at: null,
						},
					},
					withDeleted: {},
					onlyDeleted: {
						where: {
							deleted_at: { [Op.ne]: null },
						},
					},
				},
				indexes: [
					{
						name: 'apartments_pkey',
						unique: true,
						fields: [{ name: 'id' }],
					},
					{
						name: 'idx_apartments_building_id',
						fields: [{ name: 'building_id' }],
					},
					{
						name: 'idx_apartments_code',
						fields: [{ name: 'code' }],
					},
					{
						name: 'idx_apartments_deleted_at',
						fields: [{ name: 'deleted_at' }],
					},
					{
						name: 'idx_apartments_status',
						fields: [{ name: 'status' }],
					},
					{
						name: 'uq_apartments_code',
						unique: true,
						fields: [{ name: 'building_id' }, { name: 'code' }],
					},
				],
			},
		);
	}
}
