# -*- coding: utf-8 -*-
require "magellan/cli/resources"

module Magellan
  module Cli
    module Resources

      class Project < Base
        self.resource_name = "project"
        self.resource_dependency = nil
        self.hidden_fields = %w[default_nebula_id created_at updated_at].map(&:freeze).freeze
        self.field_associations = {"organization_id" => {name: "organization", class: "Organization"} }

        desc "update ATTRIBUTES", "update project with ATTRIBUTES"
        def update(attrs)
          s = load_selection("project")
          attrs = JSON.parse(File.readable?(attrs) ? File.read(attrs) : attrs)
          put_json("/admin/project/#{s['id']}/edit", {"project" => attrs})
        end

        desc "create NAME", "create new project with NAME"
        def create(name)
          o = load_selection(Organization.parameter_name)
          params = {
            parameter_name => {
              "organization_id" => o["id"],
              "name" => name,
            }
          }
          post_json("/admin/#{resource_name}/new.json", params)
          select(name)
        end
      end

    end
  end
end