class Embed < ApplicationRecord
  belongs_to :parent, class_name: "Embed", foreign_key: :parent_id, primary_key: :provider_id
  enum :source, {youtube_video: 0, youtube_channel: 1}

  def channel
    youtube_video? && self.class.youtube_channel.find_by_provider_id(parent_id)
  end

  def duration_in_seconds
    return unless duration = data.safe_dig("contentDetails", "duration")

    parts = duration.match(/^
      (?<sign>\+|-)?
      P(?:
        (?:
          (?:(?<years>\d+)Y)?
          (?:(?<months>\d+)M)?
          (?:(?<days>\d+)D)?
          (?<time>T
            (?:(?<hours>\d+(?:[.,]\d+)?)H)?
            (?:(?<minutes>\d+(?:[.,]\d+)?)M)?
            (?:(?<seconds>\d+(?:[.,]\d+)?)S)?
          )?
        ) |
        (?<weeks>\d+(?:[.,]\d+)?W)
      )
    $/x)

    parts = parts.named_captures.transform_values(&:to_i)

    (((((parts["weeks"] * 7) + parts["days"]) * 24 + parts["hours"]) * 60) + parts["minutes"]) * 60 + parts["seconds"]
  end

  def chapters
    text = data.safe_dig("snippet", "description") || ""
    @chapters ||= TextToChapters.call(text, duration_in_seconds)
  end

  def live_broadcast_content
    data.safe_dig("snippet", "liveBroadcastContent")
  end

  def scheduled_start_time
    data.safe_dig("liveStreamingDetails", "scheduledStartTime")
  end

  def scheduled_time
    Time.parse(scheduled_start_time) if scheduled_start_time
  end
end
