
import {
    DataTypes
} from 'sequelize';

import {
    LabelDB as Database
} from './db_conn.mjs'

import {
    Queue
} from "./queue.model.mjs";

import {
    Template,
    Variable
} from "./template.model.mjs";


export const Job = Database.define('job', {
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    }
}, {
    timestamps: false
});


export const JobValue = Database.define('jobValue', {
    id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true
    },
    key: {
        type: DataTypes.STRING,
        allowNull: false
    },
    value: {
        type: DataTypes.STRING,
        allowNull: false
    }
}, {
    tableName: 'job_values',
    timestamps: false
});


JobValue.belongsTo(Job, {
    onDelete: 'CASCADE',
    foreignKey: {
        allowNull: false
    }
});
Job.hasMany(JobValue, {
    onDelete: 'CASCADE',
    foreignKey: {
        allowNull: false
    }
});


Job.belongsTo(Queue, {
    onDelete: 'CASCADE',
    foreignKey: {
        allowNull: false
    }
});
Queue.hasMany(Job, {
    onDelete: 'CASCADE',
    foreignKey: {
        allowNull: false
    }
});


Job.belongsTo(Template);
Template.hasMany(Job, {
    onDelete: 'CASCADE',
    foreignKey: {
        allowNull: false
    }
});
