
const DatabaseModel = require("./DatabaseModel.js").DatabaseModel;

function MediaFileModel() {
    DatabaseModel.call(this);
    this.path = "";
    this.md5sum = "";
    this.fixedSum = "";
    this.date = null;
    this.version = 0;
    this.inaccessible = 0;
    this.updateVersion();
}

MediaFileModel.prototype = Object.create(DatabaseModel.prototype);

MediaFileModel.prototype.getTableName = function() {
    return "mediaFile";
}

MediaFileModel.prototype.createOrUpdateBase = async function(dbHelper) {
    await dbHelper.runSql(`CREATE TABLE IF NOT EXISTS 'mediaFile' (
        path STRING NOT NULL,
        md5sum varchar(32) NOT NULL,
        fixedSum varchar(32) NOT NULL,
        date DateTime NOT NULL,
        inaccessible DateTime NOT NULL,
        version integer NOT NULL,
        PRIMARY KEY (path, md5sum))`);
}

MediaFileModel.prototype.describe = function() {
    return {
        "path": this.path,
        "md5sum": this.md5sum,
        "fixedSum": this.fixedSum,
        "date": this.date?.getTime(),
        "inaccessible": this.inaccessible,
        "version": this.version
    };
}

MediaFileModel.prototype.versionColumn = function() { return ""; }

MediaFileModel.prototype.fromDb = function(dbObj) {
    this.path = dbObj["path"];
    this.md5sum = dbObj["md5sum"];
    this.fixedSum = dbObj["fixedSum"];
    this.date = new Date(dbObj["date"]);
    this.inaccessible = dbObj["inaccessible"] || 0;
    this.version = dbObj["fixedSum"];
}

MediaFileModel.prototype.updateVersion = function() {
    this.version = Date.now();
}

module.exports.MediaFileModel = MediaFileModel;

