import * as Sequelize from 'sequelize';
import { Op } from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { ContractFile, ContractFileId } from './ContractFile';
import type { ContractType, ContractTypeId } from './ContractType';
import type { Resident, ResidentId } from './Resident';
import type { User, UserId } from './User';
export interface ContractAttributes {
	id: string;
	resident_id: string;
	contract_number: string;
	contract_name?: string | null;
	description?: string | null;
	rent_amount?: number | null;
	deposit_amount?: number | null;
	payment_day?: number | null;
	signed_date?: string | null;
	expiry_date?: string | null;
	created_by?: string | null;
	updated_by?: string | null;
	deleted_at?: Date | null;
	deleted_by?: string | null;
	created_at: Date;
	updated_at: Date;
	contract_type_id: string;
}
export type ContractPk = 'id';
export type ContractId = Contract[ContractPk];
export type ContractOptionalAttributes =
	| 'id'
	| 'contract_name'
	| 'description'
	| 'rent_amount'
	| 'deposit_amount'
	| 'payment_day'
	| 'signed_date'
	| 'expiry_date'
	| 'created_by'
	| 'updated_by'
	| 'deleted_at'
	| 'deleted_by'
	| 'created_at'
	| 'updated_at';
export type ContractCreationAttributes = Optional<ContractAttributes, ContractOptionalAttributes>;
export class Contract extends Model<ContractAttributes> implements ContractAttributes {
	declare id: string;
	declare resident_id: string;
	declare contract_number: string;
	declare contract_name?: string | null;
	declare description?: string | null;
	declare rent_amount?: number | null;
	declare deposit_amount?: number | null;
	declare payment_day?: number | null;
	declare signed_date?: string | null;
	declare expiry_date?: string | null;
	declare created_by?: string | null;
	declare updated_by?: string | null;
	declare deleted_at?: Date | null;
	declare deleted_by?: string | null;
	declare created_at: Date;
	declare updated_at: Date;
	declare contract_type_id: string;
	// Contract belongsTo ContractType via contract_type_id
	declare contract_type: ContractType;
	declare getContract_type: Sequelize.BelongsToGetAssociationMixin<ContractType>;
	declare setContract_type: Sequelize.BelongsToSetAssociationMixin<ContractType, ContractTypeId>;
	declare createContract_type: Sequelize.BelongsToCreateAssociationMixin<ContractType>;
	// Contract hasMany ContractFile via contract_id
	declare contract_files: ContractFile[];
	declare getContract_files: Sequelize.HasManyGetAssociationsMixin<ContractFile>;
	declare setContract_files: Sequelize.HasManySetAssociationsMixin<ContractFile, ContractFileId>;
	declare addContract_file: Sequelize.HasManyAddAssociationMixin<ContractFile, ContractFileId>;
	declare addContract_files: Sequelize.HasManyAddAssociationsMixin<ContractFile, ContractFileId>;
	declare createContract_file: Sequelize.HasManyCreateAssociationMixin<ContractFile>;
	declare removeContract_file: Sequelize.HasManyRemoveAssociationMixin<ContractFile, ContractFileId>;
	declare removeContract_files: Sequelize.HasManyRemoveAssociationsMixin<ContractFile, ContractFileId>;
	declare hasContract_file: Sequelize.HasManyHasAssociationMixin<ContractFile, ContractFileId>;
	declare hasContract_files: Sequelize.HasManyHasAssociationsMixin<ContractFile, ContractFileId>;
	declare countContract_files: Sequelize.HasManyCountAssociationsMixin;
	// Contract belongsTo Resident via resident_id
	declare resident: Resident;
	declare getResident: Sequelize.BelongsToGetAssociationMixin<Resident>;
	declare setResident: Sequelize.BelongsToSetAssociationMixin<Resident, ResidentId>;
	declare createResident: Sequelize.BelongsToCreateAssociationMixin<Resident>;
	// Contract 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>;
	// Contract 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>;
	// Contract 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 Contract {
		return Contract.init(
			{
				id: {
					type: DataTypes.UUID,
					allowNull: false,
					defaultValue: DataTypes.UUIDV4,
					primaryKey: true,
				},
				resident_id: {
					type: DataTypes.UUID,
					allowNull: false,
					references: {
						model: 'residents',
						key: 'id',
					},
				},
				contract_number: {
					type: DataTypes.STRING(100),
					allowNull: false,
				},
				contract_name: {
					type: DataTypes.STRING(255),
					allowNull: true,
				},
				description: {
					type: DataTypes.TEXT,
					allowNull: true,
				},
				rent_amount: {
					type: DataTypes.DECIMAL,
					allowNull: true,
				},
				deposit_amount: {
					type: DataTypes.DECIMAL,
					allowNull: true,
				},
				payment_day: {
					type: DataTypes.INTEGER,
					allowNull: true,
				},
				signed_date: {
					type: DataTypes.DATEONLY,
					allowNull: true,
				},
				expiry_date: {
					type: DataTypes.DATEONLY,
					allowNull: true,
				},
				created_by: {
					type: DataTypes.UUID,
					allowNull: true,
					references: {
						model: 'users',
						key: 'id',
					},
				},
				updated_by: {
					type: DataTypes.UUID,
					allowNull: true,
					references: {
						model: 'users',
						key: 'id',
					},
				},
				deleted_at: {
					type: DataTypes.DATE,
					allowNull: true,
				},
				deleted_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'),
				},
				contract_type_id: {
					type: DataTypes.UUID,
					allowNull: false,
					references: {
						model: 'contract_types',
						key: 'id',
					},
				},
			},
			{
				sequelize,
				tableName: 'contracts',
				schema: 'public',
				timestamps: false,

				scopes: {
					default: {
						where: {
							deleted_at: null,
						},
					},
					withDeleted: {},
					onlyDeleted: {
						where: {
							deleted_at: { [Op.ne]: null },
						},
					},
				},
				indexes: [
					{
						name: 'contracts_pkey',
						unique: true,
						fields: [{ name: 'id' }],
					},
					{
						name: 'idx_contracts_contract_type_id',
						fields: [{ name: 'contract_type_id' }],
					},
					{
						name: 'idx_contracts_number',
						fields: [{ name: 'contract_number' }],
					},
					{
						name: 'idx_contracts_resident_id',
						fields: [{ name: 'resident_id' }],
					},
					{
						name: 'uq_contracts_resident_number',
						unique: true,
						fields: [{ name: 'resident_id' }, { name: 'contract_number' }],
					},
				],
			},
		);
	}
}
