import * as Sequelize from 'sequelize';
import { Op } from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { IncidentFile, IncidentFileId } from './IncidentFile';
import type { IncidentType, IncidentTypeId } from './IncidentType';
import type { Room, RoomId } from './Room';
import type { Technician, TechnicianId } from './Technician';
import type { User, UserId } from './User';
export interface IncidentAttributes {
	id: string;
	title: string;
	description?: string | null;
	type_id: string;
	room_id: string;
	reporter_id: string;
	technician_id?: string | null;
	priority: 'low' | 'medium' | 'high' | 'urgent';
	status: 'new' | 'assigned' | 'in_progress' | 'pending_material' | 'completed' | 'cancelled' | 'on_hold';
	deleted_at?: Date | null;
	deleted_by?: string | null;
	created_by?: string | null;
	updated_by?: string | null;
	created_at: Date;
	updated_at: Date;
	code?: string | null;
	deadline?: Date | null;
}
export type IncidentPk = 'id';
export type IncidentId = Incident[IncidentPk];
export type IncidentOptionalAttributes =
	| 'id'
	| 'description'
	| 'technician_id'
	| 'priority'
	| 'status'
	| 'deleted_at'
	| 'deleted_by'
	| 'created_by'
	| 'updated_by'
	| 'created_at'
	| 'updated_at'
	| 'code'
	| 'deadline';
