class Admins::CompetitionBasicSettingService < ApplicationService
  attr_reader :competition, :params

  def initialize(competition, params)
    @params = params
    @competition = competition
  end

  def call
    ActiveRecord::Base.transaction do
      competition.name = strip params[:name]
      competition.sub_title = strip params[:sub_title]
      competition.start_time = params[:start_time]
      competition.end_time = params[:end_time]
      competition.mode = params[:mode]
      competition.identifier = strip params[:identifier]
      competition.bonus = params[:bonus]
      competition.awards_count = params[:awards_count]
      competition.description = strip params[:description]

      competition.save!

      # 竞赛模式相关设置
      if competition.mode == 1 || competition.mode == 4
        competition.competition_mode_setting&.destroy
      else
        setting = competition.competition_mode_setting || CompetitionModeSetting.create!(competition_id: competition.id)
        if competition.mode == 2
          setting.course_id = params[:course_id]
        elsif competition.mode == 3
          setting.start_time = params[:teach_start_time]
          setting.end_time = params[:teach_end_time]
        end
        setting.save!
      end

      # 管理员设置
      params[:manager_ids] = Array.wrap(params[:manager_ids]).map(&:to_i)
      old_manager_ids = competition.competition_managers.pluck(:user_id)
      new_manager_ids = params[:manager_ids] - old_manager_ids
      delete_manager_ids = old_manager_ids - params[:manager_ids]
      CompetitionManager.bulk_insert(*%i[user_id competition_id created_at updated_at]) do |worker|
        new_manager_ids.each do |manager_id|
          worker.add(user_id: manager_id, competition_id: competition.id)
        end
      end
      competition.competition_managers.where(user_id: delete_manager_ids).delete_all

      # 主办方设置
      params[:sponsor_schools] = Array.wrap(params[:sponsor_schools]).map(&:to_i)
      new_sponsor_school_ids = competition.sponsor_schools.pluck(:school_id)
      new_school_ids = params[:sponsor_schools] - new_sponsor_school_ids
      delete_school_ids = new_sponsor_school_ids - params[:sponsor_schools]
      new_school_ids.each do |school_id|
        CompetitionSchool.create!(competition_id: competition.id, school_id: school_id, source: 'sponsor')
      end
      competition.sponsor_schools.where(school_id: delete_school_ids).destroy_all

      # 开放范围设置
      params[:region_schools] = Array.wrap(params[:region_schools]).map(&:to_i)
      old_region_school_ids = competition.region_schools.pluck(:school_id)
      new_region_school_ids = params[:region_schools] - old_region_school_ids
      delete_region_school_ids = old_region_school_ids - params[:region_schools]
      new_region_school_ids.each do |school_id|
        CompetitionSchool.create!(competition_id: competition.id, school_id: school_id, source: 'region')
      end
      competition.region_schools.where(school_id: delete_region_school_ids).destroy_all unless delete_region_school_ids.length == 0

      competition
    end
  end
end