defmodule Outlook.Articles.Article do
  use Ecto.Schema
  import Ecto.Changeset

  alias Outlook.Articles.InternalTree
  alias Outlook.Authors.Author
  alias Outlook.Translations.Translation

  schema "articles" do
    field :content, InternalTree
    field :date, :utc_datetime
    field :language, :string, default: "EN"
    field :title, :string
    field :url, :string
    belongs_to :author, Author
    has_many :translations, Translation, on_delete: :delete_all

    timestamps()
  end

  @doc false
  def changeset(article, attrs) do
    article
    |> cast(attrs, [:title, :content, :url, :language, :date, :author_id])
    |> validate_required([:title, :content, :url, :language, :date, :author_id])
    |> foreign_key_constraint(:author_id)
  end
end
