import {sequelize} from "../app";
import * as crypto from "crypto";
import {DataTypes, InferAttributes, InferCreationAttributes, Model} from "sequelize";

export interface IShortenedLink extends Model<InferAttributes<IShortenedLink>, InferCreationAttributes<IShortenedLink>> {
    shortenedId?: string
    originalUrl: string
    clientId?: number
    clicks?: number
}

const ShortenedSchema = sequelize.define<IShortenedLink>('shortened_links', {
    shortenedId: {
        type: DataTypes.STRING,
        defaultValue: () => crypto.randomBytes(3).toString('hex'),
        allowNull: true
    },
    originalUrl: {
        type: DataTypes.STRING,
        allowNull: false
    },
    clientId: {
        type: DataTypes.BIGINT,
        allowNull: false
    },
    clicks: {
        type: DataTypes.INTEGER,
        defaultValue: 0
    }
});

export const ShortenedLink = ShortenedSchema;