export type IncidentCreationAttributes = Optional<IncidentAttributes, IncidentOptionalAttributes>;
export class Incident extends Model<IncidentAttributes> implements IncidentAttributes {
	declare id: string;
	declare title: string;
	declare description?: string | null;
	declare type_id: string;
	declare room_id: string;
	declare reporter_id: string;
	declare technician_id?: string | null;
	declare priority: 'low' | 'medium' | 'high' | 'urgent';
	declare status: 'new' | 'assigned' | 'in_progress' | 'pending_material' | 'completed' | 'cancelled' | 'on_hold';
	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;
	declare code?: string | null;
	declare deadline?: Date | null;
	// Incident belongsTo IncidentType via type_id
	declare type: IncidentType;
	declare getType: Sequelize.BelongsToGetAssociationMixin<IncidentType>;
	declare setType: Sequelize.BelongsToSetAssociationMixin<IncidentType, IncidentTypeId>;
	declare createType: Sequelize.BelongsToCreateAssociationMixin<IncidentType>;
	// Incident hasMany IncidentFile via incident_id
	declare incident_files: IncidentFile[];
	declare getIncident_files: Sequelize.HasManyGetAssociationsMixin<IncidentFile>;
	declare setIncident_files: Sequelize.HasManySetAssociationsMixin<IncidentFile, IncidentFileId>;
	declare addIncident_file: Sequelize.HasManyAddAssociationMixin<IncidentFile, IncidentFileId>;
	declare addIncident_files: Sequelize.HasManyAddAssociationsMixin<IncidentFile, IncidentFileId>;
	declare createIncident_file: Sequelize.HasManyCreateAssociationMixin<IncidentFile>;
	declare removeIncident_file: Sequelize.HasManyRemoveAssociationMixin<IncidentFile, IncidentFileId>;
	declare removeIncident_files: Sequelize.HasManyRemoveAssociationsMixin<IncidentFile, IncidentFileId>;
	declare hasIncident_file: Sequelize.HasManyHasAssociationMixin<IncidentFile, IncidentFileId>;
	declare hasIncident_files: Sequelize.HasManyHasAssociationsMixin<IncidentFile, IncidentFileId>;
	declare countIncident_files: Sequelize.HasManyCountAssociationsMixin;
	// Incident belongsTo Room via room_id
	declare room: Room;
	declare getRoom: Sequelize.BelongsToGetAssociationMixin<Room>;
	declare setRoom: Sequelize.BelongsToSetAssociationMixin<Room, RoomId>;
	declare createRoom: Sequelize.BelongsToCreateAssociationMixin<Room>;
	// Incident belongsTo Technician via technician_id
	declare technician: Technician;
	declare getTechnician: Sequelize.BelongsToGetAssociationMixin<Technician>;
	declare setTechnician: Sequelize.BelongsToSetAssociationMixin<Technician, TechnicianId>;
	declare createTechnician: Sequelize.BelongsToCreateAssociationMixin<Technician>;
	// Incident 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>;
	// Incident 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>;
	// Incident belongsTo User via reporter_id
	declare reporter: User;
	declare getReporter: Sequelize.BelongsToGetAssociationMixin<User>;
	declare setReporter: Sequelize.BelongsToSetAssociationMixin<User, UserId>;
	declare createReporter: Sequelize.BelongsToCreateAssociationMixin<User>;
	// Incident 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 Incident {
		return Incident.init(
			{
				id: {
					type: DataTypes.UUID,
					allowNull: false,
					defaultValue: DataTypes.UUIDV4,
					primaryKey: true,
				},
				title: {
					type: DataTypes.STRING(255),
					allowNull: false,
				},
				description: {
					type: DataTypes.TEXT,
					allowNull: true,
				},
				type_id: {
					type: DataTypes.UUID,
					allowNull: false,
					references: {
						model: 'incident_types',
						key: 'id',
					},
				},
				room_id: {
					type: DataTypes.UUID,
					allowNull: false,
					references: {
						model: 'rooms',
						key: 'id',
					},
				},
				reporter_id: {
					type: DataTypes.UUID,
					allowNull: false,
					references: {
						model: 'users',
						key: 'id',
					},
				},
				technician_id: {
					type: DataTypes.UUID,
					allowNull: true,
					references: {
						model: 'technicians',
						key: 'id',
					},
				},
				priority: {
					type: DataTypes.ENUM('low', 'medium', 'high', 'urgent'),
					allowNull: false,
					defaultValue: 'medium',
				},
				status: {
					type: DataTypes.ENUM(
						'new',
						'assigned',
						'in_progress',
						'pending_material',
						'completed',
						'cancelled',
						'on_hold',
					),
					allowNull: false,
					defaultValue: 'new',
				},
				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'),
				},
				code: {
					type: DataTypes.STRING(50),
					allowNull: true,
				},
				deadline: {
					type: DataTypes.DATE,
					allowNull: true,
				},
			},
			{
				sequelize,
				tableName: 'incidents',
				schema: 'public',
				timestamps: false,

				scopes: {
					default: {
						where: {
							deleted_at: null,
						},
					},
					withDeleted: {},
					onlyDeleted: {
						where: {
							deleted_at: { [Op.ne]: null },
						},
					},
				},
				indexes: [
					{
						name: 'idx_incidents_code',
						fields: [{ name: 'code' }],
					},
					{
						name: 'idx_incidents_code_unique',
						unique: true,
						fields: [{ name: 'code' }],
					},
					{
						name: 'idx_incidents_created_at',
						fields: [{ name: 'created_at', order: 'DESC' }],
					},
					{
						name: 'idx_incidents_deadline',
						fields: [{ name: 'deadline' }],
					},
					{
						name: 'idx_incidents_priority',
						fields: [{ name: 'priority' }],
					},
					{
						name: 'idx_incidents_reporter_id',
						fields: [{ name: 'reporter_id' }],
					},
					{
						name: 'idx_incidents_room_id',
						fields: [{ name: 'room_id' }],
					},
					{
						name: 'idx_incidents_status',
						fields: [{ name: 'status' }],
					},
					{
						name: 'idx_incidents_technician_id',
						fields: [{ name: 'technician_id' }],
					},
					{
						name: 'idx_incidents_type_id',
						fields: [{ name: 'type_id' }],
					},
					{
						name: 'incidents_pkey',
						unique: true,
						fields: [{ name: 'id' }],
					},
				],
			},
		);
	}
}
