/**
 * Represents a product with various attributes.
 * 
 * @class
 * @property {string|null} name - The name of the product
 * @property {string|null} description - A description of the product
 * @property {string|null} author - The author or creator of the product
 * @property {string|null} image - An image URL or reference for the product
 * @property {number|null} stock_count - The current inventory count
 * @property {string|null} barcode - The unique barcode identifier 
 */
export class product {
    name;
    description;
    author;
    image;
    stock_count;
    barcode;
    thumbnail;
    on_stock;

    constructor(target) {
        this.name = null;
        this.description = null;
        this.author = null;
        this.image = null;
        this.stock_count = null;
        this.barcode = null;
        this.thumbnail = null;
        this.on_stock = null;

        if ("name" in target) this.name = target["name"];
        if ("description" in target) this.description = target["description"];
        if ("author" in target) this.author = target["author"];
        if ("image" in target) this.image = target["image"];
        if ("stock_count" in target) this.stock_count = target["stock_count"];
        if ("barcode" in target) this.barcode = target["barcode"];
        if ("thumbnail" in target) this.thumbnail = target["thumbnail"];
        if ("on_stock" in target) this.on_stock = target["on_stock"];

        try {
            this.stock_count = Number(this.stock_count);
        } catch {
            this.stock_count = 0;
        }

        try {
            this.on_stock = Number(this.on_stock);        
        } catch {
            this.on_stock = 0;
        }
    }

    get dump() {
        const dumped = {
            "name": new String(this.name),
            "description": new String(this.description),
            "author": new String(this.author),
            "image": new String(this.image),
            "barcode": new String(this.barcode),
            "thumbnail": new String(this.thumbnail),
            "stock_count": new String(this.stock_count)
        };

        if (this.on_stock !== null) {
            dumped["on_stock"] = new String(this.on_stock);
        }

        return dumped;
    }   

    get ready() {
        if (this.name === null || this.description === null) return false;
        if (this.author === null || this.image === null) return false;
        if (this.stock_count === null || this.barcode === null) return false;
        if (this.thumbnail === null) return false;

        return true;
    }   

    copy() {
        return new product(this.dump);
    }
}
