defmodule TololoCore.Brands.Brand do
  @moduledoc """
  Define the data fields for storing store brand information.
  """

  use Ash.Resource,
    otp_app: :tololo,
    domain: TololoCore.Brands,
    extensions: [AshGraphql.Resource, AshAdmin.Resource],
    data_layer: AshPostgres.DataLayer,
    authorizers: [Ash.Policy.Authorizer],
    notifiers: [TololoCore.Kafka.AshNotifier]

  use Gettext, backend: TololoCore.Gettext
  alias Ash.Changeset

  graphql do
    type :brand

    queries do
      read_one :get_brand, :read
    end

    mutations do
    end
  end

  admin do
  end

  postgres do
    table "brands"
    repo Tololo.Repo

    references do
    end
  end

  field_policies do
    field_policy :* do
      description "the rest of the fields don't require any special policies"
      authorize_if always()
    end
  end

  code_interface do
    define :create
    define :update
  end

  actions do
    defaults [:read, :destroy, create: :*, update: :*]
  end

  policies do
    bypass always() do
      description "admin has access to every action"
      authorize_if actor_attribute_equals(:access_level, :admin)
    end

    policy action_type(:read) do
      description "read access is always allowed"
      authorize_if always()
    end
  end

  attributes do
    uuid_v7_primary_key :id

    attribute :name, :string do
      allow_nil? false
      sensitive? false
      public? true
    end

    # Define a Headquarters Tololo URL to obtain
    # Common brand and products information
    attribute :hq_url, :string do
      allow_nil? true
      sensitive? true
      public? false
    end

    timestamps()
  end

  relationships do
    has_one :branch, TololoCore.Brands.Branch do
      public? true
    end
  end
end
