class Account < ApplicationRecord
  include Syncable
  include PgSearch
  has_many :account_auths
  has_many :users, through: :account_auths
  has_many :projects, dependent: :destroy
  has_many :videos, through: :projects
  has_many :contract_templates, through: :projects

  validates :name, presence: true
  validates :slug, presence: true, uniqueness: true

  before_validation :set_slug

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

  has_one_attached :logo

  pg_search_scope :search, {
    against: [:name, :slug],
    using: {
      tsearch: {any_word: true, prefix: true},
      trigram: {},
      dmetaphone: {any_word: true}
    }
  }

  def managers
    users.where(account_auths: { role: :account_manager })
  end

  def current_month_video_duration_total
    @current_month_video_duration_total ||=
      Video.with_attached_file.where(project: projects, created_at: Time.zone.now.beginning_of_month..Time.zone.now.end_of_month).sum { |video| video.file.blob.metadata.dig("duration").to_f }
  end

  def video_duration_total
    @video_duration_total ||=
      Video.with_attached_file.where(project: projects).sum { |video| video.file.blob.metadata.dig("duration").to_f }
  end

  def storage_total
    ActiveStorage::Blob.joins(:attachments).merge(
      ActiveStorage::Attachment.where(record: [
        AppearanceRelease.where(project: projects),
        TalentRelease.where(project: projects),
        MaterialRelease.where(project: projects),
        LocationRelease.where(project: projects),
        AcquiredMediaRelease.where(project: projects),
        Import.where(project: projects),
        MusicRelease.where(project: projects),
        Video.where(project: projects),
        Directory.where(project: projects),
        Download.where(project: projects),
        User.joins(:project_memberships).where(project_memberships: { project: projects }),
        Broadcast.where(project: projects),
        ZoomMeeting.where(project: projects),
        MedicalRelease.where(project: projects),
        MatchingRequest.where(project: projects),
        self
      ])).sum(:byte_size).to_f
  end

  def to_param
    slug
  end

  def me_suite_enabled?
    plan_uid.to_s == "me_suite"
  end

  def deliverme_enabled?
    plan_uid.to_s == "me_suite" || plan_uid.to_s == "deliverme"
  end

  def directme_enabled?
    plan_uid.to_s == "me_suite" || plan_uid.to_s == "directme"
  end

  def releaseme_enabled?
    plan_uid.to_s == "me_suite" || plan_uid.to_s == "releaseme"
  end

  def plan_name
    case plan_uid.to_s
    when "deliverme"
      "DeliverME"
    when "directme"
      "DirectME"
    when "releaseme"
      "ReleaseME"
    when "me_suite"
      "ME Suite"
    end
  end

  private

  def set_slug
    return if name.blank? || slug.present?

    counter = 1
    begin
      if counter == 1
        self.slug = name.parameterize
      else
        self.slug = [name.parameterize, counter].join("-")
      end
      counter += 1
    end while self.class.exists?(slug: self.slug)
  end
end
