class Project < ApplicationRecord
  include Archivable
  include Filterable
  include Syncable

  SIGNABLE_RELEASE_TYPES = %w(talent appearance acquired_media location material medical)
  AVAILABLE_RELEASE_TYPES = %w(appearance location material acquired_media talent music medical)

  belongs_to :account
  has_many :acquired_media_releases, dependent: :destroy
  has_many :appearance_releases, dependent: :destroy
  has_many :location_releases, dependent: :destroy
  has_many :material_releases, dependent: :destroy
  has_many :music_releases, dependent: :destroy
  has_many :talent_releases, dependent: :destroy
  has_many :medical_releases, dependent: :destroy
  has_many :videos, dependent: :destroy
  has_many :imports, dependent: :destroy
  has_many :contract_templates, dependent: :destroy
  has_many :project_memberships, dependent: :destroy
  has_many :users, through: :project_memberships
  has_many :directories, dependent: :destroy
  has_many :downloads, dependent: :destroy
  has_many :broadcasts, dependent: :destroy
  has_many :zoom_meetings, dependent: :destroy

  accepts_nested_attributes_for :project_memberships

  has_settings do |s|
    s.key :features, defaults: {
      acquired_media_release: false,
      appearance_release: true,
      location_release: false,
      material_release: false,
      music_release: false,
      talent_release: false,
      medical_release: false,
      video_analysis: false,
    }
  end

  validates :name, presence: true, uniqueness: { scope: :account_id }

  filterable_by :active, :inactive, :archived

  scope :order_by_name, -> { order(name: :asc) }

  def all_releases_count
    AVAILABLE_RELEASE_TYPES.sum { |name| public_send("#{name}_releases").count }
  end

  def members
    users + account.managers
  end

  attr_reader :predefined_client_name
  def predefined_client_name=(value)
    @predefined_client_name = value

    case value.to_s
    when "discovery"
      self.client_name = value.titleize
      self.settings(:features).attributes = {
        acquired_media_release: true,
        appearance_release: true,
        location_release: true,
        material_release: true,
        music_release: true,
        talent_release: true,
        medical_release: true,
        video_analysis: true,
      }
    when "nat_geo"
      self.client_name = value.titleize
      self.settings(:features).attributes = {
        acquired_media_release: true,
        appearance_release: true,
        location_release: true,
        material_release: true,
        music_release: true,
        talent_release: true,
        medical_release: true,
        video_analysis: true,
      }
    else
      # Do nothing - the features will be set manually
    end
  end

  def features_settings=(values)
    return if self.predefined_client_name.to_s != "other"
    settings(:features).attributes = values.transform_values { |v| !v.to_i.zero? }
  end

  def feature_enabled?(name)
    settings(:features).send(name)
  end

  def import_contract_templates(template_ids)
    ContractTemplate.where(id: template_ids).each do |contract_template|
      contract_templates << contract_template.dup.tap do |imported_contract_template|
        imported_contract_template.parent = contract_template

        # Rich text fields must be manually duplicated because they are associations
        if contract_template.body.present?
          imported_contract_template.body.body = contract_template.body.body.dup
        end
        if contract_template.guardian_clause.present?
          imported_contract_template.guardian_clause.body = contract_template.guardian_clause.body.dup
        end
      end
    end
    save
  end

  def zoom_meeting_url
    zoom_meeting.meeting_url
  end

  def zoom_meeting
    current_zoom_meeting = zoom_meetings.active.first

    unless current_zoom_meeting.present?
      zoom_user = ZoomUser.for_new_meeting
      current_zoom_meeting = ZoomMeeting.create(zoom_user: zoom_user, project: self)
    end

    current_zoom_meeting
  end
end
