class EdFi::Indiana::V2026::CalendarDatesService < EdFi::Indiana::V2026::ApplicationService
  def call
    school_days.each { |d| create_or_update(d, props(d)) }
  end

  def delete(ed_fi_id)
    checked_id = ed_fi_id || @params[:id]
    return if !checked_id || checked_id.school_year != school_year

    response = rest_client_request(:delete, "#{url}/#{checked_id.number}", nil)
    response.nil? ? false : checked_id.delete
  end

  def endpoint
    'ed-fi/calendardates'
  end

  private
    def school_days
      @school_days ||= school_year.school_days
        .includes(:day_descriptor)
        .where.not(hours: 0)
        .where.not(day_descriptor_id: nil)
    end

    def props(day)
      code = day.day_descriptor.code
      {
        schoolReference: {
          schoolId: state_id.number
        },
        date: day.date.strftime('%Y-%m-%d'),
        calendarReference: {
          calendarCode: reporting_config.school_year_id,
          schoolId: state_id.number,
          schoolYear: school_year.academic_year
        },
        calendarEvents: [
          {
            calendarEventDescriptor: "uri://doe.in.gov/CalendarEventDescriptor##{code}"
          }
        ],
        _ext: {
          IDOE: {
            eventDuration: day.half_day? ? 0.5 : 1,
            eventMinutes: (day.hours * 60).to_i
          }
        }
      }
    end
end
