import { ajax } from "discourse/lib/ajax";

const AdminProduct = Discourse.Model.extend({
  isNew: false,
  metadata: {},

  destroy() {
    return ajax(`/s/admin/products/${this.id}`, { method: "delete" });
  },

  save() {
    const data = {
      name: this.name,
      statement_descriptor: this.statement_descriptor,
      metadata: this.metadata,
      active: this.active
    };

    return ajax("/s/admin/products", {
      method: "post",
      data
    }).then(product => AdminProduct.create(product));
  },

  update() {
    const data = {
      name: this.name,
      statement_descriptor: this.statement_descriptor,
      metadata: this.metadata,
      active: this.active
    };

    return ajax(`/s/admin/products/${this.id}`, {
      method: "patch",
      data
    });
  }
});

AdminProduct.reopenClass({
  findAll() {
    return ajax("/s/admin/products", { method: "get" }).then(result =>
      result.map(product => AdminProduct.create(product))
    );
  },

  find(id) {
    return ajax(`/s/admin/products/${id}`, {
      method: "get"
    }).then(product => AdminProduct.create(product));
  }
});

export default AdminProduct;
