import * as Sequelize from 'sequelize';
import { Op } from 'sequelize';
import { DataTypes, Model, Optional } from 'sequelize';
import type { File, FileId } from './File';
import type { PostTarget, PostTargetId } from './PostTarget';
import type { User, UserId } from './User';
export interface PostAttributes {
	id: string;
	title: string;
	content?: string | null;
	target_type: string;
	status: string;
	is_featured: boolean;
	author_id: string;
	published_at?: Date | null;
	deleted_at?: Date | null;
	deleted_by?: string | null;
	created_by?: string | null;
	updated_by?: string | null;
	created_at: Date;
	updated_at: Date;
	thumbnail_id?: string | null;
}
export type PostPk = 'id';
export type PostId = Post[PostPk];
export type PostOptionalAttributes =
	| 'id'
	| 'content'
	| 'status'
	| 'published_at'
	| 'deleted_at'
	| 'deleted_by'
	| 'created_by'
	| 'updated_by'
	| 'created_at'
	| 'updated_at'
	| 'thumbnail_id';
export type PostCreationAttributes = Optional<PostAttributes, PostOptionalAttributes>;
export class Post extends Model<PostAttributes> implements PostAttributes {
	declare id: string;
	declare title: string;
	declare content?: string | null;
	declare target_type: string;
	declare status: string;
	declare is_featured: boolean;
	declare author_id: string;
	declare published_at?: Date | null;
	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 thumbnail_id?: string | null;
	// Post belongsTo File via thumbnail_id
	declare thumbnail: File;
	declare getThumbnail: Sequelize.BelongsToGetAssociationMixin<File>;
	declare setThumbnail: Sequelize.BelongsToSetAssociationMixin<File, FileId>;
	declare createThumbnail: Sequelize.BelongsToCreateAssociationMixin<File>;
	// Post hasMany PostTarget via post_id
	declare post_targets: PostTarget[];
	declare getPost_targets: Sequelize.HasManyGetAssociationsMixin<PostTarget>;
	declare setPost_targets: Sequelize.HasManySetAssociationsMixin<PostTarget, PostTargetId>;
	declare addPost_target: Sequelize.HasManyAddAssociationMixin<PostTarget, PostTargetId>;
	declare addPost_targets: Sequelize.HasManyAddAssociationsMixin<PostTarget, PostTargetId>;
	declare createPost_target: Sequelize.HasManyCreateAssociationMixin<PostTarget>;
	declare removePost_target: Sequelize.HasManyRemoveAssociationMixin<PostTarget, PostTargetId>;
	declare removePost_targets: Sequelize.HasManyRemoveAssociationsMixin<PostTarget, PostTargetId>;
	declare hasPost_target: Sequelize.HasManyHasAssociationMixin<PostTarget, PostTargetId>;
	declare hasPost_targets: Sequelize.HasManyHasAssociationsMixin<PostTarget, PostTargetId>;
	declare countPost_targets: Sequelize.HasManyCountAssociationsMixin;
	// Post belongsTo User via author_id
	declare author: User;
	declare getAuthor: Sequelize.BelongsToGetAssociationMixin<User>;
	declare setAuthor: Sequelize.BelongsToSetAssociationMixin<User, UserId>;
	declare createAuthor: Sequelize.BelongsToCreateAssociationMixin<User>;
	// Post 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>;
	// Post 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>;
	// Post 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 Post {
		return Post.init(
			{
				id: {
					type: DataTypes.UUID,
					allowNull: false,
					defaultValue: DataTypes.UUIDV4,
					primaryKey: true,
				},
				title: {
					type: DataTypes.STRING(255),
					allowNull: false,
				},
				content: {
					type: DataTypes.TEXT,
					allowNull: true,
				},
				target_type: {
					type: DataTypes.STRING(20),
					allowNull: false,
				},
				status: {
					type: DataTypes.STRING(20),
					allowNull: false,
					defaultValue: 'draft',
				},
				is_featured: {
					type: DataTypes.BOOLEAN,
					allowNull: false,
					defaultValue: false,
				},
				author_id: {
					type: DataTypes.UUID,
					allowNull: false,
					references: {
						model: 'users',
						key: 'id',
					},
				},
				published_at: {
					type: DataTypes.DATE,
					allowNull: true,
				},
				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'),
				},
				thumbnail_id: {
					type: DataTypes.UUID,
					allowNull: true,
					references: {
						model: 'files',
						key: 'id',
					},
				},
			},
			{
				sequelize,
				tableName: 'posts',
				schema: 'public',
				hasTrigger: true,
				timestamps: false,

				scopes: {
					default: {
						where: {
							deleted_at: null,
						},
					},
					withDeleted: {},
					onlyDeleted: {
						where: {
							deleted_at: { [Op.ne]: null },
						},
					},
				},
				indexes: [
					{
						name: 'posts_pkey',
						unique: true,
						fields: [{ name: 'id' }],
					},
				],
			},
		);
	}
}
