const Model = require("flitter-orm/src/model/Model");
const uuid = require('uuid/v4');

/*
 * Node Model
 * -------------------------------------------------------------
 * Put some description here!
 */
class Node extends Model {
	static get services() {
		return [...super.services, 'models']
	}

	static get schema() {
		// Return a flitter-orm schema here.
		return {
			UUID: { type: String, default: () => uuid() },
			Type: String,
			Value: {
				Mode: {type: String, default: 'norm'},
				Value: String,
			},
			PageId: String,
			CreatedAt: { type: Date, default: () => new Date },
			UpdatedAt: { type: Date, default: () => new Date },
			CreatedUserId: String,
			UpdateUserId: String
		};
	}

	// Static and instance methods can go here
	get page() {
		const Page = this.models.get('api:Page')
		return this.belongs_to_one(Page, "PageId", "UUID")
	}

	to_html() {
		switch(this.Type){
			case 'paragraph':
				return '<p>' + this.Value.Value + '</p>'
			case 'header1':
				return '<h1>' + this.Value.Value + '</h1>'
			case 'header2':
				return '<h2>' + this.Value.Value + '</h2>'
			case 'header3':
				return '<h3>' + this.Value.Value + '</h3>'
			case 'header4':
				return '<h4>' + this.Value.Value + '</h4>'
			case 'block_code':
				return '<pre><code>' + this.Value.Value + '</code></pre>'
			case 'click_link':
				return '<a href="' + this.Value.Value + '">' + this.Value.Value + '</a>'
		}
	}

	update_from_raw(data) {
		if ( data.Type ) this.Type = data.Type
		if ( data.Value ) this.Value = data.Value
	}
}

module.exports = exports = Node;
