// CREATE TABLE attachments (
//     id INT PRIMARY KEY AUTO_INCREMENT,
//     filename VARCHAR(255) NOT NULL,
//     filepath VARCHAR(255) NOT NULL,
//     file_type VARCHAR(50) NOT NULL,
//     file_size INT NOT NULL,
//     created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
//     updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
// );

import { Sequelize, DataTypes, Optional, Model } from "sequelize"

interface AttachmentAttributes {
    id: number
    filename: string
    filepath: string
    file_type: string
    file_size: number

    createdAt?: Date
    updatedAt?: Date
}

export interface AttachmentInput extends Optional<AttachmentAttributes, "id"> {}
export interface AttachmentOutput extends Required<AttachmentAttributes> {}
export type TAttachmentModel = ReturnType<typeof AttachmentModel>

type DT = typeof DataTypes
export default function AttachmentModel(sequelize: Sequelize, DataTypes: DT) {
    class Attachment extends Model<AttachmentAttributes, AttachmentInput> implements AttachmentAttributes {
        public id!: number
        public filename!: string
        public filepath!: string
        public file_type!: string
        public file_size!: number

        // timestamps!
        public readonly createdAt!: Date
        public readonly updatedAt!: Date
    }
    Attachment.init(
        {
            id: {
                type: DataTypes.INTEGER,
                autoIncrement: true,
                primaryKey: true,
            },
            filename: {
                type: DataTypes.STRING,
                allowNull: false,
            },
            filepath: {
                type: DataTypes.STRING,
                allowNull: false,
            },
            file_type: {
                type: DataTypes.STRING,
                allowNull: false,
            },
            file_size: {
                type: DataTypes.INTEGER,
                allowNull: false,
            },
        },
        {
            modelName: "attachment",
            sequelize,
            underscored: true,
            timestamps: true,
        },
    )

    return Attachment
}