defmodule Operately.Activities.Content.GoalEditing do
  use Operately.Activities.Content

  defmodule UpdatedTarget do
    use Operately.Activities.Content

    embedded_schema do
      field :id, :string

      field :old_name, :string
      field :new_name, :string

      field :old_from, :float
      field :new_from, :float

      field :old_to, :float
      field :new_to, :float

      field :old_unit, :string
      field :new_unit, :string

      field :old_index, :integer
      field :new_index, :integer
    end

    def changeset(update, attrs) do
      update
      |> cast(attrs, __schema__(:fields))
      |> validate_required(__schema__(:fields))
    end
  end

  defmodule AddedTarget do
    use Operately.Activities.Content

    embedded_schema do
      field :id, :string
      field :name, :string
      field :from, :float
      field :to, :float
      field :unit, :string
      field :index, :integer
    end

    def changeset(update, attrs) do
      update
      |> cast(attrs, __schema__(:fields))
      |> validate_required(__schema__(:fields))
    end
  end

  defmodule DeletedTarget do
    use Operately.Activities.Content

    embedded_schema do
      field :id, :string
      field :name, :string
      field :from, :float
      field :to, :float
      field :unit, :string
      field :index, :integer
    end

    def changeset(update, attrs) do
      update
      |> cast(attrs, __schema__(:fields))
      |> validate_required(__schema__(:fields))
    end
  end

  embedded_schema do
    belongs_to :company, Operately.Companies.Company
    belongs_to :space, Operately.Groups.Group
    belongs_to :goal, Operately.Goals.Goal

    field :old_name, :string
    field :new_name, :string

    embeds_one :previous_timeframe, Operately.ContextualDates.Timeframe
    embeds_one :current_timeframe, Operately.ContextualDates.Timeframe

    belongs_to :old_champion, Operately.People.Person
    belongs_to :new_champion, Operately.People.Person

    belongs_to :old_reviewer, Operately.People.Person
    belongs_to :new_reviewer, Operately.People.Person

    embeds_many :added_targets, AddedTarget
    embeds_many :updated_targets, UpdatedTarget
    embeds_many :deleted_targets, DeletedTarget
  end

  def changeset(attrs) do
    %__MODULE__{}
    |> cast(attrs, [:company_id, :space_id, :goal_id, :old_name, :new_name, :old_champion_id, :new_champion_id, :old_reviewer_id, :new_reviewer_id] -- [:old_timeframe, :new_timeframe])
    |> cast_embed(:previous_timeframe)
    |> cast_embed(:current_timeframe)
    |> cast_embed(:added_targets)
    |> cast_embed(:updated_targets)
    |> cast_embed(:deleted_targets)
    |> validate_required([:company_id, :space_id, :goal_id, :old_name, :new_name, :old_champion_id, :new_champion_id, :old_reviewer_id, :new_reviewer_id] -- [:old_timeframe, :new_timeframe])
  end

  def build(params) do
    changeset(params)
  end
end
