defmodule TestBed.Blog.Author do
  # Using Ash.Resource turns this module into an Ash resource.
  use Ash.Resource,
    # Tells Ash where the generated code interface belongs
    domain: TestBed.Blog,
    # Tells Ash you want this resource to store its data in Postgres.
    data_layer: AshPostgres.DataLayer

  # The Postgres keyword is specific to the AshPostgres module.
  postgres do
    # Tells Postgres what to call the table
    table("authors")
    # Tells Ash how to interface with the Postgres table
    repo(TestBed.Repo)
  end

  actions do
    # Exposes default built in actions to manage the resource
    defaults([:read, :destroy])

    create :create do
      primary? true
      # accept title as input
      accept([:name])
    end

    update :update do
      # accept content as input
      accept([:name])
    end

    # Defines custom read action which fetches post by id.
    read :by_id do
      # This action has one argument :id of type :uuid
      argument(:id, :uuid, allow_nil?: false)
      # Tells us we expect this action to return a single result
      get?(true)
      # Filters the `:id` given in the argument
      # against the `id` of each element in the resource
      filter(expr(id == ^arg(:id)))
    end
  end

  # Attributes are simple pieces of data that exist in your resource
  attributes do
    # Add an autogenerated UUID primary key called `:id`.
    uuid_primary_key(:id)
    # Add a string type attribute called `:title`
    attribute :name, :string do
      # We don't want the title to ever be `nil`
      allow_nil?(false)
    end
  end
end
