import { DataTypes, Model, Optional, Sequelize } from 'sequelize';

export interface AttachmentAttributes extends Optional<AttachmentInterface, 'id'> { }

export class Attachment extends Model<AttachmentInterface, AttachmentAttributes> implements AttachmentInterface {
    public id!: number;
    public userId!: number;
    public accountId!: number;
    public fileName!: string;
    public extension!: string;
    public uniqueName!: string;
    public filePath!: string;
    public type!: number;
    public size!: number;
    public thumbnails!: Record<string, unknown> | null;
    public dataKey!: Text | string | null;
    public altText!: string | null;
    public caption!: string | null;
    public title!: string | null;
    public description!: Text | string | null;
    public status!: number;
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;

    static initModel(sequelize: Sequelize): typeof Attachment {
        Attachment.init(
            {
                id: { type: DataTypes.BIGINT, autoIncrement: true, primaryKey: true, comment: "unique identifier" },
                userId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "ref to User" },
                accountId: { type: DataTypes.BIGINT, allowNull: true, defaultValue: null, comment: "ref to user account" },
                fileName: { type: DataTypes.STRING, allowNull: false, comment: "file name" },
                extension: { type: DataTypes.STRING, allowNull: false, comment: "file extension" },
                uniqueName: { type: DataTypes.STRING, allowNull: false, unique: "unique-attachment", comment: "file unique name" },
                filePath: { type: DataTypes.STRING, allowNull: false, comment: "file path" },
                type: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 1, comment: "1=>Stored in file system, 2=>Stored on S3 bucket" },
                size: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, comment: "filesize in bytes" },
                thumbnails: { type: DataTypes.JSON, allowNull: true, defaultValue: null, comment: "thumbnail and derived media metadata" },
                dataKey: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "datakey for the file" },
                altText: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "SEO alt text" },
                caption: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "attachment caption" },
                title: { type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: "attachment title" },
                description: { type: DataTypes.TEXT, allowNull: true, defaultValue: null, comment: "attachment description" },
                status: { type: DataTypes.INTEGER, defaultValue: 0, comment: "Status of file uploaded. 0-> not connected to entity, 1-> Connected to entity" }
            },
            {
                paranoid: true,
                underscored: true,
                sequelize,
                tableName: 'attachments',
                timestamps: true,
            }
        );
        return Attachment;
    }

    public static associate(models: any) {
        Attachment.belongsTo(models.User, { foreignKey: 'userId', as: 'author' });
    }
}
